scikit-learn 似乎可以正常工作,但在执行以下操作时:
from sklearn.feature_selection import VarianceThreshold
我遇到了以下错误:
ImportError: 无法导入名称 VarianceThreshold
如何解决这个问题?我是Python新手,所以不知道该怎么做。
我按照这里的建议尝试调整导入顺序:ImportError: Cannot import name X,但没有效果。
import sysimport pandas as pdimport numpy as npimport operatorfrom sklearn.feature_selection import VarianceThresholdfrom sklearn.feature_extraction.text import TfidfVectorizerfrom sklearn.feature_extraction.text import HashingVectorizerfrom sklearn.feature_extraction.text import CountVectorizerfrom sklearn.preprocessing import normalizefrom 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 VarianceThresholdexcept: pass # 它会捕获任何异常
如果你只想捕获属性错误异常,则使用以下代码
try: from sklearn.feature_selection import VarianceThresholdexcept AttributeError: pass # 只捕获属性异常