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