[2022年]SVMの学習セットのフォーマット(CSVファイル)

今回はSVMを行う際に必要なデータのフォーマット形式を紹介します。
以下の2種類の方法を紹介します。

①トレーニング用のCSVファイルデータとテスト用の配列データを用いる方法
②トレーニング用とテスト用のCSVファイルデータを用いる方法
***

手の座標の見本です。
![a.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2702119/54becfa8-b2f2-b1ef-5da7-dbcb80cc249d.jpeg)

・[MediaPipeで取得した座標データの統一化の方法[CSVファイル]](https://qiita.com/netineti512/private/adf23a6fee757668c1dc)

上記のサイトで取得した「P1〜P20」を説明変数とし、21番目に「pose」の変数を加え、画像データに該当する手の形の値を加える。それを目的変数とする。
|poseの値|手の形|
|---|---|
|0|手を挙げている|
|1|goodのポーズ|
|2|中指立てる|
|3|口に人差し指をあてるポーズ|


トレーニング用のデータ(CSVファイル)を用意します。
※以下のデータは「中指を立てる」ポーズなので他のポーズのデータが必要です。
```csv:train_naka.csv
#file_name,0_x,0_y,0_z,1_x,1_y,1_z,2_x,2_y,2_z,3_x,3_y,3_z,4_x,4_y,4_z,5_x,5_y,5_z,6_x,6_y,6_z,7_x,7_y,7_z,8_x,8_y,8_z,9_x,9_y,9_z,10_x,10_y,10_z,11_x,11_y,11_z,12_x,12_y,12_z,13_x,13_y,13_z,14_x,14_y,14_z,15_x,15_y,15_z,16_x,16_y,16_z,17_x,17_y,17_z,18_x,18_y,18_z,19_x,19_y,19_z,20_x,20_y,20_z

train1_naka.jpg,0.389803499,0.801913738,1.17E-06,0.543673038,0.713049769,-0.004092365,0.606320858,0.597075701,-0.082220919,0.541908503,0.490023404,-0.139316931,0.449993223,0.411764026,-0.182852536,0.655518532,0.611132681,-0.361248881,0.601327658,0.440905184,-0.363025904,0.551220357,0.471162826,-0.274242491,0.546140015,0.510585964,-0.214817867,0.52099359,0.617507637,-0.37574178,0.49615398,0.401354373,-0.470776081,0.500869513,0.286642373,-0.457072645,0.504625559,0.208498865,-0.454686075,0.384011745,0.612078309,-0.346963972,0.410913289,0.456780493,-0.355458677,0.423998296,0.497195512,-0.24509716,0.426028758,0.54156214,-0.183991969,0.27774024,0.599688888,-0.313295215,0.338105261,0.477949828,-0.276744634,0.348003089,0.512286663,-0.181187212,0.343510807,0.54753226,-0.125042766
train2_naka.jpg,0.372496843,0.628432393,-2.40E-07,0.374212742,0.494185925,0.069492929,0.395322174,0.411588341,0.08281707,0.40517801,0.346344531,0.070942067,0.435244918,0.311984569,0.059673067,0.508733571,0.451129079,0.09572342,0.500271916,0.334446698,0.085553415,0.470387101,0.333002806,0.076344244,0.461937785,0.348397315,0.070135526,0.517293453,0.462896049,0.039495688,0.517974496,0.29879266,0.024034292,0.509268165,0.202741519,0.0063766,0.514311612,0.132987827,-0.006171352,0.507725716,0.469993651,-0.016582165,0.465638489,0.333584279,-0.029415699,0.426328689,0.394639224,-0.021574484,0.432011366,0.445190787,-0.014548351,0.489029557,0.471302301,-0.069037214,0.450983316,0.368504882,-0.069542766,0.421672225,0.418516129,-0.054685004,0.425720096,0.467970699,-0.044081938
train3_naka.jpg,0.274685919,0.780708909,-2.28E-07,0.408616662,0.686340928,0.079342514,0.455591828,0.558643878,0.047753569,0.368123889,0.432402402,0.002857706,0.27842012,0.35755384,-0.035260428,0.612514853,0.540450513,-0.12617141,0.524855673,0.358763993,-0.145434484,0.44557488,0.387057424,-0.112501658,0.432187796,0.430043101,-0.090421207,0.515985489,0.548542976,-0.201803893,0.455659211,0.319143027,-0.253439188,0.430850148,0.207008421,-0.252630502,0.429816395,0.127613872,-0.264951915,0.396300972,0.554936171,-0.250595987,0.329391122,0.381238997,-0.255305946,0.286241621,0.430437267,-0.176496536,0.287454575,0.478170782,-0.136999264,0.275396198,0.563065588,-0.292133868,0.238096178,0.437929094,-0.272287577,0.214001596,0.483849287,-0.196431071,0.226783335,0.527509272,-0.150058031
train4_naka.jpg,0.389803499,0.801913738,1.17E-06,0.543673038,0.713049769,-0.004092365,0.606320858,0.597075701,-0.082220919,0.541908503,0.490023404,-0.139316931,0.449993223,0.411764026,-0.182852536,0.655518532,0.611132681,-0.361248881,0.601327658,0.440905184,-0.363025904,0.551220357,0.471162826,-0.274242491,0.546140015,0.510585964,-0.214817867,0.52099359,0.617507637,-0.37574178,0.49615398,0.401354373,-0.470776081,0.500869513,0.286642373,-0.457072645,0.504625559,0.208498865,-0.454686075,0.384011745,0.612078309,-0.346963972,0.410913289,0.456780493,-0.355458677,0.423998296,0.497195512,-0.24509716,0.426028758,0.54156214,-0.183991969,0.27774024,0.599688888,-0.313295215,0.338105261,0.477949828,-0.276744634,0.348003089,0.512286663,-0.181187212,0.343510807,0.54753226,-0.125042766
train5_naka.jpg,0.524961829,0.984922409,5.10E-07,0.732751012,0.89183557,-0.091441698,0.793630004,0.665617824,-0.107155629,0.70465064,0.485649437,-0.120235041,0.527832687,0.456020325,-0.119114965,0.646648347,0.602651298,-0.01932917,0.652660668,0.470028818,-0.121000238,0.62683928,0.548590302,-0.190049157,0.581981122,0.620753765,-0.216853067,0.514869094,0.601325035,-0.01736442,0.51281482,0.405975699,-0.101172484,0.514294803,0.279683113,-0.15259324,0.504913807,0.161186218,-0.179147989,0.393526435,0.636616826,-0.03461368,0.364629209,0.498105884,-0.177425906,0.45121938,0.627796412,-0.184088528,0.498499632,0.732604563,-0.144077986,0.281677991,0.697762132,-0.056949627,0.297564685,0.588006616,-0.16391699,0.374960333,0.685463727,-0.139755785,0.410475522,0.773387969,-0.094834082
```
上記五枚の座標データを以下の記事で統一化する。
・[MediaPipeで取得した座標データの統一化の方法[CSVファイル]](https://qiita.com/netineti512/private/adf23a6fee757668c1dc)

統一化したCSVデータです。
末尾に目的変数poseを追加しときます。
```distance_naka.csv
#file_name,P1, P2, P3, P5, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14, P15, p16, p17, P18, P19, P20, pose

landmark_naka.csv,0.35079307,0.493618097,0.346770268,0.137221039,0.605779369,0.482234558,0.367999511,0.356417305,0.299088274,0.242458722,0.253209234,0.261772299,-0.01320409,0.048126277,0.077957586,0.08258665,-0.255482761,-0.117862078,-0.095296927,-0.105538469,-0.202592824,-0.466991481,-0.711050208,-0.889466597,-0.434944259,-0.82303034,-0.754048713,-0.664171511,-0.420410581,-0.913198612,-1.174719995,-1.352872222,-0.432788409,-0.786837677,-0.694699176,-0.593551764,-0.461033915,-0.738575649,-0.660294243,-0.579941035,-0.009332476,-0.187450611,-0.317618579,-0.416871418,-0.823580907,-0.827632179,-0.625223007,-0.489746249,-0.85662194,-1.073281939,-1.04204073,-1.036599808,-0.791014051,-0.810380351,-0.558777236,-0.419469107,-0.714255735,-0.630927413,-0.413074798,-0.285076218,2
distance2_naka.csv,0.007679244,0.102151285,0.146259575,0.280819433,0.609706683,0.571837837,0.438092909,0.400279285,0.648015128,0.651063032,0.612099177,0.634670354,0.605196182,0.416841221,0.240916211,0.266348164,0.521524375,0.351254231,0.220076917,0.238192546,-0.600799578,-0.970452459,-1.262441172,-1.416213938,-0.793493921,-1.315688108,-1.322150026,-1.253254251,-0.740832642,-1.475252313,-1.905114535,-2.217286537,-0.709068408,-1.319547732,-1.046305645,-0.82006984,-0.703211744,-1.163265904,-0.939448201,-0.718121821,0.311006071,0.370636231,0.317491467,0.267058778,0.42839663,0.382882325,0.341668086,0.313881916,0.176757998,0.10756288,0.028538574,-0.02761787,-0.074209874,-0.131644397,-0.096552236,-0.06510785,-0.30896444,-0.311226961,-0.244733315,-0.197280911,2
distance3_naka.csv,0.342567233,0.462720025,0.238995067,0.009551316,0.864096777,0.639882663,0.437098737,0.402857336,0.617194561,0.462892377,0.399435908,0.396791781,0.311066237,0.139924633,0.029557104,0.03265959,0.001816747,-0.093589015,-0.155217989,-0.122524936,-0.241373843,-0.567996575,-0.890896249,-1.082343442,-0.614531453,-1.07924812,-1.006879355,-0.896930854,-0.5938326,-1.180590384,-1.467407592,-1.670482482,-0.577480125,-1.021761692,-0.895922659,-0.773830167,-0.556686752,-0.87676011,-0.759305721,-0.647632479,0.202942379,0.122144369,0.007310006,-0.090188323,-0.322719876,-0.371990849,-0.287755468,-0.231278226,-0.516172177,-0.648244618,-0.646176166,-0.677691801,-0.640972296,-0.653019401,-0.451441175,-0.350415295,-0.747217634,-0.696454912,-0.502429606,-0.383816929,2
distance4_naka.csv,0.35079307,0.493618097,0.346770268,0.137221039,0.605779369,0.482234558,0.367999511,0.356417305,0.299088274,0.242458722,0.253209234,0.261772299,-0.01320409,0.048126277,0.077957586,0.08258665,-0.255482761,-0.117862078,-0.095296927,-0.105538469,-0.202592824,-0.466991481,-0.711050208,-0.889466597,-0.434944259,-0.82303034,-0.754048713,-0.664171511,-0.420410581,-0.913198612,-1.174719995,-1.352872222,-0.432788409,-0.786837677,-0.694699176,-0.593551764,-0.461033915,-0.738575649,-0.660294243,-0.579941035,-0.009332476,-0.187450611,-0.317618579,-0.416871418,-0.823580907,-0.827632179,-0.625223007,-0.489746249,-0.85662194,-1.073281939,-1.04204073,-1.036599808,-0.791014051,-0.810380351,-0.558777236,-0.419469107,-0.714255735,-0.630927413,-0.413074798,-0.285076218,2
distance5_naka.csv,0.540944631,0.699432976,0.467789979,0.007473802,0.316790641,0.33244272,0.265221025,0.14844026,-0.026274759,-0.031622721,-0.027769831,-0.052191696,-0.342170221,-0.41739935,-0.19197622,-0.068889935,-0.63334907,-0.591990701,-0.390503984,-0.298046088,-0.242336126,-0.83125646,-1.299774269,-1.376908741,-0.995179354,-1.340439956,-1.135918179,-0.948052587,-0.998632059,-1.507191614,-1.835973389,-2.144460376,-0.906755743,-1.267345978,-0.929718226,-0.65686761,-0.747574093,-1.033304352,-0.779590948,-0.550694785,-0.238054603,-0.278963213,-0.31301329,-0.310097358,-0.050321612,-0.315005353,-0.494762747,-0.564542275,-0.045206711,-0.263387088,-0.397252487,-0.466383366,-0.090112295,-0.461900209,-0.479245239,-0.375084437,-0.148260224,-0.42673199,-0.363832305,-0.246886111,2
```

# ①トレーニング用のCSVファイルとテスト用の配列データを用いたSVMの実行方法
※[【Python】pythonで簡単に機械学習入門(SVM)](https://qiita.com/ryu1104/items/06a71d8f6956da889bb8)を参考

テスト用の配列データです。
```python:svm_sample.py
#評価データ
test = [[-0.30181402,-0.438666784,-0.418243643,-0.306983866,-0.280096113,-0.242419664,-0.238359856,-0.225115948,0.001199974,-0.039860178,-0.06420965,-0.048321616,0.243574254,0.18521859,0.08773647,0.041050436,0.443562298,0.345202432,0.259105894,0.240894456,-0.323833715,-0.747166142,-1.124002454,-1.390885808,-0.839496505,-1.531766393,-1.443036115,-1.261565321,-0.802763794,-1.614120159,-2.012549769,-2.259612902,-0.733202925,-1.453312834,-1.338057222,-1.139398942,-0.649311386,-1.196066772,-1.123236582,-0.956444975,-0.000861433,-0.110502661,-0.187145251,-0.232317484,-0.580646438,-0.657396042,-0.563512625,-0.475380614,-0.596295943,-0.804362866,-0.829009856,-0.838510323,-0.538975037,-0.59635351,-0.408969775,-0.277145945,-0.470216843,-0.43816092,-0.254893789,-0.114830338]] 
```

ソースコードです。
```python:svm_sample.py
#csvファイルの読み込み
import numpy as np
from sklearn import svm

#csvファイルの読み込み
npArray = np.loadtxt("landmark_naka.csv", delimiter = ",", dtype = "float")

# 説明変数の格納
x = npArray[:, 2:65]

#目的変数の格納
y = npArray[66].ravel()

#学習手法にSVMを選択
model = svm.SVC()

#学習
model.fit(x,y)

#評価データ
test = [[-0.30181402,-0.438666784,-0.418243643,-0.306983866,-0.280096113,-0.242419664,-0.238359856,-0.225115948,0.001199974,-0.039860178,-0.06420965,-0.048321616,0.243574254,0.18521859,0.08773647,0.041050436,0.443562298,0.345202432,0.259105894,0.240894456,-0.323833715,-0.747166142,-1.124002454,-1.390885808,-0.839496505,-1.531766393,-1.443036115,-1.261565321,-0.802763794,-1.614120159,-2.012549769,-2.259612902,-0.733202925,-1.453312834,-1.338057222,-1.139398942,-0.649311386,-1.196066772,-1.123236582,-0.956444975,-0.000861433,-0.110502661,-0.187145251,-0.232317484,-0.580646438,-0.657396042,-0.563512625,-0.475380614,-0.596295943,-0.804362866,-0.829009856,-0.838510323,-0.538975037,-0.59635351,-0.408969775,-0.277145945,-0.470216843,-0.43816092,-0.254893789,-0.114830338]] 

#predict関数で、評価データの天気を予測
ans = model.predict(test)

# 検証結果の表示
if test == 0:
    print("挙手")
if test == 1:
    print("goodのポーズ")
if test == 2:
    print("中指")
if test == 3:
    print("シー")
```
|コード|説明|
|---|---|
|delimiter = ","|「,」で区切り読み込む。|

Scikit-learnでは、「svm.SVC()」を用いることでサポート・ベクター・マシン(SVM)を実装できます。
```
svm.SVC(gamma, C)
```
|パラメータ|説明|
|---|---|
|gamma|rbf, poly, sigmoidカーネル(ガウシアンカーネル)のパラメータです。(値が大きいほど境界が複雑になる)|
|C|どれだけ誤分類を許容するかのパラメータです。(値が小さいほど誤分類を許容)|
|kernel|非線形の境界線を生成するのに必要なパラメータ。カーネル関数の種類(デフォルトはrbf)。rbf(RBFカーネル), linear(線形カーネル：これを使うと単純な線形となるが処理速度は速い), poly(多項式カーネル), sigmoid(シグモイドカーネル)などを指定できる。|

実行結果です。
```
#実行結果
$ python3 svm_sample.py
挙手
```
***
# ②トレーニング用とテスト用のCSVファイルを用いてSVMを実行する方法
※[【Scikit-learn】サポートベクタマシン(SVM)で教師あり学習（分類器作成）](https://algorithm.joho.info/machine-learning/python-scikit-learn-svm-training/)を参考

テスト用のデータ(CSVファイル)を用意します。
```csv:test_naka.csv
#file_name,P1, P2, P3, P5, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14, P15, p16, p17, P18, P19, P20

test_naka.csv,-0.30181402,-0.438666784,-0.418243643,-0.306983866,-0.280096113,-0.242419664,-0.238359856,-0.225115948,0.001199974,-0.039860178,-0.06420965,-0.048321616,0.243574254,0.18521859,0.08773647,0.041050436,0.443562298,0.345202432,0.259105894,0.240894456,-0.323833715,-0.747166142,-1.124002454,-1.390885808,-0.839496505,-1.531766393,-1.443036115,-1.261565321,-0.802763794,-1.614120159,-2.012549769,-2.259612902,-0.733202925,-1.453312834,-1.338057222,-1.139398942,-0.649311386,-1.196066772,-1.123236582,-0.956444975,-0.000861433,-0.110502661,-0.187145251,-0.232317484,-0.580646438,-0.657396042,-0.563512625,-0.475380614,-0.596295943,-0.804362866,-0.829009856,-0.838510323,-0.538975037,-0.59635351,-0.408969775,-0.277145945,-0.470216843,-0.43816092,-0.254893789,-0.114830338
```

ソースコードです。
```python:svm_test.py
import pandas as pd
import numpy as np
from sklearn import svm
import joblib

# 学習用のデータを読み込み
train_data = pd.read_csv("train_naka.csv", sep=",")

# 説明変数：0_x～20_z
train_X = train_data.loc[:, 2:65].values

# 目的変数：pose
train_y = train_data[66].values

# 学習（SVM）
clf = svm.SVC(gamma=0.001, C=100.)
clf.fit(train_X, train_y)

# 学習結果を出力
joblib.dump(clf, 'train_naka.learn')

# 学習済ファイルのロード
clf2 = joblib.load('train_naka.learn')

# テスト用データの読み込み
test_data = pd.read_csv("test_naka.csv", sep=",")

# 学習結果の検証（テスト用データP1〜P20を入力）
test_X = test_data.loc[:, [2:65]].values
test_y = clf2.predict(test_X)

# 検証結果の表示
if test_y == 0:
    print("挙手")
if test_y == 1:
    print("goodのポーズ")
if test_y == 2:
    print("中指")
if test_y == 3:
    print("シー")
```
実行結果です。
```
#実行結果
$ python3 svm_test.py
挙手
```
終わり。
***
>他の記事
・[[2022年]SVMの学習セットのフォーマット(CSVファイル)](https://qiita.com/netineti512/items/2596d4fbdebd700a7aa0)
・[[2022年]MediaPipeで取得した座標データの統一化の方法[CSVファイル]](https://qiita.com/netineti512/items/fd5929361a6fdb8f629b)
・[[2022年]PythonのMediaPipeを使い、動画ファイルの手を認識し、CSVファイルに座標データを保存する方法](https://qiita.com/netineti512/items/b79ff4f878c7795b6b91)
・[[2022年]PythonのOpenCVを使って動画を録画する方法](https://qiita.com/netineti512/items/57b532d5acd29ab36e67)