Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么我的混淆矩阵只返回一个数字?_Python_Scikit Learn_Confusion Matrix - Fatal编程技术网

Python 为什么我的混淆矩阵只返回一个数字?

Python 为什么我的混淆矩阵只返回一个数字?,python,scikit-learn,confusion-matrix,Python,Scikit Learn,Confusion Matrix,我在做二元分类。每当我的预测等于基本事实时,我都会找到sklearn.metrics.conflusion\u matrix以返回单个值。没有问题吗 from sklearn.metrics import confusion_matrix print(confusion_matrix([True, True], [True, True]) # [[2]] 我希望有这样的情况: [[2 0] [0 0]] 解决方案:如果您想要获得所需的输出,您应该填写标签=[True,False]: fro

我在做二元分类。每当我的预测等于基本事实时,我都会找到
sklearn.metrics.conflusion\u matrix
以返回单个值。没有问题吗

from sklearn.metrics import confusion_matrix
print(confusion_matrix([True, True], [True, True])
# [[2]]
我希望有这样的情况:

[[2 0]
 [0 0]]

解决方案:如果您想要获得所需的输出,您应该填写
标签=[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]]
说明: 从中,
混淆矩阵(y\u true,y\u pred)
的输出为:

C:形状的排列(n_类,n_类)

变量
n_classes
为:

  • 猜测为
    y\u true
    y\u pred
  • 取自可选参数的长度
    标签

在您的情况下,因为您没有填写
标签
,所以变量
n_classes
是根据
[True,True]
中的唯一值的数量猜测出来的,即1。结果就是这样。

解决方案:如果您想要获得所需的输出,您应该填写
标签=[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]]
说明: 从中,
混淆矩阵(y\u true,y\u pred)
的输出为:

C:形状的排列(n_类,n_类)

变量
n_classes
为:

  • 猜测为
    y\u true
    y\u pred
  • 取自可选参数的长度
    标签

在您的情况下,因为您没有填写
标签
,所以变量
n_classes
是根据
[True,True]
中的唯一值的数量猜测出来的,即1。结果就是这样。

因为您没有指定标签,所以得到了1个数字。我认为这段代码将帮助你获得真正的正面,真正的负面,进动,回忆,f1分数等

代码

from sklearn import metrics

A     = [True, True]
B     = [True, True]
label = [True, False] 


# True positives and False Negatives
cm = metrics.confusion_matrix(y_true=A, y_pred=B, labels=label)
print(cm)

# Report (f1-score, precesion, recall, support)
report = metrics.classification_report([True, False], [False, True], digits=3,output_dict=True)
df = pd.DataFrame(report).transpose()
df
结果

[[2 0]
 [0 0]]

由于未指定标签,您将获得1个数字。我认为这段代码将帮助你获得真正的正面,真正的负面,进动,回忆,f1分数等

代码

from sklearn import metrics

A     = [True, True]
B     = [True, True]
label = [True, False] 


# True positives and False Negatives
cm = metrics.confusion_matrix(y_true=A, y_pred=B, labels=label)
print(cm)

# Report (f1-score, precesion, recall, support)
report = metrics.classification_report([True, False], [False, True], digits=3,output_dict=True)
df = pd.DataFrame(report).transpose()
df
结果

[[2 0]
 [0 0]]

这是我先前发布的答案的明显重复,显然我在那里也投了反对票。尽可能不公平?耶!我接受了你的答案,并对其进行了修改,以获取更多信息。我投你一票!但你所补充的并不是问题所要求的。这是我先前发布的答案的明显重复,显然我在那里也投了反对票。尽可能不公平?耶!我接受了你的答案,并对其进行了修改,以获取更多信息。我投你一票!但你补充的并不是问题所要求的。