为什么我的混淆矩阵只返回一个数字? [英] Why is my confusion matrix returning only one number?

查看:64
本文介绍了为什么我的混淆矩阵只返回一个数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个二元分类.每当我的预测等于基本事实时,我就会发现 sklearn.metrics.confusion_matrix 返回单个值.没有问题吗?

I'm doing a binary classification. Whenever my prediction equals the ground truth, I find sklearn.metrics.confusion_matrix to return a single value. Isn't there a problem?

from sklearn.metrics import confusion_matrix
print(confusion_matrix([True, True], [True, True])
# [[2]]

我希望是这样的:

[[2 0]
 [0 0]]

推荐答案

解决方案: 如果你想要得到想要的输出,你应该填写 labels=[True, False]:

Solution: Should you want to have the desired output, you should fill-in labels=[True, False]:

from sklearn.metrics import confusion_matrix

cm = confusion_matrix(y_true=[True, True], y_pred=[True, True], labels=[True, False])
print(cm)

# [[2 0]
#  [0 0]]

说明:来自 docs 的输出混淆矩阵(y_true,y_pred)是:

C:形状的 ndarray (n_classes, n_classes)

C: ndarray of shape (n_classes, n_classes)

变量 n_classes 是:

  • 猜测为 y_truey_pred
  • 中唯一值的数量
  • 取自可选参数的长度labels

在你的例子中,因为你没有填写labels,变量n_classes是根据[True, True] 为 1.因此结果.

In your case, because you did not fill in labels, the variable n_classes is guessed from the number of unique values in [True, True] which is 1. Hence the result.

这篇关于为什么我的混淆矩阵只返回一个数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