scikit加权F1分数的计算和使用

6

我有一个关于sklearn.metrics.f1_score中加权平均的问题。

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

Calculate metrics for each label, and find their average, weighted by support (the number of true instances for each label). This alters ‘macro’ to account for label imbalance; it can result in an F-score that is not between precision and recall.

首先,如果有任何参考资料支持使用加权F1值,我只是好奇在哪些情况下应该使用加权F1值。

其次,我听说加权F1值已经被弃用了,这是真的吗?

第三,加权F1值实际上是如何计算的,例如:

{
    "0": {
        "TP": 2,
        "FP": 1,
        "FN": 0,
        "F1": 0.8
    },
    "1": {
        "TP": 0,
        "FP": 2,
        "FN": 2,
        "F1": -1
    },
    "2": {
        "TP": 1,
        "FP": 1,
        "FN": 2,
        "F1": 0.4
    }
}

如何计算上述示例的加权 F1 值。我认为应该像这样计算:(0.8*2/3 + 0.4*1/3)/3,但是我错了。
1个回答

10

首先,如果有任何参考资料可以证明使用加权 F1 分数的合理性,我很好奇在哪些情况下应该使用加权 F1。

我没有任何参考资料,但如果您对多标签分类感兴趣,并关心所有类别的准确率/召回率,则使用加权 F1 分数是适当的。如果您进行的是二元分类,只关心正样本,则可能不适用。

其次,我听说加权 F1 已被弃用,这是真的吗?

不,加权 F1 本身并没有被弃用。仅在 v0.16 中弃用了函数接口的某些方面,然后只是为了在以前不明确的情况下更加明确。(历史讨论请参见 Github 上的 链接,或查看 源代码 并搜索“弃用”以获取详细信息。)

第三,加权 F1 实际上如何计算?

根据 f1_score 的文档:

``'weighted'``:
  Calculate metrics for each label, and find their average, weighted
  by support (the number of true instances for each label). This
  alters 'macro' to account for label imbalance; it can result in an
  F-score that is not between precision and recall.
因此,平均值受到“支持度”的加权影响,这是具有给定标签的样本数。由于您上面的示例数据未包含支持度,因此无法从您列出的信息计算加权f1分数。

谢谢您提供这么详细的答案。只有一个问题:如果支持是每个标签的真实实例数量,那么我们是否可以通过为每个标签添加“TP + FN”来计算它呢? - arranjdavis

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接