Python 混淆矩阵不支持多标签指标

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46953967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 17:56:52  来源:igfitidea点击:

Multilabel-indicator is not supported for confusion matrix

pythonnumpyscikit-learnclassification

提问by Khaine775

multilabel-indicator is not supportedis the error message I get, when trying to run:

multilabel-indicator is not supported是我在尝试运行时收到的错误消息:

confusion_matrix(y_test, predictions)

confusion_matrix(y_test, predictions)

y_testis a DataFramewhich is of shape:

y_test是一个DataFrame形状为:

Horse | Dog | Cat
1       0     0
0       1     0
0       1     0
...     ...   ...

predictionsis a numpy array:

predictions是一个numpy array

[[1, 0, 0],
 [0, 1, 0],
 [0, 1, 0]]

I've searched a bit for the error message, but haven't really found something I could apply. Any hints?

我已经搜索了一些错误消息,但还没有真正找到我可以应用的东西。任何提示?

回答by cs95

No, your input to confusion_matrixmust be a list of predictions, not OHEs (one hot encodings). Call argmaxon your y_testand y_pred, and you should get what you expect.

不,您的输入confusion_matrix必须是预测列表,而不是 OHE(一种热编码)。调用argmax你的y_testand y_pred,你应该得到你期望的结果。

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

array([[1, 0],
       [0, 2]])

回答by Joshua Howard

The confusion matrix takes a vector of labels (not the one-hot encoding). You should run

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

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