scikit-learn似乎有效,但是当我这样做时:

from sklearn.feature_selection import VarianceThreshold


我收到以下错误:


  ImportError:无法导入名称VarianceThreshold


如何绕过这个?我是Python的新手,所以我不知道该怎么做。

我按照我的建议按进口顺序进行游戏:ImportError: Cannot import name X,但是没有运气。

import sys
import pandas as pd
import numpy as np
import operator
from sklearn.feature_selection import VarianceThreshold
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.feature_extraction.text import HashingVectorizer
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.preprocessing import normalize
from sklearn import decomposition




我也得到这个:

code/python/k_means/serial_version$ python -c 'import sklearn; print(sklearn.VarianceThreshold)'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
AttributeError: 'module' object has no attribute 'VarianceThreshold'




版:

>>> import sklearn
>>> sklearn.__version__
'0.14.1'

最佳答案

您可以通过捕获异常来绕过

try:
    from sklearn.feature_selection import VarianceThreshold
except:
    pass  # it will catch any exception here

如果您只想捕获属性错误异常,请在下面使用
try:
    from sklearn.feature_selection import VarianceThreshold
except AttributeError:
    pass # catches only Attribute Exception

关于python - ImportError:无法导入名称VarianceThreshold,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35237289/

10-10 10:28