teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

コードを追加しました。

2019/07/31 07:53

投稿

tott-1559
tott-1559

スコア5

title CHANGED
File without changes
body CHANGED
@@ -1,3 +1,37 @@
1
1
  ### CSVの行ごとに判定結果を知りたい
2
2
 
3
- CSVで取り込んだ学習用データでモデルを作成した後に、別のCSVのデータをモデルにインプットして精度を計算するところまではできたのですが、どのデータが正解と認識され、どのデータを誤って分類したのか知る方法が分かりません。Azure Machine Learningでは行ごとに予測クラスを確認する事ができた為、pythonでもできるのではと思い調べたのですが分かりませんでした。ご存知の方、教えていただけると幸いです。よろしくお願いします。
3
+ 学習用データ(dataA.csv)でモデルを作成した後に、別のデータ(dataB.csv)をモデルにインプットして精度を計算するところまではできたのですが、どのデータが正解と認識され、どのデータを誤って分類したのか知る方法が分かりません。
4
+ Azure Machine Learningでは行ごとに予測クラスを確認する事ができた為、pythonでもできるのではと思い調べたのですが分かりませんでした。ご存知の方、教えていただけると幸いです。よろしくお願いします。
5
+
6
+ ###python3でのコード(ライブラリのインポートは省略します)
7
+ ```ここに言語を入力
8
+ #データ読み込み
9
+ df = pd.read_csv('dataA.csv')
10
+ df1 = pd.read_csv('dataB.csv')
11
+
12
+ train_x = df.drop(['Class'], axis=1)
13
+ train_y = df['Class']
14
+ (train_x, test_x ,train_y, test_y) = train_test_split(train_x, train_y, test_size = 0.3, random_state = 42)
15
+
16
+ # モデル構築ランダムフォレスト
17
+ random_forest = RandomForestClassifier(bootstrap=True, max_depth=10, n_estimators=10, random_state=42)
18
+ random_forest.fit(train_x, train_y)
19
+
20
+ # 予測値算出
21
+ y_pred = random_forest.predict(test_x)
22
+ trainaccuracy_random_forest = random_forest.score(train_x, train_y)
23
+ accuracy_random_forest = accuracy_score(test_y, y_pred)
24
+
25
+ #評価用データ
26
+ test_x1 = df1.drop(['Class'], axis=1)
27
+ test_y1 = df1['Class']
28
+ y_pred1 = random_forest.predict(test_x1)
29
+ random_forest = random_forest.score(test_x1, test_y1)
30
+
31
+ #confusion matrix
32
+ mat = confusion_matrix(test_y1, y_pred1)
33
+ sns.heatmap(mat, square=True, annot=True, cbar=False, fmt='d', cmap='RdPu')
34
+ plt.xlabel('predicted class')
35
+ plt.ylabel('true value')
36
+ ```
37
+ Confusin Matrixでの分類の可視化では、データごとの予測クラスを知る事ができない為、データごとの予測モデルを表示する方法を知りたいです。