confusion_matrixを表示したいのですがエラーが出てしまいます。
どうすれば表示できるのかお聞きしたいです。
具体的なプログラムを教えてもらえると助かります。
最初のソースコード
from __future__ import print_function import pandas as pd from pandas import Series,DataFrame from sklearn import svm from sklearn.model_selection import train_test_split from sklearn.metrics import confusion_matrix, accuracy_score, precision_score,f1_score import numpy as np import matplotlib.pyplot as plt import keras from keras.models import Sequential from keras.layers import Dense, Dropout from keras.optimizers import Adam from keras.optimizers import SGD csvdataset = pd.read_csv("Train_Random.csv",header=0) #説明変数 x = DataFrame(csvdataset.drop("Result",axis=1)) #目的変数 y = DataFrame(csvdataset["Result"]) #説明変数・目的変数をそれぞれ訓練データ・テストデータに分割 x_train,x_test,y_train,y_test = train_test_split(x,y,test_size=0.20) y_test2 = y_test y_pred2 = x_test #データの整形 x_train = x_train.astype(np.float) x_test = x_test.astype(np.float) y_train = keras.utils.to_categorical(y_train,10) y_test = keras.utils.to_categorical(y_test,10) #ニューラルネットワークの実装① model = Sequential() model.add(Dense(60, activation='relu', input_shape=(59,))) model.add(Dropout(0.2)) model.add(Dense(60, activation='relu', input_shape=(59,))) model.add(Dropout(0.2)) model.add(Dense(60, activation='relu', input_shape=(59,))) model.add(Dropout(0.2)) model.add(Dense(10, activation='softmax')) sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True) #ニューラルネットワークの実装② model.compile(loss='categorical_crossentropy',optimizer=sgd,metrics=['accuracy']) #ニューラルネットワークの学習 history = model.fit(x_train, y_train,batch_size=1000,epochs=10,verbose=1,validation_data=(x_test, y_test)) #ニューラルネットワークの推論 score = model.evaluate(x_test,y_test,verbose=1) print("\n") print("Test 損失値:",score[0]) print("Test 正解率:",score[1]) print('confusion matrix 混合行列 =\n ', confusion_matrix(y_true=y_test2, y_pred=y_pred2))#混合行列 print('accuracy 正解率 = ', accuracy_score(y_true=y_test2, y_pred=y_pred2))#正解率 print('precision 適合率 = ', precision_score(y_true=y_test2, y_pred=y_pred2))#適合率 print('recall 再現率 = ', recall_score(y_true=y_test2, y_pred=y_pred2))#再現率 print('f1 score = ', f1_score(y_true=y_test2, y_pred=y_pred2))#f1スコア
エラーコード
ValueError Traceback (most recent call last) <ipython-input-4-ba6b4268701a> in <module> 69 print("Test 正解率:",score[1]) 70 ---> 71 tp, fn, fp, tn = confusion_matrix(y_test2, y_pred2).ravel() 72 73 print('confusion matrix 混合行列 =\n ', confusion_matrix(y_true=y_test2, y_pred=y_pred2))#混合行列 ~\Anaconda3\lib\site-packages\sklearn\metrics\_classification.py in confusion_matrix(y_true, y_pred, labels, sample_weight, normalize) 266 267 """ --> 268 y_type, y_true, y_pred = _check_targets(y_true, y_pred) 269 if y_type not in ("binary", "multiclass"): 270 raise ValueError("%s is not supported" % y_type) ~\Anaconda3\lib\site-packages\sklearn\metrics\_classification.py in _check_targets(y_true, y_pred) 88 if len(y_type) > 1: 89 raise ValueError("Classification metrics can't handle a mix of {0} " ---> 90 "and {1} targets".format(type_true, type_pred)) 91 92 # 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 binary and multiclass-multioutput targets
###修正後のy_pred2
回答でいただいたものをもとにy_pred2を変えたらエラーが変わりました
pred = model.predict(x_test, batch_size=1, verbose=0) y_pred2 = np.max(pred)
最新のエラーコード
ValueError Traceback (most recent call last) <ipython-input-13-b74e493b703b> in <module> 70 71 ---> 72 print('confusion matrix 混合行列 =\n ', confusion_matrix(y_true=y_test2, y_pred=y_pred2))#混合行列 73 print('accuracy 正解率 = ', accuracy_score(y_true=y_test2, y_pred=y_pred2))#正解率(正しく分類されたデータ数の割合) 74 print('precision 適合率 = ', precision_score(y_true=y_test2, y_pred=y_pred2))#適合率(Aに分類されたデータで実際にAであるデータ数の割合) ~\Anaconda3\lib\site-packages\sklearn\metrics\_classification.py in confusion_matrix(y_true, y_pred, labels, sample_weight, normalize) 266 267 """ --> 268 y_type, y_true, y_pred = _check_targets(y_true, y_pred) 269 if y_type not in ("binary", "multiclass"): 270 raise ValueError("%s is not supported" % y_type) ~\Anaconda3\lib\site-packages\sklearn\metrics\_classification.py in _check_targets(y_true, y_pred) 78 y_pred : array or indicator matrix 79 """ ---> 80 check_consistent_length(y_true, y_pred) 81 type_true = type_of_target(y_true) 82 type_pred = type_of_target(y_pred) ~\Anaconda3\lib\site-packages\sklearn\utils\validation.py in check_consistent_length(*arrays) 210 if len(uniques) > 1: 211 raise ValueError("Found input variables with inconsistent numbers of" --> 212 " samples: %r" % [int(l) for l in lengths]) 213 214 ValueError: Found input variables with inconsistent numbers of samples: [1, 12000]
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/01/30 07:14 編集
2020/01/30 07:13
2020/01/30 07:49
2020/01/30 08:09
2020/02/01 02:23
2020/02/01 06:58
2020/02/01 11:32
2020/02/01 11:37
2020/02/01 12:14
2020/02/01 12:21
2020/02/01 12:32
2020/02/01 12:35