質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Keras

Kerasは、TheanoやTensorFlow/CNTK対応のラッパーライブラリです。DeepLearningの数学的部分を短いコードでネットワークとして表現することが可能。DeepLearningの最新手法を迅速に試すことができます。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

2回答

2078閲覧

keras:MNISTの畳み込みニューラルネットワークで混同行列は使えない?

python_2019

総合スコア68

Keras

Kerasは、TheanoやTensorFlow/CNTK対応のラッパーライブラリです。DeepLearningの数学的部分を短いコードでネットワークとして表現することが可能。DeepLearningの最新手法を迅速に試すことができます。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2019/11/07 06:46

編集2019/11/08 01:24

mnistのモデル評価を行うために、混同行列を取得しようとすると、エラーが発生します。
通常のディープラーニングの2分類では、ちゃんと混同行列を得ることができていたのですが、何故でしょうか?
mnistは10分類なので、混同行列はできないのでしょうか?

お詳しい方、ご指導をお願いいたします。

(以下、全てのコードとエラーメッセージです。)

import keras from keras.datasets import mnist from keras.models import Sequential from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D #from keras.optimizers import RMSprop #from keras.utils import np_utils #from sklearn.datasets import fetch_mldata # データを高速かつ効率的に使えるPandasをインポート import pandas as pd # 数値計算を効率的に行うNumpyをインポート import numpy as np # グラフが簡単に描写できるMatplotlibをインポート import matplotlib import matplotlib.pyplot as plt # MNISTデータを読込む (x_train, y_train), (x_test, y_test) = mnist.load_data() # MNISTデータを加工する # 1次元に加工する場合(畳み込みは2次元)(デフォルトは2次元) #x_train = x_train.reshape(60000, 784) #x_test = x_test.reshape(10000, 784) # データを float 型に変換 x_train = x_train.astype('float32') x_test = x_test.astype('float32') # 0〜255 までの範囲のデータを 0〜1 までの範囲に変更(正規化) x_train /= 255 x_test /= 255 # 分類するクラス数 y_train = keras.utils.to_categorical(y_train, 10) y_test = keras.utils.to_categorical(y_test, 10) # 後の評価で使用? y_test_backup = y_test # 両方のサイズを確認 print("x_train.shape(学習用の画像データ) : ", x_train.shape) print("y_train_shape(学習用の正解データ) : ", y_train.shape) print("x_test.shape(テスト用の画像データ) : ", x_test.shape) print("y_test.shape(テスト用の正解データ) : ", y_test.shape) x_train.shape(学習用の画像データ) : (60000, 28, 28) y_train_shape(学習用の正解データ) : (60000, 10) x_test.shape(テスト用の画像データ) : (10000, 28, 28) y_test.shape(テスト用の正解データ) : (10000, 10) # 28x28x1のサイズへ変換 x_train = x_train.reshape(x_train.shape[0], 28, 28,1) x_test = x_test.reshape(x_test.shape[0], 28, 28,1) # モデルの宣言 model = Sequential() # 先に作成したmodelへレイヤーを追加 model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(28,28,1))) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.25)) model.add(Flatten()) model.add(Dense(128, activation='relu')) model.add(Dense(10, activation='softmax')) # Learnig Processの設定 model.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy']) # 注意 - 数分程度かかる # モデルの訓練(エポック数) model.fit(x_train, y_train, epochs=1) Epoch 1/1 60000/60000 [==============================] - 19s 318us/step - loss: 0.4984 - acc: 0.8590 <keras.callbacks.History at 0x1fa3aaad5c0> # テストデータを使ってモデルの評価 loss_and_metrics = model.evaluate(x_test, y_test, batch_size=128) print(loss_and_metrics) 10000/10000 [==============================] - 1s 54us/step [0.2483551863193512, 0.9253] # 評価の実行 from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score predict_classes = model.predict_classes(x_test) score = model.evaluate(x_test,y_test) print('正確度(accuracy):', score[1]) print(' ') #print('精度(正確度):{:.3f}'.format(accuracy_score(y_test, predict_classes))) #print('適合率:{:.3f}'.format(precision_score(y_test, predict_classes))) #print('再現率:{:.3f}'.format(recall_score(y_test, predict_classes))) #print('f-1値:{:.3f}'.format(f1_score(y_test, predict_classes))) # 混同行列(Confusion Matrix) print(' ') from sklearn.metrics import confusion_matrix print(confusion_matrix(y_test, predict_classes)) 10000/10000 [==============================] - 1s 60us/step 正確度(accuracy): 0.9253     --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-7-0c069648309b> in <module> 16 from sklearn.metrics import confusion_matrix 17 ---> 18 print(confusion_matrix(y_test, predict_classes)) C:\python\anaconda\pgm\lib\site-packages\sklearn\metrics\classification.py in confusion_matrix(y_true, y_pred, labels, sample_weight) 251 252 """ --> 253 y_type, y_true, y_pred = _check_targets(y_true, y_pred) 254 if y_type not in ("binary", "multiclass"): 255 raise ValueError("%s is not supported" % y_type) C:\python\anaconda\pgm\lib\site-packages\sklearn\metrics\classification.py in _check_targets(y_true, y_pred) 79 if len(y_type) > 1: 80 raise ValueError("Classification metrics can't handle a mix of {0} " ---> 81 "and {1} targets".format(type_true, type_pred)) 82 83 # We can't have more than one value on y_type => The set is no more needed ValueError: Classification metrics can't handle a mix of multilabel-indicator and multiclass targets

