教本で機械学習の学習をすすめているのですが、不明点があります。
ワインデータを読み込むところで
no = 0
とnoにゼロを代入する意味は何でしょうか。
python
1from sklearn import svm, metrics 2from sklearn.model_selection import train_test_split 3 4#ワインデータを読み込む 5wine_csv = [] 6with open("winequality-white.csv", "r", encoding="utf-8") as fp: 7 no = 0 # ゼロをnoに代入する意味は不明 8 for line in fp: 9 # stripで無駄な空白文字を削除 10 line = line.strip() 11 cols = line.split(";") # ;でcolsに分割 12 wine_csv.append(cols) # colsをwine_csvにデータ格納 13 14# 1行目はヘッダなので削除 15wine_csv = wine_csv[1:] 16 17# CSVの各データを数値に変換 18labels = [] 19data = [] 20for cols in wine_csv: 21 # strのままなので数値に 22 cols = list(map(lambda n:float(n), cols)) 23 # ワインのグレードを調整 24 grade = cols[11] 25 if grade == 9: grade = 8 26 if grade < 4: grade = 5 27 labels.append(grade) 28 data.append(cols[0:11]) 29 30# 訓練用データとテスト用データにわける 31data_train, data_test, label_train, label_test = \ 32train_test_split(data, labels) 33 34# SVMのアルゴリズムを利用して学習 35clf = svm.SVC() 36clf.fit(data_train, label_train) 37 38# 予測してみる(predictは予測するの意味) 39# data_testにワインの数値データを代入すると判定結果が出る 40predict = clf.predict(data_test) 41 42# 結果を出力 43ac_score = metrics.accuracy_score(label_test, predict) 44cl_report = metrics.classification_report(label_test, predict) 45print("正解率:", ac_score) 46print("レポート=\n", cl_report) 47 48 49
回答1件
あなたの回答
tips
プレビュー