質問編集履歴

1

全コードとエラーメッセージ、print結果を記載しました。

2019/11/08 01:24

投稿

python_2019
python_2019

スコア68

test CHANGED
File without changes
test CHANGED
@@ -10,11 +10,203 @@
10
10
 
11
11
 
12
12
 
13
+ (以下、全てのコードとエラーメッセージです。)
14
+
15
+
16
+
13
17
  ```ここに言語を入力
14
18
 
19
+ import keras
20
+
21
+ from keras.datasets import mnist
22
+
23
+ from keras.models import Sequential
24
+
25
+ from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D
26
+
27
+ #from keras.optimizers import RMSprop
28
+
29
+ #from keras.utils import np_utils
30
+
31
+ #from sklearn.datasets import fetch_mldata
32
+
33
+
34
+
35
+ # データを高速かつ効率的に使えるPandasをインポート
36
+
37
+ import pandas as pd
38
+
39
+
40
+
41
+ # 数値計算を効率的に行うNumpyをインポート
42
+
43
+ import numpy as np
44
+
45
+
46
+
47
+ # グラフが簡単に描写できるMatplotlibをインポート
48
+
49
+ import matplotlib
50
+
51
+ import matplotlib.pyplot as plt
52
+
53
+
54
+
55
+ # MNISTデータを読込む
56
+
57
+ (x_train, y_train), (x_test, y_test) = mnist.load_data()
58
+
59
+
60
+
61
+ # MNISTデータを加工する
62
+
63
+
64
+
65
+ # 1次元に加工する場合(畳み込みは2次元)(デフォルトは2次元)
66
+
67
+ #x_train = x_train.reshape(60000, 784)
68
+
69
+ #x_test = x_test.reshape(10000, 784)
70
+
71
+
72
+
73
+ # データを float 型に変換
74
+
75
+ x_train = x_train.astype('float32')
76
+
77
+ x_test = x_test.astype('float32')
78
+
79
+
80
+
81
+ # 0〜255 までの範囲のデータを 0〜1 までの範囲に変更(正規化)
82
+
83
+ x_train /= 255
84
+
85
+ x_test /= 255
86
+
87
+
88
+
89
+ # 分類するクラス数
90
+
91
+ y_train = keras.utils.to_categorical(y_train, 10)
92
+
93
+ y_test = keras.utils.to_categorical(y_test, 10)
94
+
95
+
96
+
97
+ # 後の評価で使用?
98
+
99
+ y_test_backup = y_test
100
+
101
+
102
+
103
+
104
+
105
+ # 両方のサイズを確認
106
+
107
+
108
+
109
+ print("x_train.shape(学習用の画像データ) : ", x_train.shape)
110
+
111
+ print("y_train_shape(学習用の正解データ) : ", y_train.shape)
112
+
113
+ print("x_test.shape(テスト用の画像データ) : ", x_test.shape)
114
+
115
+ print("y_test.shape(テスト用の正解データ) : ", y_test.shape)
116
+
117
+
118
+
119
+ x_train.shape(学習用の画像データ) : (60000, 28, 28)
120
+
121
+ y_train_shape(学習用の正解データ) : (60000, 10)
122
+
123
+ x_test.shape(テスト用の画像データ) : (10000, 28, 28)
124
+
125
+ y_test.shape(テスト用の正解データ) : (10000, 10)
126
+
127
+
128
+
129
+ # 28x28x1のサイズへ変換
130
+
131
+ x_train = x_train.reshape(x_train.shape[0], 28, 28,1)
132
+
133
+ x_test = x_test.reshape(x_test.shape[0], 28, 28,1)
134
+
135
+
136
+
137
+ # モデルの宣言
138
+
139
+ model = Sequential()
140
+
141
+
142
+
143
+ # 先に作成したmodelへレイヤーを追加
144
+
145
+ model.add(Conv2D(32, kernel_size=(3, 3),
146
+
147
+ activation='relu',
148
+
149
+ input_shape=(28,28,1)))
150
+
151
+ model.add(MaxPooling2D(pool_size=(2, 2)))
152
+
153
+ model.add(Dropout(0.25))
154
+
155
+ model.add(Flatten())
156
+
157
+ model.add(Dense(128, activation='relu'))
158
+
159
+ model.add(Dense(10, activation='softmax'))
160
+
161
+
162
+
163
+ # Learnig Processの設定
164
+
165
+ model.compile(loss='categorical_crossentropy',
166
+
167
+ optimizer='sgd',
168
+
169
+ metrics=['accuracy'])
170
+
171
+
172
+
173
+ # 注意 - 数分程度かかる
174
+
175
+ # モデルの訓練(エポック数)
176
+
177
+ model.fit(x_train, y_train, epochs=1)
178
+
179
+
180
+
181
+
182
+
183
+ Epoch 1/1
184
+
185
+ 60000/60000 [==============================] - 19s 318us/step - loss: 0.4984 - acc: 0.8590
186
+
187
+ <keras.callbacks.History at 0x1fa3aaad5c0>
188
+
189
+
190
+
191
+ # テストデータを使ってモデルの評価
192
+
193
+ loss_and_metrics = model.evaluate(x_test, y_test, batch_size=128)
194
+
195
+ print(loss_and_metrics)
196
+
197
+
198
+
199
+ 10000/10000 [==============================] - 1s 54us/step
200
+
201
+ [0.2483551863193512, 0.9253]
202
+
203
+
204
+
15
205
  # 評価の実行
16
206
 
207
+
208
+
17
- from sklearn.metrics import accuracy_score
209
+ from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
18
210
 
19
211
  predict_classes = model.predict_classes(x_test)
20
212
 
@@ -24,6 +216,16 @@
24
216
 
25
217
  print('正確度(accuracy):', score[1])
26
218
 
219
+ print(' ')
220
+
221
+ #print('精度(正確度):{:.3f}'.format(accuracy_score(y_test, predict_classes)))
222
+
223
+ #print('適合率:{:.3f}'.format(precision_score(y_test, predict_classes)))
224
+
225
+ #print('再現率:{:.3f}'.format(recall_score(y_test, predict_classes)))
226
+
227
+ #print('f-1値:{:.3f}'.format(f1_score(y_test, predict_classes)))
228
+
27
229
 
28
230
 
29
231
  # 混同行列(Confusion Matrix)
@@ -32,62 +234,94 @@
32
234
 
33
235
  from sklearn.metrics import confusion_matrix
34
236
 
237
+
238
+
35
239
  print(confusion_matrix(y_test, predict_classes))
36
240
 
241
+
242
+
243
+ 10000/10000 [==============================] - 1s 60us/step
244
+
245
+ 正確度(accuracy): 0.9253
246
+
247
+  
248
+
249
+  
250
+
251
+ ---------------------------------------------------------------------------
252
+
253
+ ValueError Traceback (most recent call last)
254
+
255
+ <ipython-input-7-0c069648309b> in <module>
256
+
257
+ 16 from sklearn.metrics import confusion_matrix
258
+
259
+ 17
260
+
261
+ ---> 18 print(confusion_matrix(y_test, predict_classes))
262
+
263
+
264
+
265
+ C:\python\anaconda\pgm\lib\site-packages\sklearn\metrics\classification.py in confusion_matrix(y_true, y_pred, labels, sample_weight)
266
+
267
+ 251
268
+
269
+ 252 """
270
+
271
+ --> 253 y_type, y_true, y_pred = _check_targets(y_true, y_pred)
272
+
273
+ 254 if y_type not in ("binary", "multiclass"):
274
+
275
+ 255 raise ValueError("%s is not supported" % y_type)
276
+
277
+
278
+
279
+ C:\python\anaconda\pgm\lib\site-packages\sklearn\metrics\classification.py in _check_targets(y_true, y_pred)
280
+
281
+ 79 if len(y_type) > 1:
282
+
283
+ 80 raise ValueError("Classification metrics can't handle a mix of {0} "
284
+
285
+ ---> 81 "and {1} targets".format(type_true, type_pred))
286
+
287
+ 82
288
+
289
+ 83 # We can't have more than one value on y_type => The set is no more needed
290
+
291
+
292
+
293
+ ValueError: Classification metrics can't handle a mix of multilabel-indicator and multiclass targets
294
+
295
+
296
+
37
297
  ```
