我有一个关于weighted
平均值在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的计算方式,但显然我错了。
回答:
首先,如果有任何参考资料证明使用加权F1的合理性,我只是好奇在哪些情况下我应该使用加权F1。
我没有任何参考资料,但如果你对多标签分类感兴趣,并且关心所有类别的精确度/召回率,那么加权F1分数是合适的。如果你进行的是二元分类,并且只关心正样本,那么它可能不合适。
其次,我听说加权F1已被废弃,这是真的吗?
不,加权F1本身并没有被废弃。只是函数接口的某些方面在v0.16版本中被废弃,仅是为了在之前模糊的情况下使其更加明确。(历史讨论可以在github上找到,或者查看源代码并搜索页面中的”deprecated”以获取详细信息。)
第三,加权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分数。