本文介绍了为什么此“剥夺警告"会中止代码执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用Sci-Kit Learn软件包中的TfidifVectorizer和CountVectorizer,但是在导入它们时:
from sklearn.feature_extraction.text import TfidfVectorizer, CountVectorizer

I tried to use the TfidifVectorizer and CountVectorizer from the Sci-Kit Learn package, but when I import them:
from sklearn.feature_extraction.text import TfidfVectorizer, CountVectorizer

我收到以下警告消息:

此后,即使消息只是警告,我的代码也停止运行,表明出现了错误(即使未报告任何错误).我想这就是我要提出警告的真正原因,因为这是我必须做的所有事情.这是SKLearn的错误吗?自从python 3.7更新以来,开发人员在后面吗?任何有关我是否应该报告此问题或如何使用anaconda还原为python 3.6来解决此问题的建议,将不胜感激.谢谢!

My code stops running after this even though the message is just a warning, indicating that an error arises (even though no error is reported back). I suppose this is the true reason I am asking about the warning because it is all that I have to go off of.Is this a bug with SKLearn? Since the update to python 3.7, are the developers behind? Any suggestions on whether I should report this, or how to revert to python 3.6 using anaconda to get around this would be greatly appreciated. Thank you!

推荐答案

此弃用警告引起问题的可能性很小.默认情况下,所有警告仅会打印一条消息,然后继续.警告可以视为异常,但是您会看到一个堆栈跟踪而不是警告文本.弃用警告是针对开发人员的,旨在告知开发人员他们的库将在将来的python版本中中断.它们并不适合最终用户,因为代码仍然可以正常工作.例如,根据警告,from collections import Mapping, defaultdict仍适用于您的python版本(3.7),但不适用于python 3.8.

It's highly unlikely that this deprecation warning is causing an issue. By default, all warnings just print a message and then continue. Warnings can be treated as exceptions, but then you'd see a stacktrace instead of the warning text. Deprecation warnings are meant for developers, to inform them that their library will break for future python releases. They are not meant for end users, as the code still works perfectly fine. Case in point, as per the warning, from collections import Mapping, defaultdict still works in your version of python (3.7), but won't in python 3.8.

因此,此警告不太可能成为问题的根源.您看到它的原因是sklearn更改了默认警告过滤器,以便用户可以看到sklearn发出的弃用警告.除非将警告设置为错误,否则警告不会更改执行流程.要验证警告不是问题,您可以尝试在此工具中运行程序.这很hacky,但是需要停止sklearn覆盖默认的警告过滤器.通过使用警告过滤器的值,您应该能够看到不赞成使用警告不是问题的根源.

As such, this warning is unlikely to be the source of the problem. That you are seeing it is because sklearn changes the default warning filters so that it users will see deprecation warnings issued by sklearn.Warnings don't change the execution flow unless they are set to be treated as errors.To verify that the warning is not the problem you could try running your program in a this harness. It's rather hacky, but it's required to stop sklearn overriding the default warning filters. By playing around with the values of the warning filter you should be able to see that the deprecation warning is not the source of your problem.

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()

这篇关于为什么此“剥夺警告"会中止代码执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 12:35