问题描述
我运行一个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.
- 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.
在我的情况下,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
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.
召回率= 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说FN大于0时F1分数是不确定的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!