One hot encoding "got an unexpected keyword argument 'categorical_features'"

Question:

[categories[0] does not seem to be working [1]

0th column is what I'm trying to one hot encode

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()
Asked By: Pawan Nirpal

||

Answers:

With scikit-learn version 0.21.3 I’m able to get correct one-hot-encoding as follows:

enter image description here

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

Answered By: Balraj Ashwath

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()
Answered By: YOLO

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)
Answered By: umair

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:]
Answered By: Praveen Inbavadivu