ImportError:无法导入名称 VarianceThreshold

Posted

技术标签:

【中文标题】ImportError:无法导入名称 VarianceThreshold【英文标题】:ImportError: cannot import name VarianceThreshold 【发布时间】:2016-05-16 04:56:22 【问题描述】:

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'

【问题讨论】:

这里有同样的问题。你在什么操作系统上? @GeorgeWS 发帖时我使用的是 Ubuntu 14.04,64 位。 好的。这昨天在 Ubuntu 14.04.4 上为我工作,但现在无法通过 ssh 进入 Mac OS X 10.11.4 的同一台机器。 @GeorgeWS 我完全不知道您为什么会遇到问题。您可以发布一个新问题,如果您认为我的问题可以扩展,您应该悬赏并编辑我的问题。 我可能认识@GeorgeWS!既然你提到了它,那时我的项目的截止日期就快到了,所以不,我继续前进。可能这仍然是一个活跃的问题...... 【参考方案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

【讨论】:

那么,我无法实际上使用 VarianceThreshold,对吧? :// 从文档中您正在正确导入模块。 link to document。你可以试试 python -c 'import sklearn;打印(sklearn.feature_selection.VarianceThreshold)' AttributeError: 'module' object has no attribute 'feature_selection'。好的,谢谢。

以上是关于ImportError:无法导入名称 VarianceThreshold的主要内容,如果未能解决你的问题,请参考以下文章

ImportError:无法导入名称“WebClient”

ImportError:无法导入名称“AFAVSignature”

ImportError:无法导入名称“评估”(来自意外导入评估)

Django 'ImportError: 无法导入名称 url'

ImportError:导入 PCA 时无法导入名称“LatentDirichletAllocation”[关闭]

如何修复“ImportError:无法导入名称'StringIO'”