How to disable ConvergenceWarning using sklearn?

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

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.

Leave a Comment

tech