質問編集履歴

2

現在の状況

2017/12/11 14:47

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -1,8 +1,8 @@
1
1
  ###前提・実現したいこと
2
2
 
3
- k 近傍法の実装の正解率の結果に混行列を作成したいです。
3
+ Digtisデータで混合行列作成するコードようmnistデータでも同じように行列を作成したいですがどのようにしたらいいですか
4
-
5
- 容量が大きいためかうまくいきません。
4
+
5
+
6
6
 
7
7
  原因と改善策を教えてください。
8
8
 

1

書式な改善と現在のコードの状況

2017/12/11 14:46

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -8,6 +8,12 @@
8
8
 
9
9
 
10
10
 
11
+ また、Digtisデータで混同行列は以下のようにできたのに今回はエラーが出るのでしょうか。
12
+
13
+
14
+
15
+
16
+
11
17
  ###発生している問題・エラーメッセージ
12
18
 
13
19
 
@@ -18,41 +24,41 @@
18
24
 
19
25
  ValueError Traceback (most recent call last)
20
26
 
21
- <ipython-input-5-8b4965474dfa> in <module>()
27
+ <ipython-input-5-f6ad643389d7> in <module>()
22
-
28
+
23
- 28
29
+ 29
24
-
30
+
25
- 29 if __name__ == '__main__':
31
+ 30 if __name__ == '__main__':
26
-
32
+
27
- ---> 30 main()
33
+ ---> 31 main()
28
-
29
-
30
-
34
+
35
+
36
+
31
- <ipython-input-5-8b4965474dfa> in main()
37
+ <ipython-input-5-f6ad643389d7> in main()
32
-
38
+
33
- 24
39
+ 21
34
-
40
+
35
- 25 # 混合行列表示
41
+ 22 # 正解率計算
36
-
42
+
37
- ---> 26 cm = confusion_matrix(test_dataY, predicted_labels)
43
+ ---> 23 score = accuracy_score(test_dataY, predicted_labels)
38
-
44
+
39
- 27 print(cm)
45
+ 24 print("正解率:{}".format(score))
40
-
46
+
41
- 28
47
+ 25
42
-
43
-
44
-
48
+
49
+
50
+
45
- ~\Anaconda3\lib\site-packages\sklearn\metrics\classification.py in confusion_matrix(y_true, y_pred, labels, sample_weight)
51
+ ~\Anaconda3\lib\site-packages\sklearn\metrics\classification.py in accuracy_score(y_true, y_pred, normalize, sample_weight)
46
-
52
+
47
- 248
53
+ 174
48
-
54
+
49
- 249 """
55
+ 175 # Compute accuracy for each possible representation
50
-
56
+
51
- --> 250 y_type, y_true, y_pred = _check_targets(y_true, y_pred)
57
+ --> 176 y_type, y_true, y_pred = _check_targets(y_true, y_pred)
52
-
58
+
53
- 251 if y_type not in ("binary", "multiclass"):
59
+ 177 if y_type.startswith('multilabel'):
54
-
60
+
55
- 252 raise ValueError("%s is not supported" % y_type)
61
+ 178 differing_labels = count_nonzero(y_true - y_pred, axis=1)
56
62
 
57
63
 
58
64
 
@@ -84,18 +90,92 @@
84
90
 
85
91
 
86
92
 
87
- ValueError: Found input variables with inconsistent numbers of samples: [21000, 0]
93
+ ValueError: Found input variables with inconsistent numbers of samples: [21000, 1]
88
94
 
89
95
 
90
96
 
91
97
  ```
92
98
 
93
-
94
-
95
- ###該当のソースコード
99
+ ###Digitsデータでのソースコード
96
100
 
97
101
  ```python
98
102
 
103
+ from sklearn import datasets
104
+
105
+ from sklearn.model_selection import LeaveOneOut
106
+
107
+ from sklearn.metrics import accuracy_score, confusion_matrix
108
+
109
+ from sklearn.neighbors import KNeighborsClassifier
110
+
111
+
112
+
113
+ def main():
114
+
115
+ dataset = datasets.load_digits()
116
+
117
+
118
+
119
+ features = dataset.data
120
+
121
+ targets = dataset.target
122
+
123
+
124
+
125
+ predicted_labels = []
126
+
127
+
128
+
129
+ loo = LeaveOneOut()
130
+
131
+ for train, test in loo.split(features):
132
+
133
+ train_data = features[train]
134
+
135
+ target_data = targets[train]
136
+
137
+
138
+
139
+ model = KNeighborsClassifier(n_neighbors=1, metric='euclidean')
140
+
141
+ model.fit(train_data, target_data)
142
+
143
+
144
+
145
+ predicted_label = model.predict(features[test])
146
+
147
+ predicted_labels.append(predicted_label)
148
+
149
+
150
+
151
+ print(predicted_labels)
152
+
153
+ score = accuracy_score(targets, predicted_labels)
154
+
155
+ print(score)
156
+
157
+
158
+
159
+ # 混合行列を表示
160
+
161
+ cm = confusion_matrix(targets, predicted_labels)
162
+
163
+ print(cm)
164
+
165
+
166
+
167
+
168
+
169
+ if __name__ == '__main__':
170
+
171
+ main()
172
+
173
+ ```
174
+
175
+ ###mnistデータでのソースコード
176
+
177
+ ```python
178
+
99
179
  from collections import Counter
100
180
 
101
181
  from matplotlib import pyplot as plt
@@ -134,16 +214,12 @@
134
214
 
135
215
 
136
216
 
217
+ #データを分割
218
+
137
219
  train_dataX, test_dataX, train_dataY, test_dataY = model_selection.train_test_split(features,targets,test_size=0.3)
138
220
 
139
221
 
140
222
 
141
-
142
-
143
- # 使う近傍数ごとに正解率&各経過時間を計算
144
-
145
- accuracy_scores = []
146
-
147
223
  predicted_labels = []
148
224
 
149
225
 
@@ -160,15 +236,21 @@
160
236
 
161
237
  predicted_label = model.predict(test_dataX)
162
238
 
239
+ predicted_labels.append(predicted_label)
240
+
241
+
242
+
243
+ ## print(predicted_labels)
244
+
163
245
 
164
246
 
165
247
  # 正解率を計算
166
248
 
167
- score = accuracy_score(test_dataY, predicted_label)
249
+ score = accuracy_score(test_dataY, predicted_labels)
168
250
 
169
251
  print("正解率:{}".format(score))
170
252
 
171
-
253
+
172
254
 
173
255
  # 混合行列を表示
174
256
 
@@ -176,7 +258,7 @@
176
258
 
177
259
  print(cm)
178
260
 
179
-
261
+
180
262
 
181
263
  if __name__ == '__main__':
182
264