1

scikit-learn seems to work, but when I did:

from sklearn.feature_selection import VarianceThreshold

I got the following error:

ImportError: cannot import name VarianceThreshold

How to bypass this? I am a newbie in Python, so I have no idea what to do.

I played with the order of my imports, as suggested here: ImportError: Cannot import name X, but no luck.

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

I am also getting this:

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'

Version:

>>> import sklearn
>>> sklearn.__version__
'0.14.1'
Community
  • 1
  • 1
gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • Same issue here. What OS are you on? – George WS May 01 '16 at 20:53
  • @GeorgeWS I was on Ubuntu 14.04, 64 bits at the time of posting. – gsamaras May 01 '16 at 20:57
  • Okay. This was working for me yesterday on Ubuntu 14.04.4, but not working now ssh'd into the same machine from Mac OS X 10.11.4. – George WS May 01 '16 at 21:01
  • @GeorgeWS I have absolutely no idea why you are facing problems. You could post a new question *or* if you think that mine can be extended, you should put a bounty on it and edit my question. – gsamaras May 01 '16 at 21:04
  • Totally—not expecting you to know what's causing it for me. Curious though, did you ever solve it on your machine? – George WS May 01 '16 at 21:13
  • 1
    There could be a chance I knew @GeorgeWS! Now that you are mentioning it, deadline was approaching fast for my project back then, so no, I moved forward. Probably this can still be an active question... – gsamaras May 01 '16 at 21:16

1 Answers1

0

You can bypass by catching the exception

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

If you want to catch only Attribue Error Exception then use below

try:
    from sklearn.feature_selection import VarianceThreshold
except AttributeError:
    pass # catches only Attribute Exception
gsamaras
  • 71,951
  • 46
  • 188
  • 305
Pavan
  • 350
  • 2
  • 7
  • 1
    So, there is no way I can *actually* use VarianceThreshold, right? :/ – gsamaras Feb 06 '16 at 19:11
  • 1
    from documnetation you are importing module correctly. [link to document](http://scikit-learn.org/stable/modules/feature_selection.html). you can try python -c 'import sklearn; print(sklearn.feature_selection.VarianceThreshold)' – Pavan Feb 08 '16 at 05:34
  • `AttributeError: 'module' object has no attribute 'feature_selection'`. OK thanks. – gsamaras Feb 08 '16 at 20:08