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

質問編集履歴

2

現在の状況

2017/12/11 14:47

投稿

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

1

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

2017/12/11 14:46

投稿

退会済みユーザー
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-8b4965474dfa> in <module>()
14
+ <ipython-input-5-f6ad643389d7> in <module>()
12
- 28
15
+ 29
13
- 29 if __name__ == '__main__':
16
+ 30 if __name__ == '__main__':
14
- ---> 30 main()
17
+ ---> 31 main()
15
18
 
16
- <ipython-input-5-8b4965474dfa> in main()
19
+ <ipython-input-5-f6ad643389d7> in main()
17
- 24
20
+ 21
18
- 25 # 混合行列表示
21
+ 22 # 正解率計算
19
- ---> 26 cm = confusion_matrix(test_dataY, predicted_labels)
22
+ ---> 23 score = accuracy_score(test_dataY, predicted_labels)
20
- 27 print(cm)
23
+ 24 print("正解率:{}".format(score))
21
- 28
24
+ 25
22
25
 
23
- ~\Anaconda3\lib\site-packages\sklearn\metrics\classification.py in confusion_matrix(y_true, y_pred, labels, sample_weight)
26
+ ~\Anaconda3\lib\site-packages\sklearn\metrics\classification.py in accuracy_score(y_true, y_pred, normalize, sample_weight)
24
- 248
27
+ 174
25
- 249 """
28
+ 175 # Compute accuracy for each possible representation
26
- --> 250 y_type, y_true, y_pred = _check_targets(y_true, y_pred)
29
+ --> 176 y_type, y_true, y_pred = _check_targets(y_true, y_pred)
27
- 251 if y_type not in ("binary", "multiclass"):
30
+ 177 if y_type.startswith('multilabel'):
28
- 252 raise ValueError("%s is not supported" % y_type)
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, 0]
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, predicted_label)
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
  ```