One hot encoding "got an unexpected keyword argument 'categorical_features'"
Question:
[
error : oneHot = OneHotEncoder(categorical_features = [0])
TypeError: init() got an unexpected keyword argument
‘categorical_features’.
I’m tring to encode first column
here’s my code sample:
from sklearn.preprocessing import LabelEncoder,OneHotEncoder
labelen_x = LabelEncoder() # string to numeric encoding object
x[:,0]= labelen_x.fit_transform(x[:,0]) # replaces the string labels with numerics for ML algorithm to be able to work with it
oneHot = OneHotEncoder(categorical_features = [0])
x = oneHot.fit_transform(x).toarray()
Answers:
With scikit-learn version 0.21.3
I’m able to get correct one-hot-encoding as follows:
As you can see with the warning, that the parameter categorical_features
would be depreciated with sklearn version 0.22
onwards. So, you need to use ColumnTransformer
instead. Here is the doc for ColumnTransformer
The parameter is just categories
.
https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.OneHotEncoder.html
You shouldn’t pass any obejct while initialise OHE class, simply do:
from sklearn.preprocessing import OneHotEncoder
# sample data
df = pd.DataFrame({'col': [0,1,2,3,0,1,2]})
colnames = ['col'] # modify this for your df
oneHot = OneHotEncoder()
x_ohe = oneHot.fit_transform(df[colnames].values.reshape(-1,1))
To check how the one-hot-encoded data looks (see matrix), simply do:
x_ohe.todense()
I also had the same issue, could not find the cause but be able to find an answer. Hope it would help. Further details can be taken from this link.
-Thanks-
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import OneHotEncoder
ct = ColumnTransformer([('any_name', OneHotEncoder(), [1])], remainder='passthrough')
X = np.array(ct.fit_transform(X), dtype=np.float)
This is the latest solution for the categorical_feature error.
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
Label_x = LabelEncoder()
x[:,0] = Label_x.fit_transform(x[:,0])
onehotencoder = OneHotEncoder.categorical_features =[0]
OneHotEncoder().fit_transform(x).toarray()
x = x[:, 1:]
[
error : oneHot = OneHotEncoder(categorical_features = [0])
TypeError: init() got an unexpected keyword argument
‘categorical_features’.
I’m tring to encode first column
here’s my code sample:
from sklearn.preprocessing import LabelEncoder,OneHotEncoder
labelen_x = LabelEncoder() # string to numeric encoding object
x[:,0]= labelen_x.fit_transform(x[:,0]) # replaces the string labels with numerics for ML algorithm to be able to work with it
oneHot = OneHotEncoder(categorical_features = [0])
x = oneHot.fit_transform(x).toarray()
With scikit-learn version 0.21.3
I’m able to get correct one-hot-encoding as follows:
As you can see with the warning, that the parameter categorical_features
would be depreciated with sklearn version 0.22
onwards. So, you need to use ColumnTransformer
instead. Here is the doc for ColumnTransformer
The parameter is just categories
.
https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.OneHotEncoder.html
You shouldn’t pass any obejct while initialise OHE class, simply do:
from sklearn.preprocessing import OneHotEncoder
# sample data
df = pd.DataFrame({'col': [0,1,2,3,0,1,2]})
colnames = ['col'] # modify this for your df
oneHot = OneHotEncoder()
x_ohe = oneHot.fit_transform(df[colnames].values.reshape(-1,1))
To check how the one-hot-encoded data looks (see matrix), simply do:
x_ohe.todense()
I also had the same issue, could not find the cause but be able to find an answer. Hope it would help. Further details can be taken from this link.
-Thanks-
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import OneHotEncoder
ct = ColumnTransformer([('any_name', OneHotEncoder(), [1])], remainder='passthrough')
X = np.array(ct.fit_transform(X), dtype=np.float)
This is the latest solution for the categorical_feature error.
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
Label_x = LabelEncoder()
x[:,0] = Label_x.fit_transform(x[:,0])
onehotencoder = OneHotEncoder.categorical_features =[0]
OneHotEncoder().fit_transform(x).toarray()
x = x[:, 1:]