Search code examples
pythonscikit-learndata-science

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


[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()

Solution

  • 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()