38
298
 
39
299
 
40
300
 
301
+ 以下、printの結果です。
302
+
41
303
  ```ここに言語を入力
42
304
 
43
- 10000/10000 [==============================] - 1s 89us/step
44
-
45
- 正確度(accuracy): 0.9741
305
+ print(y_test)
46
-
47
-
48
-
49
- ---------------------------------------------------------------------------
306
+
50
-
51
- ValueError Traceback (most recent call last)
52
-
53
- <ipython-input-49-2f493ca036b8> in <module>
54
-
55
- 14 print(' ')
307
+ print(predict_classes)
56
-
57
- 15 from sklearn.metrics import confusion_matrix
308
+
58
-
59
- ---> 16 print(confusion_matrix(y_test, predict_classes))
309
+
60
-
61
-
62
-
63
- C:\python\anaconda\pgm\lib\site-packages\sklearn\metrics\classification.py in confusion_matrix(y_true, y_pred, labels, sample_weight)
310
+
64
-
65
- 251
66
-
67
- 252 """
311
+ [[0. 0. 0. ... 1. 0. 0.]
68
-
312
+
69
- --> 253 y_type, y_true, y_pred = _check_targets(y_true, y_pred)
313
+ [0. 0. 1. ... 0. 0. 0.]
70
-
314
+
71
- 254 if y_type not in ("binary", "multiclass"):
315
+ [0. 1. 0. ... 0. 0. 0.]
316
+
72
-
317
+ ...
318
+
73
- 255 raise ValueError("%s is not supported" % y_type)
319
+ [0. 0. 0. ... 0. 0. 0.]
74
-
75
-
76
-
320
+
77
- C:\python\anaconda\pgm\lib\site-packages\sklearn\metrics\classification.py in _check_targets(y_true, y_pred)
321
+ [0. 0. 0. ... 0. 0. 0.]
78
-
322
+
79
- 79 if len(y_type) > 1:
323
+ [0. 0. 0. ... 0. 0. 0.]]
80
-
81
- 80 raise ValueError("Classification metrics can't handle a mix of {0} "
324
+
82
-
83
- ---> 81 "and {1} targets".format(type_true, type_pred))
84
-
85
- 82
86
-
87
- 83 # We can't have more than one value on y_type => The set is no more needed
325
+ [7 2 1 ... 4 5 6]
88
-
89
-
90
-
91
- ValueError: Classification metrics can't handle a mix of multilabel-indicator and multiclass targets
92
326
 
93
327
  ```