Memo

メモ > 技術 > プログラミング言語: Python > scikit-learn(機械学習)でアヤメの分類

■scikit-learn(機械学習)でアヤメの分類
■アヤメの分類 pandas/iris.csv at master - pandas-dev/pandas - GitHub https://github.com/pandas-dev/pandas/blob/master/pandas/tests/data/iris.csv GitHub - kujirahand/book-mlearn-gyomu: Book sample (AI Machine-learning Deep-learning) https://github.com/kujirahand/book-mlearn-gyomu
import pandas as pd from sklearn.model_selection import train_test_split from sklearn.svm import SVC from sklearn.metrics import accuracy_score # アヤメデータの読み込み iris_data = pd.read_csv("iris.csv", encoding="utf-8") # アヤメデータをラベルと入力データに分離する y = iris_data.loc[:,"Name"] x = iris_data.loc[:,["SepalLength","SepalWidth","PetalLength","PetalWidth"]] # 学習用とテスト用に分離する x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.2, train_size = 0.8, shuffle = True) # 学習する clf = SVC() clf.fit(x_train, y_train) # 評価する y_pred = clf.predict(x_test) print("正解率 = " , accuracy_score(y_test, y_pred))
以下、勉強メモ train_test_split関数でデータ分割 - PyQ 1.0 ドキュメント https://docs.pyq.jp/python/machine_learning/tips/train_test_split.html iris.csv
SepalLength,SepalWidth,PetalLength,PetalWidth,Name 5.1,3.5,1.4,0.2,Iris-setosa 4.9,3.0,1.4,0.2,Iris-setosa 7.0,3.2,4.7,1.4,Iris-versicolor 6.4,3.2,4.5,1.5,Iris-versicolor 6.3,3.3,6.0,2.5,Iris-virginica 5.8,2.7,5.1,1.9,Iris-virginica
iris.py
import pandas as pd from sklearn.model_selection import train_test_split from sklearn.svm import SVC from sklearn.metrics import accuracy_score # アヤメデータの読み込み iris_data = pd.read_csv("iris.csv", encoding="utf-8") print("iris_test.csv") print(iris_data) # アヤメデータをラベルと入力データに分離する y = iris_data.loc[:,"Name"] x = iris_data.loc[:,["SepalLength","SepalWidth","PetalLength","PetalWidth"]] print("y") print(y) print("x") print(x) # 学習用とテスト用に分離する x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.2, train_size = 0.8, shuffle = True) print("x_train") print(x_train) print("x_test") print(x_test) print("y_train") print(y_train) print("y_test") print(y_test)
以下のとおり実行
$ python3 iris_test.py iris_test.csv SepalLength SepalWidth PetalLength PetalWidth Name 0 5.1 3.5 1.4 0.2 Iris-setosa 1 4.9 3.0 1.4 0.2 Iris-setosa 2 7.0 3.2 4.7 1.4 Iris-versicolor 3 6.4 3.2 4.5 1.5 Iris-versicolor 4 6.3 3.3 6.0 2.5 Iris-virginica 5 5.8 2.7 5.1 1.9 Iris-virginica y 0 Iris-setosa 1 Iris-setosa 2 Iris-versicolor 3 Iris-versicolor 4 Iris-virginica 5 Iris-virginica Name: Name, dtype: object x SepalLength SepalWidth PetalLength PetalWidth 0 5.1 3.5 1.4 0.2 1 4.9 3.0 1.4 0.2 2 7.0 3.2 4.7 1.4 3 6.4 3.2 4.5 1.5 4 6.3 3.3 6.0 2.5 5 5.8 2.7 5.1 1.9 x_train SepalLength SepalWidth PetalLength PetalWidth 5 5.8 2.7 5.1 1.9 2 7.0 3.2 4.7 1.4 0 5.1 3.5 1.4 0.2 3 6.4 3.2 4.5 1.5 x_test SepalLength SepalWidth PetalLength PetalWidth 1 4.9 3.0 1.4 0.2 4 6.3 3.3 6.0 2.5 y_train 5 Iris-virginica 2 Iris-versicolor 0 Iris-setosa 3 Iris-versicolor Name: Name, dtype: object y_test 1 Iris-setosa 4 Iris-virginica Name: Name, dtype: object

Advertisement