Kha*_*775 17 python numpy classification scikit-learn
multilabel-indicator is not supported
是我尝试运行时收到的错误消息:
confusion_matrix(y_test, predictions)
y_test
是一个DataFrame
形状:
Horse | Dog | Cat
1 0 0
0 1 0
0 1 0
... ... ...
Run Code Online (Sandbox Code Playgroud)
predictions
是一个numpy array
:
[[1, 0, 0],
[0, 1, 0],
[0, 1, 0]]
Run Code Online (Sandbox Code Playgroud)
我已经搜索了一些错误消息,但还没找到我可以应用的东西.任何提示?
cs9*_*s95 34
不,您的输入confusion_matrix
必须是预测列表,而不是OHE(一个热编码).呼叫argmax
你的y_test
和y_pred
,你应该得到你所期望的.
confusion_matrix(
y_test.values.argmax(axis=1), predictions.argmax(axis=1))
array([[1, 0],
[0, 2]])
Run Code Online (Sandbox Code Playgroud)
混淆矩阵采用标签矢量(不是单热编码).你应该跑
confusion_matrix(y_test.values.argmax(axis=1), predictions.argmax(axis=1))
Run Code Online (Sandbox Code Playgroud)
小智 6
如果您有 numpy.ndarray 您可以尝试以下操作
import seaborn as sns
T5_lables = ['4TCM','WCM','WSCCM','IWCM','CCM']
ax= plt.subplot()
cm = confusion_matrix(np.asarray(Y_Test).argmax(axis=1), np.asarray(Y_Pred).argmax(axis=1))
sns.heatmap(cm, annot=True, fmt='g', ax=ax); #annot=True to annotate cells, ftm='g' to disable scientific notation
# labels, title and ticks
ax.set_xlabel('Predicted labels');ax.set_ylabel('True labels');
ax.set_title('Confusion Matrix');
ax.xaxis.set_ticklabels(T5_lables); ax.yaxis.set_ticklabels(T5_lables);
Run Code Online (Sandbox Code Playgroud)
from sklearn.metrics import confusion_matrix
predictions_one_hot = model.predict(test_data)
cm = confusion_matrix(labels_one_hot.argmax(axis=1), predictions_one_hot.argmax(axis=1))
print(cm)
Run Code Online (Sandbox Code Playgroud)
输出将是这样的:
[[298 2 47 15 77 3 49]
[ 14 31 2 0 5 1 2]
[ 64 5 262 22 94 38 43]
[ 16 1 20 779 15 14 34]
[ 49 0 71 33 316 7 118]
[ 14 0 42 23 5 323 9]
[ 20 1 27 32 97 13 436]]
Run Code Online (Sandbox Code Playgroud)