gpt4 book ai didi

machine-learning - 为什么不能从宏精度和召回率计算宏 F1 度量?

转载 作者:行者123 更新时间:2023-12-04 08:02:21 32 4
gpt4 key购买 nike

我对通过宏精度和手动召回计算宏 f1-score 感兴趣。但结果并不相等。代码中 f1 和 f1_new 的最终公式有什么区别?

from sklearn.metrics import precision_score, recall_score, f1_score

y_true = [0, 1, 0, 1, 0 , 1, 1, 0]
y_pred = [0, 1, 0, 0, 1 , 1, 0, 0]

p = precision_score(y_true, y_pred, average='macro')
r = recall_score(y_true, y_pred, average='macro')
f1_new = (2 * p * r) / (p + r) # 0.6291390728476821

f1 = f1_score(y_true, y_pred, average='macro') # 0.6190476190476191

print(f1_new == f1)
# False

最佳答案

f1_score在 scikit-learn 中计算如下:

all_positives = 4
all_negatives = 4
true_positives = 2
true_negatives = 3

true_positive_rate = true_positives/all_positives = 2/4
true_negative_rate = true_negatives/all_negatives = 3/4

pred_positives = 3
pred_negatives = 5

positive_predicted_value = true_positives/pred_positives = 2/3
negative_predicted_value = true_negatives/pred_negatives = 3/5

f1_score_pos = 2 * true_positive_rate * positive_predicted_value / (true_positive_rate + positive_predicted_value)
= 2 * 2/4 * 2/3 / (2/4 + 2/3)

f1_score_neg = 2 * true_negative_rate * negative_predicted_value / (true_negative_rate + negative_predicted_value)
= 2 * 3/4 * 3/5 / (3/4 + 3/5)

f1 = average(f1_score_pos, f1_score_neg)
= 2/4 * 2/3 / (2/4 + 2/3) + 3/4 * 3/5 / (3/4 + 3/5)
= 0.6190476190476191
这与 documentation 中给出的定义相匹配为 'macro' Skicit-learn 的参数 f1_score :计算每个标签的指标,并找到它们的未加权平均值。此定义也适用于 precision_scorerecall_score .
您手动计算的 F1 分数如下:
precision = average(positive_predicted_value, negative_predicted_value)
= average(2/3, 3/5)
= 19/30

recall = average(true_positive_rate, true_negative_rate)
= average(2/4, 3/4)
= 5/8

f1_new = 2 * precision * recall / (precision + recall)
= 2 * 19/30 * 5/8 / (19/30 + 5/8)
= 0.6291390728476821
其实通式 F1 = 2 * (precision * recall) / (precision + recall)docs 中所述仅对 average='binary' 有效和 average='micro' ,但不适用于 average='macro'average='weighted' .从这个意义上说,正如目前在 scikit-learn 中提出的那样,该公式具有误导性,因为它表明它与所选参数无关,但事实并非如此。

关于machine-learning - 为什么不能从宏精度和召回率计算宏 F1 度量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66392243/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com