首页 > 解决方案 > 沉默未定义度量警告

问题描述

UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 due to no predicted samples.运行带有交叉验证的随机搜索管道确实有很多结果。我知道是什么导致了这种行为,建议设置为 0.0 分数目前对我来说很好,所以我现在只想让这个警告静音。

我试过了:

warnings.filterwarnings('ignore') 

from sklearn.exceptions import UndefinedMetricWarning
warnings.filterwarnings('ignore', category=UndefinedMetricWarning) 

但是我仍然收到这些警告,即使 StackOverflow 上的其他答案表明它们应该被这些行抑制(实际上它在前一段时间在笔记本中对我有用)。

warnings.filterwarnings(...)行位于import语句的正下方,警告来自嵌套函数之一。

标签: scikit-learnsuppress-warnings

解决方案


按原样使用以下内容(复制粘贴):

from sklearn.exceptions import UndefinedMetricWarning

def warn(*args, **kwargs):
    pass
import warnings
warnings.warn = warn

# more code here...
# more code here...

推荐阅读