質問編集履歴

1

書式の改善

2017/12/11 16:33

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -4,87 +4,7 @@
4
4
 
5
5
 
6
6
 
7
- ・predicted_labelsは、初期設定しているもののその後にデータが与えれておらず空の状態なので、test_dataYはエラーメッセージからすると21,000件なのでそれに見合った件数の予測結果を引数に設定する方法を教えてください。
8
-
9
-
10
-
11
- エラー
12
-
13
- ```python
14
-
15
- ---------------------------------------------------------------------------
16
-
17
- ValueError Traceback (most recent call last)
18
-
19
- <ipython-input-5-f6ad643389d7> in <module>()
7
+ ・今の状態だとK=1の時でしか混合行列がわからないのでKが1以外の時を指定して表示させるにはどうすればいいですか
20
-
21
- 29
22
-
23
- 30 if __name__ == '__main__':
24
-
25
- ---> 31 main()
26
-
27
-
28
-
29
- <ipython-input-5-f6ad643389d7> in main()
30
-
31
- 21
32
-
33
- 22 # 正解率を計算
34
-
35
- ---> 23 score = accuracy_score(test_dataY, predicted_labels)
36
-
37
- 24 print("正解率:{}".format(score))
38
-
39
- 25
40
-
41
-
42
-
43
- ~\Anaconda3\lib\site-packages\sklearn\metrics\classification.py in accuracy_score(y_true, y_pred, normalize, sample_weight)
44
-
45
- 174
46
-
47
- 175 # Compute accuracy for each possible representation
48
-
49
- --> 176 y_type, y_true, y_pred = _check_targets(y_true, y_pred)
50
-
51
- 177 if y_type.startswith('multilabel'):
52
-
53
- 178 differing_labels = count_nonzero(y_true - y_pred, axis=1)
54
-
55
-
56
-
57
- ~\Anaconda3\lib\site-packages\sklearn\metrics\classification.py in _check_targets(y_true, y_pred)
58
-
59
- 69 y_pred : array or indicator matrix
60
-
61
- 70 """
62
-
63
- ---> 71 check_consistent_length(y_true, y_pred)
64
-
65
- 72 type_true = type_of_target(y_true)
66
-
67
- 73 type_pred = type_of_target(y_pred)
68
-
69
-
70
-
71
- ~\Anaconda3\lib\site-packages\sklearn\utils\validation.py in check_consistent_length(*arrays)
72
-
73
- 202 if len(uniques) > 1:
74
-
75
- 203 raise ValueError("Found input variables with inconsistent numbers of"
76
-
77
- --> 204 " samples: %r" % [int(l) for l in lengths])
78
-
79
- 205
80
-
81
- 206
82
-
83
-
84
-
85
- ValueError: Found input variables with inconsistent numbers of samples: [21000, 1]
86
-
87
- ```
88
8
 
89
9
 
90
10
 
@@ -124,7 +44,7 @@
124
44
 
125
45
  def main():
126
46
 
127
-
47
+
128
48
 
129
49
  # 特徴データとラベルデータを取り出す
130
50
 
@@ -132,49 +52,55 @@
132
52
 
133
53
  targets = mnist.target
134
54
 
135
-
55
+
136
56
 
137
57
  #データを分割
138
58
 
139
59
  train_dataX, test_dataX, train_dataY, test_dataY = model_selection.train_test_split(features,targets,test_size=0.3)
140
60
 
61
+
141
62
 
63
+ # 検証する近傍数
142
64
 
65
+ K = 1
66
+
143
- predicted_labels = []
67
+ ks = range(1, K + 1)
144
68
 
145
69
 
146
70
 
147
- # モデルを学習
71
+ for k in ks:
148
72
 
149
- model = KNeighborsClassifier(n_neighbors=1, metric='euclidean')
73
+ predicted_labels = []
150
74
 
75
+
76
+
77
+ # モデルを学習
78
+
79
+ model = KNeighborsClassifier(n_neighbors=1, metric='euclidean')
80
+
151
- model.fit(train_dataX,train_dataY)
81
+ model.fit(train_dataX,train_dataY)
152
82
 
153
83
 
154
84
 
155
- # 一つだけ取り除いたテストデータを識別
85
+ # 一つだけ取り除いたテストデータを識別
156
86
 
157
- predicted_label = model.predict(test_dataX)
87
+ predicted_label = model.predict(test_dataX)
158
88
 
159
- predicted_labels.append(predicted_label)
89
+ predicted_labels.append(predicted_label)
160
90
 
161
-
162
-
163
- ## print(predicted_labels)
91
+
164
-
165
-
166
92
 
167
93
  # 正解率を計算
168
94
 
169
- score = accuracy_score(test_dataY, predicted_labels)
95
+ score = accuracy_score(test_dataY, predicted_label)
170
96
 
171
97
  print("正解率:{}".format(score))
172
98
 
173
-
99
+
174
100
 
175
101
  # 混合行列を表示
176
102
 
177
- cm = confusion_matrix(test_dataY, predicted_labels)
103
+ cm = confusion_matrix(test_dataY, predicted_label)
178
104
 
179
105
  print(cm)
180
106