Python 消除来自 scikit-learn 的警告

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32612180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 11:54:11  来源:igfitidea点击:

Eliminating warnings from scikit-learn

pythonscikit-learnwarnings

提问by Chris Fonnesbeck

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

我想在教学时忽略所有包的警告,但 scikit-learn 似乎可以解决使用warnings包来控制这一点。例如:

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?

我是否错误地使用了这个模块,或者 sklearn 做了不应该做的事情?

回答by joshterrell805

It annoys me to the extreme that sklearn forces warnings.

sklearn强制发出警告让我非常恼火。

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

我开始在 main.py 的顶部使用它:

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

#... import sklearn stuff...

回答by Zakum

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

他们在 2013 年改变了警告政策。您可以使用以下内容忽略警告(也是特定类型):

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.

//编辑:在下面的评论中,里德理查兹指出,the filterwarnings call needs to be in the file that calls the function that gives the warning.我希望这可以帮助那些遇到此解决方案问题的人。