【问题标题】:Why the value of precision and recall is almost the same as precision and recall of the underrepresented class为什么准确率和召回率的值与代表性不足的类别的准确率和召回率几乎相同
【发布时间】:2021-02-22 21:43:24
【问题描述】:

我有一个二元分类,其中一个类的大小几乎是另一个类的 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

返回每个类的精度、recal、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个实例的类)完全相同?

【问题讨论】:

  • 你们类的标签是什么,少数类的标签是哪一个?
  • 0 和 1 是类,1 是少数类@desertnaut

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


【解决方案1】:

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

pos_label : str 或 int,默认为 1

average='binary' 并且数据是二进制时要报告的类。

平均 : *string, [None, ‘binary’ (默认), ‘micro’, ‘macro’, ‘samples’, ‘weighted’]*

'binary'

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

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

从你的展示来看,这门课似乎就是你这里所说的“二等”,结果确实和你报告的一致。

相比之下,precision_recall_fscore_support 函数,根据docs(强调我的):

计算准确率、召回率、F-measure 和支持每个类

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

【讨论】:

    【解决方案2】:

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

    Medium Article on imbalanced data

    【讨论】:

    • 可能是内部类别在您的预测变量中具有相似的差异。在不了解数据集的情况下,很难说。另一种解决方案可能是添加更多特征(预测变量)并重新训练您的模型。或者,如果您有很多特征,Scikit-learn 有一些特征选择功能可以帮助确保您为模型提供最有助于区分类别的特征。
    • 这是一个关于 sklearn 函数 precision_scorerecall_score 究竟返回什么的问题,与有或没有额外的模型的过采样、欠采样、方差或重新训练无关功能。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-12-17
    • 2019-07-28
    • 2016-06-19
    • 2020-03-26
    • 2016-01-09
    • 1970-01-01
    • 2017-08-17
    相关资源
    最近更新 更多