'python warnings.filterwarnings does not ignore DeprecationWarning from 'import sklearn.ensemble'

I am trying to silence the DeprecationWarning with the following method.

import warnings
warnings.filterwarnings(action='ignore')
from sklearn.ensemble import RandomForestRegressor

However, it still shows:

DeprecationWarning: numpy.core.umath_tests is an internal NumPy module and should not be imported. It will be removed in a future NumPy release. from numpy.core.umath_tests import inner1d

Why does this happen, and how can I fix it?

I'm running this on python 3.6.6, numpy 1.15.0 and scikit-learn 0.19.2, and adding category=DeprecationWarning didn't help.



Solution 1:[1]

The reason this happens is that Scikit resets your DeprecationWarning filter when you import it:

# Make sure that DeprecationWarning within this package always gets printed
warnings.filterwarnings('always', category=DeprecationWarning,
                        module=r'^{0}\.'.format(re.escape(__name__)))

Sneaky!

The only fix I've found is to temporarily suppress stderr:

import os
import sys
sys.stderr = open(os.devnull, "w")  # silence stderr
from sklearn.ensemble import RandomForestRegressor
sys.stderr = sys.__stderr__  # unsilence stderr

where sys.__stderr__ refers to the system's actual stderr (as opposed to sys.stderr, which just tells Python where to print stderr to).

Solution 2:[2]

Not sure if this would work. But I tried to recreate the warning and it was silenced so try this:

import logging
logging.captureWarnings(True)

According to the docs "If capture is True, warnings issued by the warnings module will be redirected to the logging system. "

Here's what I did:

import logging
import re
import warnings
logging.captureWarnings(True)
warnings.filterwarnings('always', category=DeprecationWarning,
                        module=r'^{0}\.'.format(re.escape(__name__)))
warnings.warn("This is a DeprecationWarning",category=DeprecationWarning)

Warning wasn't thrown.

logging.captureWarnings(False)
warnings.warn("This is a DeprecationWarning",category=DeprecationWarning)

Output:

.../ipython:2: DeprecationWarning: This is a DeprecationWarning

Solution 3:[3]

Yet another heavy-hand approach.

import warnings as wa
wa.warn_explicit = wa.warn = lambda *_, **__: None

Indeed, overwriting ~.warn_explicit and ~.warn does job once for all wherever it is called.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Omkar Sabade
Solution 3 keepAlive