ImportError: cannot import name VarianceThreshold - python

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'

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

Related

How to resolve the ImportError: cannot import name 'DesicionTreeClassifier' from 'sklearn.tree' in python?

Hello there,
I am new to python and I was trying out a project on jupyter notebook when I encountered an error which I couldn't resolve. I'd really appreciate some help.
This is my code:
import pandas as pd
from sklearn.tree import DesicionTreeClassifier
music_data = pd.read_csv(r'C:\python\python382\music.csv')
X=music_data.drop(columns=['genre'])
y=music_data['genre']
model=DesicionTreeClassifier()
model.fit(X,y)
music_data
And i got the output as :
ImportError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_2540/2462038274.py in <module>
1 import pandas as pd
----> 2 from sklearn.tree import DesicionTreeClassifier #using desicion tree algo here to make model[we import DesicionTree module from tree module which is imported from sklearn library]
3 music_data = pd.read_csv(r'C:\python\python382\music.csv')
4
5 ##Cleaning and segregating data
ImportError: cannot import name 'DesicionTreeClassifier' from 'sklearn.tree' (C:\python\python382\lib\site-packages\sklearn\tree\__init__.py)
Thank you.
You have missspelled the fumction name DesicionTreeClassifier is in reality DecisionTreeClassifier

I'm trying to import KNeihgborsClassifier from 'sklearn.neighbors'

I'm trying to import KNeihgborsClassifier from 'sklearn.neighbors' but I have this error ImportError: cannot import name 'KNeihgborsClassifier' from 'sklearn.neighbors' (C:\Users\lenovo\anaconda3\lib\site-packages\sklearn\neighbors_init_.py)
You are importing KNeihgborsClassifier which is wrong, change it to:
from sklearn.neighbors import KNeighborsClassifier

How to add a function into utils module on titanic data set?

When I run the following code. I am having an error saying AttributeError: module 'python_utils' has no attribute 'clean_data', but I know know how to fix it.
import python_utils
import pandas as pd
from sklearn import linear_model
def clean_data(data):
data["Fare"]=data["Fare"].fillna(data["Fare"].dropna().median())
data["Age"]=data["Age"].fillna(data["age"].dropna().median())
data.loc[data["Sex"]=="male", "Sex"]=0
data.loc[data["Sex"]=="female", "Sex"]=1
data["Embarked"]=data["Embarked"].fillna("S")
data.loc[data["Embarked"]=="S",Embarked]=0
data.loc[data["Embarked"]=="C",Embarked]=1
data.loc[data["Embarked"]=="Q",Embarked]=2
train=pd.read_csv('train.csv')
python_utils.clean_data(train)
target=train["Survived"].values
features=train[["Pclass","Age","Sex","SibSp","Parch"]].values
classifier=linear_model.logisticRegression()
classifier=classifier.fit(features,target)
print(classifier_.score(features,target))

Import Label Encorder sklearn

I am getting below error when running the code :-
from sklearn.preprocessing import LabelEncorder
LabelEncoder_X = LabelEncorder()
mod_set[:,0] = labelencorder_X.fit_transform(mod_set[:,0])
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-12-5d65523a64f6> in <module>
----> 1 from sklearn.preprocessing import LabelEncorder
2 LabelEncoder_X = LabelEncorder()
3 mod_set[:,0] = labelencorder_X.fit_transform(mod_set[:,0])
ImportError: cannot import name 'LabelEncorder' from 'sklearn.preprocessing' (/Users/kenilpatel/opt/anaconda3/lib/python3.7/site-packages/sklearn/preprocessing/__init__.py)
I tried reinstalling scikit as well as updating conda , but nothing seems to work.The problem is with Encorder only , Imputer and other things work fine .
You got a spelling mistake in your module import: LabelEncorder => LabelEncoder
# from sklearn.preprocessing import LabelEncorder
from sklearn.preprocessing import LabelEncoder

Not able to create shortcut for sklearn

I entered the following code:
import sklearn
import sklearn as sk
import sklearn.preprocessing as skl
from sklearn.preprocessing import Imputer
from sk.preprocessing import Imputer
from skl import Imputer
The part which reads; from sklearn.preprocessing import Imputer gets executed normally.
However, when I run from sk.preprocessing import Imputer, I get the following error:
from sk.preprocessing import Imputer
Traceback (most recent call last):`
File "<ipython-input-84-fc12144914d1>", line 1, in <module>`
from sk.preprocessing import Imputer`
ModuleNotFoundError: No module named 'sk'`
And from skl import Imputer yields the following:
from skl import Imputer`
Traceback (most recent call last):`
File "<ipython-input-85-1e925587d122>", line 1, in <module>`
from skl import Imputer`
ModuleNotFoundError: No module named 'skl'`
Why am I not able to create a shortcut for the Library?
Because it is wrong to do so. The right way do do it is as you have written already.
from sklearn.preprocessing import Imputer
the __init__.py in the preprocessing directory of sklearn defines the possible imports from that level.
The below is a valid aliasing and i think is what you are looking for.
from sklearn.preprocessing import Imputer as imp

Categories

Resources