Scikit-Learn 中的 Micro F1 分数 Class 失衡
Micro F1 score in Scikit-Learn with Class imbalance
我有一些 class 不平衡和一个简单的基线 class 分配器将大多数 class 分配给每个样本:
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,在这个例子中它们怎么可能相同?我知道这一定与微平均有关,但我无法理解这个。
是的,这是因为微平均。查看 documentation here 了解其计算方式:
Note that if all labels are included, “micro”-averaging in a
multiclass setting will produce precision, recall and f-score that are all
identical to accuracy.
正如您在上面的链接页面中看到的,精度和召回率都定义为:
其中 R(y, y-hat) 是:
因此在您的情况下,Recall-micro 将计算为
R = number of correct predictions / total predictions = 3/4 = 0.75
我有一些 class 不平衡和一个简单的基线 class 分配器将大多数 class 分配给每个样本:
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,在这个例子中它们怎么可能相同?我知道这一定与微平均有关,但我无法理解这个。
是的,这是因为微平均。查看 documentation here 了解其计算方式:
Note that if all labels are included, “micro”-averaging in a multiclass setting will produce precision, recall and f-score that are all identical to accuracy.
正如您在上面的链接页面中看到的,精度和召回率都定义为:
其中 R(y, y-hat) 是:
因此在您的情况下,Recall-micro 将计算为
R = number of correct predictions / total predictions = 3/4 = 0.75