为什么scikitlearn说F1的分数定义错误,FN大于0?

2024-09-24 02:17:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我运行一个python程序,它调用sklearn.metrics的方法来计算精度和F1分数。以下是没有预测样本时的输出:

/xxx/py2-scikit-learn/0.15.2-comp6/lib/python2.6/site-packages/sklearn/metr\
ics/metrics.py:1771: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 due to no predicted samples.
  'precision', 'predicted', average, warn_for)

/xxx/py2-scikit-learn/0.15.2-comp6/lib/python2.6/site-packages/sklearn/metr\
ics/metrics.py:1771: UndefinedMetricWarning: F-score is ill-defined and being set to 0.0 due to no predicted samples.
  'precision', 'predicted', average, warn_for)

当没有预测样本时,表示TP+FP为0,所以

  • 精度(定义为TP/(TP+FP))为0/0,未定义
  • 如果FN不为零,F1得分(定义为2TP/(2TP+FP+FN))为0。

在我的例子中,sklearn.metrics还返回0.8的准确性,并返回0。所以FN不是零。

但为什么希基勒恩说F1定义不清?

Scikilern对F1的定义是什么?


Tags: to定义sklearnscikitlearnmetricsxxxf1
2条回答

https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/metrics/classification.py

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

precision=TP/(TP+FP),正如你刚才说的,如果predictor根本不预测正类,那么precision是0。

回忆=TP/(TP+FN),如果预测因子不能预测阳性类别-TP为0-回忆为0。

现在你把0/0除以。

精确性、召回率、F1分数精确性计算

- In a given image of Dogs and Cats

  * Total Dogs - 12  D = 12
  * Total Cats - 8   C = 8

- Computer program predicts

  * Dogs - 8  
    5 are actually Dogs   T.P = 5
    3 are not             F.P = 3    
  * Cats - 12
    6 are actually Cats   T.N = 6 
    6 are not             F.N = 6

- Calculation

  * Precision = T.P / (T.P + F.P) => 5 / (5 + 3)
  * Recall    = T.P / D           => 5 / 12

  * F1 = 2 * (Precision * Recall) / (Precision + Recall)
  * F1 = 0.5

  * Accuracy = T.P + T.N / P + N
  * Accuracy = 0.55

维基百科reference

相关问题 更多 >