2016-01-13 63 views
34

我運行一個python程序,調用sklearn.metrics的方法來計算精度和F1分數。這裏是輸出當沒有預測樣本:爲什麼scikitlearn說FN大於0的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不爲零。

但爲什麼scikilearn說F1不明確?

Scikilearn使用的F1的定義是什麼?

回答

16

準確率,召回,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