为什么这个删除警告会停止代码执行?

2024-06-25 23:28:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用Sci工具包学习包中的TfidifVectorizer和CountVectorizer,但当我导入它们时:
from sklearn.feature_extraction.text import TfidfVectorizer, CountVectorizer

我收到以下警告信息:

/anaconda3/lib/python3.7/site-packages/sklearn/feature_extraction/text.py:17: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working from collections import Mapping, defaultdict

我的代码在这之后停止运行,即使消息只是一个警告,指示出现了错误(即使没有错误报告)。我想这就是我询问警告的真正原因,因为这是我必须摆脱的一切。 这是SKLearn的错误吗?自从Python3.7更新后,开发人员落后了吗?对于我是否应该报告这个问题,或者如何使用anaconda恢复到python3.6来解决这个问题,我们将非常感谢。谢谢您!在


Tags: textfromimport信息警告工具包错误sklearn
1条回答
网友
1楼 · 发布于 2024-06-25 23:28:54

此弃用警告引发问题的可能性很小。默认情况下,所有警告只打印一条消息,然后继续。警告可以被视为异常,但随后您将看到stacktrace而不是警告文本。弃用警告是为开发人员准备的,用来通知他们,他们的库将在将来的python版本中崩溃。它们不是为最终用户设计的,因为代码仍然可以完美地工作。例如,根据警告,from collections import Mapping, defaultdict仍然可以在python(3.7)版本中工作,但在python3.8中不行。在

因此,这个警告不太可能是问题的根源。您看到这是因为sklearn更改了默认的警告过滤器,这样it用户将看到sklearn发出的不推荐警告。 除非将警告设置为错误,否则警告不会更改执行流。 要验证警告不是问题所在,您可以尝试在this-harness中运行程序。这相当老套,但是需要停止sklearn覆盖默认的警告过滤器。通过使用警告过滤器的值,您应该能够看到不推荐警告不是问题的根源。在

import warnings
from warnings import (
    filterwarnings as original_filterwarnings, 
    simplefilter as original_simplefilter
)

def ignore_filterwarnings(*args, **kwargs):
    pass

def ignore_simplefilter(*args, **kwargs):
    pass

warnings.filterwarnings = ignore_filterwarnings
warnings.simplefilter = ignore_simplefilter

# no imports of sklearn should occur before the previous two lines (otherwise sklearn
# will get to use the original functions).

with warnings.catch_warnings():
    original_simplefilter("ignore")  # or "error" to see the difference

    from my_main_module import main
    main()

相关问题 更多 >