【问题标题】:Eliminating warnings from scikit-learn [duplicate]从 scikit-learn 中消除警告 [重复]
【发布时间】:2015-12-13 06:02:06
【问题描述】:

我想在教学时忽略所有包的警告,但 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 做了一些不该做的事情?

【问题讨论】:

  • 这个问题与@San 提到的帖子中的问题不同,它不是 上面标记的重复问题@joshterrell805 在下面给出了正确答案 - 它是sklearn 问题:他们强制弃用警告
  • @suever:这不是重复的。这确实是一个 scikit-learn 特定的问题。在 scikit-learn 0.22 中,该问题将得到修复,因为 scikit-learn 将不再重新配置警告过滤器,而是始终发出可以被自定义过滤器覆盖的 FutureWarnings:github.com/scikit-learn/scikit-learn/pull/15080
  • 正如@ogrisel 所提到的,这里的答案现在从版本 0.22(2019 年 12 月)开始已经过时。 Scikit-learn 现在使用 FutureWarning,您可以在上面的链接和here 中阅读更多详细信息
  • 从警告导入 filterwarnings filterwarnings("ignore")
  • 我考虑的是使用你的执行环境来重定向警告。例如,如果我从 Eclipse 开始,只需将 stderr 发送到其他地方,也许是一个文件,然后我就不必处理它了。与 scikit-learn 战斗对我来说似乎是一场失败的战斗

标签: python scikit-learn warnings


【解决方案1】:

他们有changed their warning policy in 2013。您可以通过以下方式忽略警告(也包括特定类型):

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

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

【讨论】:

  • 我试过了,但是没有用。我的编码环境是anaconda python 3.4 windows 10
  • 在不了解更多细节的情况下,很难判断您遇到问题的原因。但是,我的猜测是进口订单有问题。尝试两件事:在导入引发 DeprecationWarnings 的模块后立即放置上面的警告代码。如果这没有帮助,请尝试将代码放在所有其他导入之后。这应该足以解决您的问题或消除导入顺序问题。
  • 您甚至可以省略类别
  • 我喜欢这个,因为它清晰、直截了当,而且不那么老套,但它不适合我。我试着把它放在进口的底部,进口的顶部,什么都没有。我也不想取消所有警告
  • 似乎 filterwarnings 调用需要在调用发出警告的函数的文件中。在顶层调用它对我没有任何作用,但是当我把它放在给出警告的文件中时。并且应该澄清的是,您需要导入 sklearn 从其定义的异常集中抛出的任何警告。
【解决方案2】:

sklearn forces warnings 让我非常恼火。

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

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

#... import sklearn stuff...

【讨论】:

  • 像魅力一样工作。谢谢!
  • 不错。唯一的小问题是您实际上不能在自己的代码中使用警告
  • 并行 GridSearchCV 怎么样?
  • 对我不起作用。特别是,我收到了无法删除的 FitFailedWarning 消息。我正在做 GridSearchCV,我很清楚有些组合是不允许的,我不想被告知这一点,因为对于其他组合,这个项目非常有用。
猜你喜欢
  • 2018-01-18
  • 2019-06-04
  • 2014-02-21
  • 2019-01-12
  • 2018-08-21
  • 2013-12-03
  • 2021-10-16
  • 2018-06-14
  • 2016-05-07
相关资源
最近更新 更多