如何使用 sklearn 禁用 ConvergenceWarning? [英] How to disable ConvergenceWarning using sklearn?

查看:205
本文介绍了如何使用 sklearn 禁用 ConvergenceWarning?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 GridSearchCV 来优化 SVM 的超参数.我设置了最大迭代次数,因为我等不及几个小时才能得到结果.我知道会有收敛警告.我只是想忽略这些警告而不显示在终端中.

I'm using GridSearchCV to optimize hyper-parameters for SVM. I set the maximum number of iterations because I can't wait several hours to get result. I know there will be convergence warnings. I just want to ignore these warnings and not show up in the terminal.

提前致谢.

推荐答案

这很难找到,因为我见过的所有建议答案都不起作用.最终对我有用的是示例代码 提前停止随机梯度下降:

This was a pain to track down, as all suggested answers I've seen simply do not work. What finally worked for me was in the example code Early stopping of Stochastic Gradient Descent:

from sklearn.utils.testing import ignore_warnings
from sklearn.exceptions import ConvergenceWarning

然后你可以像这样注释一个函数:

You can then annotate a function like so:

@ignore_warnings(category=ConvergenceWarning)
def my_function():
    # Code that triggers the warning

请注意,您不需要直接从 warnings 导入任何内容.

Note that you need not directly import anything from warnings.

我认为这很好,因为它只会在您需要的特定情况下抑制警告,而不是全局.

I think that's quite nice as it will only suppress warnings in the specific case where you need it, rather than globally.

这篇关于如何使用 sklearn 禁用 ConvergenceWarning?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