Search code examples
pythonscikit-learnwarningslogistic-regression

TypeError: sklearn ignore_warnings expects class or tuple of classes


I am trying to get a rough overlook on good parameters for several models including LogisticRegression with RandomizedSearchCV. Since some of the parameters combinations are incompatible I get sklearn FitFailedWarning i.e Solver newton-cg supports only 'l2' or 'none' penalties, got l1 penalty.

I would like to simply ignore those specific warnings and the solution I found to do so was to use :

from sklearn.exceptions import FitFailedWarning
from sklearn.utils._testing import ignore_warnings
with ignore_warnings(category=[FitFailedWarning]):
    grid.fit(x_train, y_train)

My problem is that, although that works normally for most grids models (knn, decision tree etc.) it fails for LogisticRegression grid with error:

TypeError: issubclass() arg 2 must be a class or tuple of classes

while following fit without ignore_warnings works

lr_grid.fit(x_train, y_train)

Is there another proper way to silence FitFailedWarning for RandomizedSearchCV with LogisticRegression?


Solution

  • You could just leave the default param value Warning to parameter category in the context manager ignore_warnings this will mute all category warnings.

    with ignore_warnings():
        grid.fit(x_train, y_train)
    

    or pass a tuple of warnings. The error is because you're passing a list and it expects a Warning class or a tuple with warning classes.

    with ignore_warnings(category=(FitFailedWarning, UserWarning)):
        search = grid.fit(X_train, y_train)