75

I would like to ignore warnings from all packages when I am teaching, but scikit-learn seems to work around the use of the warnings package to control this. For example:

with warnings.catch_warnings():
    warnings.simplefilter("ignore")
    from sklearn import preprocessing

/usr/local/lib/python3.5/site-packages/sklearn/utils/fixes.py:66: DeprecationWarning: inspect.getargspec() is deprecated, use inspect.signature() instead
  if 'order' in inspect.getargspec(np.copy)[0]:
/usr/local/lib/python3.5/site-packages/sklearn/utils/fixes.py:358: DeprecationWarning: inspect.getargspec() is deprecated, use inspect.signature() instead
  if 'exist_ok' in inspect.getargspec(os.makedirs).args:

Am I using this module incorrectly, or is sklearn doing something its not supposed to?

Chris Fonnesbeck
  • 4,143
  • 4
  • 29
  • 30
  • 17
    This issue is different from the one in the post mentioned by @San, and it is **not** a duplicate as marked above A correct answer is given below by @joshterrell805 - it is a `sklearn` issue: they force deprecation warnings – davalo Dec 16 '16 at 19:35
  • 2
    @suever: this is not a duplicate. This was indeed a scikit-learn specific issue. In scikit-learn 0.22, the issue will be fixed as scikit-learn will no longer reconfigure the warning filters and instead always issue FutureWarnings that can be override by custom filters: https://github.com/scikit-learn/scikit-learn/pull/15080 – ogrisel Oct 29 '19 at 20:54
  • 2
    As mentioned by @ogrisel , the answers here are now outdated starting from version 0.22 (December 2019). Scikit-learn uses FutureWarning now, you can read more details in the link above and [here](https://scikit-learn.org/stable/whats_new/v0.22.html#deprecations-using-futurewarning-from-now-on) – Niourf Dec 09 '19 at 12:12
  • 1
    from warnings import filterwarnings filterwarnings("ignore") – Ashish Anand Dec 14 '19 at 16:31
  • What I have considered is to use your execution environment to redirect the warning. For example, if I kick off from Eclipse, just have stderr sent elsewhere, a file perhaps, and then I don't have to deal with it. Fighting with scikit-learn for me seems to be a losing battle – demongolem Jun 05 '20 at 12:56

2 Answers2

113

It annoys me to the extreme that sklearn forces warnings.

I started using this at the top of main.py:

def warn(*args, **kwargs):
    pass
import warnings
warnings.warn = warn

#... import sklearn stuff...
jotik
  • 17,044
  • 13
  • 58
  • 123
joshterrell805
  • 1,407
  • 2
  • 11
  • 13
78

They have changed their warning policy in 2013. You can ignore warnings (also specific types) with something like this:

import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)

//EDIT: in the comments below, Reed Richards points out that the filterwarnings call needs to be in the file that calls the function that gives the warning. I hope this helps those who experienced problems with this solution.

Zakum
  • 2,157
  • 2
  • 22
  • 30
  • 16
    I tried it but it did not work. My coding environment is anaconda python 3.4 windows 10 – Duong Trung Nghia Nov 24 '16 at 15:31
  • Hard to judge why you experience problems, without knowing more details. However, my guess would be that import order is at fault. Try two things: Placing the warnings code above right after importing the module that throws the DeprecationWarnings. If that doesnt help, try putting the code after all the other imports. That should be enough to either solve your problem or eliminate import order as the issue. – Zakum Nov 27 '16 at 00:11
  • 1
    You can even leave out the category – CodingYourLife Jan 27 '17 at 23:11
  • 3
    I like this because it's clear, straightforward, and not so hacky, but it doesn't work for me. I tried putting it at the bottom of the imports, at the top of the imports, and nothing. I also don't want to suppress all warnings – Alex Cory Mar 20 '17 at 18:44
  • Great! This worked for me with the `UserWarning` category that scikit-learn's LDA was throwing every time during my cross-validation routine. – Ébe Isaac Sep 27 '17 at 04:34
  • I use PyCharm and this worked for me – AvinashK Feb 24 '18 at 06:05
  • 5
    It seems that the filterwarnings call needs to be in the file that calls the function that gives the warning. Calling it at the top level does nothing for me but when I put it in the file that gives the warning it does. And it should be clarified that you need to import whatever warning sklearn is throwing from its defined set of exceptions. – Reed Richards Jun 02 '18 at 14:37
  • Notable that this solution also works well for sklearn's future-deprecation Warnings. Just implement in the calling module, and change category to FutureWarning. – RossGK Feb 12 '20 at 19:12