混淆矩阵不支持多标签指示符

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_testy_pred,你应该得到你所期望的.

confusion_matrix(
    y_test.values.argmax(axis=1), predictions.argmax(axis=1))

array([[1, 0],
       [0, 2]])
Run Code Online (Sandbox Code Playgroud)


Jos*_*ard 7

混淆矩阵采用标签矢量(不是单热编码).你应该跑

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)

图像


She*_*zod 5

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)