【问题标题】:Computing F1 Score using sklearn使用 sklearn 计算 F1 分数
【发布时间】:2017-05-07 18:29:34
【问题描述】:

我试图弄清楚为什么 F1 分数是 sklearn 中的分数。我知道它的计算方式是:

F1 = 2 * (precision * recall) / (precision + recall)

我的代码:

from sklearn.metrics import f1_score, precision_score, recall_score
...
fmeasure1 = f1_score(true_output, predicted_output, average="macro")
fmeasure2 = f1_score(true_output, predicted_output, average="micro")

precision = precision_score(true_output, predicted_output, average="macro")
recall = recall_score(true_output, predicted_output, average="macro")

print 2*(precision*recall)/(precision + recall), fmeasure1, fmeasure2

我的数据得到的值是:

0.785744255639 0.769527615775 0.984532095901

我不明白为什么这三个值彼此不同。我已经尝试阅读文档here,但我仍然很迷茫。

我的数据集是多类的,本质上是高度不平衡的。这里的哪个值是“正确”值,并且通过扩展,我应该使用平均值参数(即无、微观、宏观、重量)中的哪个?

谢谢,任何见解都非常有价值。

【问题讨论】:

    标签: python scikit-learn


    【解决方案1】:

    看返回值:

    Returns:    
    f1_score : float or array of float, shape = [n_unique_labels]
    F1 score of the positive class in binary classification or weighted average of the F1 scores of each class for the multiclass task.
    

    每个值都是该特定类别的 F1 分数,因此可以使用不同的分数来预测每个类别。

    关于什么是最好的分数。

    best value at 1 and worst score at 0.[ \[From documentation\]][1]
    

    附带说明,如果您正在处理高度不平衡的数据集,您应该考虑研究抽样方法,或者在允许的情况下简单地从现有数据中抽取子样本。

    如果你想要平均预测average='weighted'

    sklearn.metrics.f1_score(y_true, y_pred, labels=None, pos_label=1, average='weighted')
    

    【讨论】:

    • 我不明白。每个 F1 分数都针对特定的班级?
    • 文档第六行:在多类多标签的情况下,这是每个类的F1分数的加权平均值。
    • 好的,谢谢您的意见。但是,我的问题仍然存在:为什么这些值与返回的值不同:2*(precision*recall)/(precision + recall)?
    • 来自文档:计算每个标签的指标,并找到它们的平均值,按支持度加权(每个标签的真实实例数)。这会改变“宏观”以解决标签不平衡问题;它可能导致 F-score 不在精确率和召回率之间,因此返回的值必然不同。如果这回答了您的问题,请考虑接受
    • 好的,我现在明白了。我的输出中的第一个值采用平均精度和召回率的 f 度量,而 sklearn 返回精度和召回率/每类/的平均 f 度量。谢谢。
    猜你喜欢
    • 2017-09-11
    • 2022-01-16
    • 2019-11-25
    • 2021-08-29
    • 2016-01-24
    • 2020-06-17
    • 2015-12-10
    • 2020-05-04
    • 2019-05-06
    相关资源
    最近更新 更多