Scikit learn Error Message 'Precision and F-score are ill-defined and being set to 0.0 in labels'

爱⌒轻易说出口 提交于 2019-12-05 03:29:26

The meaning of the warning

As the other answers here suggest, you encounter a situation where precision F-Score can't be computed due to its definition (precision/recall equal to 0). In this cases, the score of the metric is valued at 0.

Test data contains all labels, why does this still happen?

Well, you are using K-Fold (specifically in your case k=10), which means that one specific split might contain 0 samples of one class

Still happens, even when using Stratified K-Fold

This is a little tricky. Stratified K-Fold ensures the same portion of each class in each split. However, this does not only depend on the real classes. For example, Precision is computed like so: TP/predicted yes. If for some reason, you are predicting all of your samples with No, you will have predicted yes=0, which will result in undefined precision (which can lead to undefined F-Score).

This sounds like an edge case but consider the fact that in grid-search, you are probably searching for a whole lot of different combinations, which some might be totally off, and result in such scenario.

I hope this answers your question!

As aadel has commented, when no data points are classified as positive, precision divides by zero as it is defined as TP / (TP + FP) (i.e., true positives / true and false positives). The library then sets precision to 0, but issues a warning as actually the value is undefined. F1 depends on precision and hence is not defined either.

Once you are aware of this, you can choose to disable the warning with:

import warnings
import sklearn.exceptions
warnings.filterwarnings("ignore", category=sklearn.exceptions.UndefinedMetricWarning)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!