Python 为什么 scikitlearn 说 F1 分数不明确,FN 大于 0?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34757653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
why does scikitlearn says F1 score is ill-defined with FN bigger than 0?
提问by Tim
I run a python program that calls sklearn.metrics
's methods to calculate precision and F1 score. Here is the output when there is no predicted sample:
我运行一个 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)
When there is no predicted sample, it means that TP+FP is 0, so
当没有预测样本时,表示TP+FP为0,所以
- precision (defined as TP/(TP+FP)) is 0/0, not defined,
- F1 score (defined as 2TP/(2TP+FP+FN)) is 0 if FN is not zero.
- 精度(定义为 TP/(TP+FP))为 0/0,未定义,
- 如果 FN 不为零,F1 分数(定义为 2TP/(2TP+FP+FN))为 0。
In my case, sklearn.metrics
also returns the accuracy as 0.8, and recall as 0. So FN is not zero.
在我的例子中,sklearn.metrics
也返回精度为 0.8,召回为 0。所以 FN 不为零。
But why does scikilearn says F1 is ill-defined?
但是为什么 scikilearn 说 F1 是不明确的?
What is the definition of F1 used by Scikilearn?
Scikilearn 使用的 F1 定义是什么?
回答by Ibraim Ganiev
https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/metrics/classification.py
https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/metrics/classification.py
F1 = 2 * (precision * recall) / (precision + recall)
F1 = 2 *(精度*召回)/(精度+召回)
precision = TP/(TP+FP) as you've just said if predictor doesn't predicts positive class at all - precision is 0.
精度 = TP/(TP+FP) 正如您刚才所说,如果预测器根本不预测正类 - 精度为 0。
recall = TP/(TP+FN), in case if predictor doesn't predict positive class - TP is 0 - recall is 0.
召回率 = TP/(TP+FN),如果预测器不能预测正类 - TP 为 0 - 召回率为 0。
So now you are dividing 0/0.
所以现在你正在除以 0/0。
回答by Wazy
Precision, Recall, F1-scoreand Accuracycalculation
Precision、Recall、F1-score和Accuracy计算
- 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
Wikipedia reference
维基百科参考