问题描述
我运行一个 Python 程序,该程序调用 sklearn.metrics
的方法来计算精度和 F1 分数.这是没有预测样本时的输出:
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:
/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,所以
When there is no predicted sample, it means that TP+FP is 0, so
- 精度(定义为TP/(TP+FP))为0/0,未定义,
- 如果 FN 不为零,F1 分数(定义为 2TP/(2TP+FP+FN))为 0.
在我的例子中,sklearn.metrics
也返回准确度为 0.8,召回为 0.所以 FN 不为零.
In my case, sklearn.metrics
also returns the accuracy as 0.8, and recall as 0. So FN is not zero.
但为什么 scikilearn 说 F1 定义不明确?
But why does scikilearn says F1 is ill-defined?
Scikilearn 对 F1 的定义是什么?
What is the definition of F1 used by Scikilearn?
推荐答案
https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/metrics/classification.py
F1 = 2 *(精度*召回)/(精度+召回)
precision = TP/(TP+FP) 正如您刚才所说,如果预测器根本不预测正类 - 精度为 0.
precision = TP/(TP+FP) as you've just said if predictor doesn't predicts positive class at all - precision is 0.
recall = TP/(TP+FN),如果预测器没有预测到正类 - TP 为 0 - 召回为 0.
recall = TP/(TP+FN), in case if predictor doesn't predict positive class - TP is 0 - recall is 0.
所以现在你正在除以 0/0.
So now you are dividing 0/0.
这篇关于为什么 scikitlearn 说 F1 分数不明确,FN 大于 0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!