消除 scikit-learn 的警告

当我教学的时候,我想忽略来自所有软件包的警告,但是 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:

是我使用这个模块不正确,还是 sklearn 做了一些它不应该做的事情?

73204 次浏览

学习 强制警告让我非常恼火。

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

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


#... import sklearn stuff...

他们有 在2013年改变了他们的警告政策。你可以忽略警告(也是特定的类型) ,如下所示:

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

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