python - 导入错误: cannot import name VarianceThreshold

标签 python python-2.7 machine-learning scikit-learn bigdata

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

from sklearn.feature_selection import VarianceThreshold

我收到以下错误:

ImportError: cannot import name 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 - 导入错误: cannot import name VarianceThreshold,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35237289/

相关文章:

python - 如何将内部字典键/值提取到 Python 中的新字典中?

python - Pandas 数据框 : simplest way to find arow and select an element from that row

machine-learning - 检查模型输出和标签之间的相关性/互信息

machine-learning - 更改成本矩阵对 weka 结果没有影响

python - 是否可以在字典定义中结合理解和键值列表?

python - 修补 python 类之外的方法

python - Matplotlib 1.3.0,图例行和文本不匹配

python-2.7 - 如何进一步向量化这个函数?

python - 异常子类必须在类之外吗?

python - 时间差分学习中的重复计算