为什么精度和召回率的值几乎与代表性不足的类的精度和召回率相同

时间:2020-11-10 22:20:10

标签: scikit-learn precision precision-recall imbalanced-data

我有一个二进制分类,其中一个类的大小几乎是另一个类的0.1。

我正在使用sklearn创建模型并进行评估。我正在使用这两个功能:

print(precision_recall_fscore_support(y_real,y_pred))

out: 
(array([0.99549296, 0.90222222]), # precision of the first class and the second class
 array([0.98770263, 0.96208531]), # recall of the first class and the second class
 array([0.99158249, 0.93119266]), # F1 score of the first class and the second class
 array([1789,  211]))             # instances of the first class and the second class

哪个返回每个类的精度,标度,fscore和支持

print(precision_score(y_real,y_pred),recall_score(y_real,y_pred))

out:
0.90222222 , 0.96208531 # precsion and recall of the model

返回预测的精度和召回率。

为什么Precsion和Recall函数返回的实例数较少的类(这里是211个实例的类)的值完全相同?

2 个答案:

答案 0 :(得分:3)

仔细查看precision_scorerecall_score的文档,您会看到两个参数-pos_label,默认值为1average ,默认值为'binary'

pos_label: str或int,默认为1

如果average='binary'并且数据为二进制,则报告的类。

平均值:*字符串,[无,“二进制”(默认),“微”,“宏”,“样本”,“加权” ] *

'binary'

仅报告pos_label指定的类的结果。这是 仅在目标(y_{true,pred})是二进制的情况下适用。

换句话说,正如文档中清楚解释的那样,这两个函数仅分别返回一类的精度和召回率-用标签1指定的一类。

从您显示的内容来看,该类似乎是您在此处所说的“第二类”,其结果确实与您的报告相符。

相反,根据docs(强调我的意思),precision_recall_fscore_support功能:

为每个类别计算精度,召回率,F度量和支持

换句话说,这里没有什么奇怪或意外的;没有“整体”精度和召回率,并且按照定义,它们始终是每班计算机。实际上,在像这样的不平衡二进制设置中,它们通常仅针对少数类进行计算。

答案 1 :(得分:0)

这可能是由于数据集不平衡造成的。您可以尝试从代表性不足的类中进行过度采样,也可以尝试从代表性不足的类中进行过度采样,具体取决于数据中的差异水平。我在数据不平衡方面也遇到了类似的问题,这篇文章对我有帮助:

Medium Article on imbalanced data

相关问题