How to detect a Scikit-learn warning programmatically

Question:

While fitting a model using sklearn.neural_network.MLPClassifier I sometimes receive a warning printed in the console:

ConvergenceWarning: Stochastic Optimizer: Maximum iterations (300) reached and the optimization hasn’t converged yet.

Is there a way to detect the warning during runtime so I can act on it?

Asked By: Ramon Balthazar

||

Answers:

Check the n_iter_ attribute after fitting. If it is less than the maximum number of iterations you configured (max_iter), then it converged.

Answered By: Juan I Carrano

You can catch the warning in realtime with warnings.catch_warnings

import warnings

with warnings.catch_warnings()
    warnings.filterwarnings('error')
    try:
        model.fit(X, y)
    except Warning:
        # do something in response

This structure will catch any Warning in line and allow you to respond to it however you see fit. In this case that may be modifying some hyperparameter to make it easier for the model to converge.

You can also ignore warnings with warnings.filterwarnings and can specify the type of warning to ignore.

To ignore ConvergenceWarning:

from sklearn.execpetions import ConvergenceWarning

warnings.filterwarnings('ignore', category=ConvergenceWarning)

...
Answered By: Grr

Let’s say that you want to train your scikit-learn model and you want to be able to store the warnings (if any).
Let’s say that you fit the model as follows:

clf.fit(X_train, y)

If you want to catch the warning. Then you can run the model:

with warnings.catch_warnings(record=True) as caught_warnings:
    clf.fit(X_train, y)

Finally, you can get the warnings by iterating the caught_warnings as follows:

for warn in caught_warnings:
    print(warn)
Answered By: George Pipis
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.