Scikit-Learn中的Micro F1分数与班级不平衡

时间:2018-11-20 14:16:51

标签: scikit-learn precision-recall

我有一些类别失衡和一个简单的基线分类器,该分类器将多数类别分配给每个样本:

from sklearn.metrics import precision_score, recall_score, confusion_matrix

y_true = [0,0,0,1]
y_pred = [0,0,0,0]
confusion_matrix(y_true, y_pred)

这产生

  

[[3,0],

     

[1,0]]

这意味着TP = 3,FP = 1,FN = 0。

到目前为止,太好了。现在,我要计算精度和召回率的微观平均值。

precision_score(y_true, y_pred, average='micro') # yields 0.75
recall_score(y_true, y_pred, average='micro') # yields 0.75

我没问题,但是为什么召回率不是1.0?在FP> 0且FN == 0的情况下,它们在本示例中如何相同?我知道这一定与微平均有关,但是我无法将其包裹在头上。

1 个答案:

答案 0 :(得分:1)

是的,这是由于微平均。请参阅documentation here以了解其计算方式:

  

请注意,如果包括所有标签,则“   多类设置将产生精度,召回率和f分数   与准确性相同

如您在上面的链接页面中所见,精度和召回率均定义为:  enter image description here

其中R(y,y-hat)是:

enter image description here

因此,在您的情况下,Recall-micro的计算方式为

R = number of correct predictions / total predictions = 3/4 = 0.75
相关问题