質問編集履歴
2
現在の状況
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
###前提・実現したいこと
|
2
|
-
|
2
|
+
Digtisデータで混合行列を作成するコードようにmnistデータでも同じように混合行列を作成したいのですがどのようにしたらいいですか。
|
3
|
-
|
3
|
+
|
4
4
|
原因と改善策を教えてください。
|
5
5
|
|
6
6
|
また、Digtisデータで混同行列は以下のようにできたのに今回はエラーが出るのでしょうか。
|
1
書式な改善と現在のコードの状況
title
CHANGED
File without changes
|
body
CHANGED
@@ -3,29 +3,32 @@
|
|
3
3
|
容量が大きいためかうまくいきません。
|
4
4
|
原因と改善策を教えてください。
|
5
5
|
|
6
|
+
また、Digtisデータで混同行列は以下のようにできたのに今回はエラーが出るのでしょうか。
|
7
|
+
|
8
|
+
|
6
9
|
###発生している問題・エラーメッセージ
|
7
10
|
|
8
11
|
```python
|
9
12
|
---------------------------------------------------------------------------
|
10
13
|
ValueError Traceback (most recent call last)
|
11
|
-
<ipython-input-5-
|
14
|
+
<ipython-input-5-f6ad643389d7> in <module>()
|
12
|
-
|
15
|
+
29
|
13
|
-
|
16
|
+
30 if __name__ == '__main__':
|
14
|
-
--->
|
17
|
+
---> 31 main()
|
15
18
|
|
16
|
-
<ipython-input-5-
|
19
|
+
<ipython-input-5-f6ad643389d7> in main()
|
17
|
-
|
20
|
+
21
|
18
|
-
|
21
|
+
22 # 正解率を計算
|
19
|
-
--->
|
22
|
+
---> 23 score = accuracy_score(test_dataY, predicted_labels)
|
20
|
-
|
23
|
+
24 print("正解率:{}".format(score))
|
21
|
-
|
24
|
+
25
|
22
25
|
|
23
|
-
~\Anaconda3\lib\site-packages\sklearn\metrics\classification.py in
|
26
|
+
~\Anaconda3\lib\site-packages\sklearn\metrics\classification.py in accuracy_score(y_true, y_pred, normalize, sample_weight)
|
24
|
-
|
27
|
+
174
|
25
|
-
|
28
|
+
175 # Compute accuracy for each possible representation
|
26
|
-
-->
|
29
|
+
--> 176 y_type, y_true, y_pred = _check_targets(y_true, y_pred)
|
27
|
-
|
30
|
+
177 if y_type.startswith('multilabel'):
|
28
|
-
|
31
|
+
178 differing_labels = count_nonzero(y_true - y_pred, axis=1)
|
29
32
|
|
30
33
|
~\Anaconda3\lib\site-packages\sklearn\metrics\classification.py in _check_targets(y_true, y_pred)
|
31
34
|
69 y_pred : array or indicator matrix
|
@@ -41,11 +44,48 @@
|
|
41
44
|
205
|
42
45
|
206
|
43
46
|
|
44
|
-
ValueError: Found input variables with inconsistent numbers of samples: [21000,
|
47
|
+
ValueError: Found input variables with inconsistent numbers of samples: [21000, 1]
|
45
48
|
|
46
49
|
```
|
50
|
+
###Digitsデータでのソースコード
|
51
|
+
```python
|
52
|
+
from sklearn import datasets
|
53
|
+
from sklearn.model_selection import LeaveOneOut
|
54
|
+
from sklearn.metrics import accuracy_score, confusion_matrix
|
55
|
+
from sklearn.neighbors import KNeighborsClassifier
|
47
56
|
|
57
|
+
def main():
|
58
|
+
dataset = datasets.load_digits()
|
59
|
+
|
60
|
+
features = dataset.data
|
61
|
+
targets = dataset.target
|
62
|
+
|
63
|
+
predicted_labels = []
|
64
|
+
|
65
|
+
loo = LeaveOneOut()
|
66
|
+
for train, test in loo.split(features):
|
67
|
+
train_data = features[train]
|
68
|
+
target_data = targets[train]
|
69
|
+
|
70
|
+
model = KNeighborsClassifier(n_neighbors=1, metric='euclidean')
|
71
|
+
model.fit(train_data, target_data)
|
72
|
+
|
73
|
+
predicted_label = model.predict(features[test])
|
74
|
+
predicted_labels.append(predicted_label)
|
75
|
+
|
76
|
+
print(predicted_labels)
|
77
|
+
score = accuracy_score(targets, predicted_labels)
|
78
|
+
print(score)
|
79
|
+
|
80
|
+
# 混合行列を表示
|
81
|
+
cm = confusion_matrix(targets, predicted_labels)
|
82
|
+
print(cm)
|
83
|
+
|
84
|
+
|
85
|
+
if __name__ == '__main__':
|
86
|
+
main()
|
87
|
+
```
|
48
|
-
###
|
88
|
+
###mnistデータでのソースコード
|
49
89
|
```python
|
50
90
|
from collections import Counter
|
51
91
|
from matplotlib import pyplot as plt
|
@@ -66,11 +106,9 @@
|
|
66
106
|
features = mnist.data
|
67
107
|
targets = mnist.target
|
68
108
|
|
109
|
+
#データを分割
|
69
110
|
train_dataX, test_dataX, train_dataY, test_dataY = model_selection.train_test_split(features,targets,test_size=0.3)
|
70
111
|
|
71
|
-
|
72
|
-
# 使う近傍数ごとに正解率&各経過時間を計算
|
73
|
-
accuracy_scores = []
|
74
112
|
predicted_labels = []
|
75
113
|
|
76
114
|
# モデルを学習
|
@@ -79,15 +117,18 @@
|
|
79
117
|
|
80
118
|
# 一つだけ取り除いたテストデータを識別
|
81
119
|
predicted_label = model.predict(test_dataX)
|
120
|
+
predicted_labels.append(predicted_label)
|
121
|
+
|
122
|
+
## print(predicted_labels)
|
82
123
|
|
83
124
|
# 正解率を計算
|
84
|
-
score = accuracy_score(test_dataY,
|
125
|
+
score = accuracy_score(test_dataY, predicted_labels)
|
85
126
|
print("正解率:{}".format(score))
|
86
|
-
|
127
|
+
|
87
128
|
# 混合行列を表示
|
88
129
|
cm = confusion_matrix(test_dataY, predicted_labels)
|
89
130
|
print(cm)
|
90
|
-
|
131
|
+
|
91
132
|
if __name__ == '__main__':
|
92
133
|
main()
|
93
134
|
```
|