以下、printの結果です。

print(y_test) print(predict_classes) [[0. 0. 0. ... 1. 0. 0.] [0. 0. 1. ... 0. 0. 0.] [0. 1. 0. ... 0. 0. 0.] ... [0. 0. 0. ... 0. 0. 0.] [0. 0. 0. ... 0. 0. 0.] [0. 0. 0. ... 0. 0. 0.]] [7 2 1 ... 4 5 6]

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

hayataka2049

2019/11/07 10:41

y_test, predict_classesそれぞれのオブジェクトの型、要素の型、shapeとndim、printした結果を教えて下さい。
python_2019

2019/11/08 01:25

ご連絡ありがとうございます。 全てのコードとエラーメッセージ、printした結果を追記いたしました。 お手数お掛け致しますが、ご指導方よろしくお願いいたします。
guest

回答2

0

ベストアンサー

y_testkeras.utils.to_categoricalを通す前のものにしてください。

たぶん

python

1# 分類するクラス数 2y_train = keras.utils.to_categorical(y_train, 10) 3y_test = keras.utils.to_categorical(y_test, 10) 4 5# 後の評価で使用? 6y_test_backup = y_test

ここは上下を入れ替えておいたほうが良い。

python

1y_test_backup = y_test 2 3y_train = keras.utils.to_categorical(y_train, 10) 4y_test = keras.utils.to_categorical(y_test, 10)

あとはy_test_backupの方で混同行列の作成を試してください。

投稿2019/11/08 02:30

hayataka2049

総合スコア30933

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

python_2019

2019/11/08 02:39

ご指導どおり修正しましたら、エラーがなくなりました。 とても助かりました。 どうもありがとうございました。
guest

0

エラーメッセージによるとscikitlearnのconfusion_matrixは多クラス分類に対応していないので、pandasのcrosstabを使うといいかと思います。

import pandas as pd print(pd.crosstab(y_test,predict_classes))

投稿2019/11/07 09:44

R.Shigemori

総合スコア3376

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

hayataka2049

2019/11/07 10:43 編集

> confusion_matrixは多クラス分類に対応していない それはないです。この関数で多クラス分類の混同行列自体は出るので、与えている配列に問題がありそうです。 >>> confusion_matrix([0,0,0,1,1,1,2,2,2], [0,0,1,0,2,1,1,2,2,]) array([[2, 1, 0], [1, 1, 1], [0, 1, 2]])
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問