Why is this Deprication Warning halting code execution?

你说的曾经没有我的故事 提交于 2019-12-04 06:25:44

问题


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

I get the following warning message:

/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

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!


回答1:


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.

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


来源:https://stackoverflow.com/questions/53453959/why-is-this-deprication-warning-halting-code-execution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!