TypeError: __init__() got an unexpected keyword argument 'categorical_features' One Hot Encoder

Viewed 1797

I'm trying to resolve the code above, which I got from Kaggle, but I tried to run it and it throws this error:

return f(**kwargs) TypeError: init() got an unexpected keyword argument 'categorical_features'

This is the entire code:

data = pd.read_csv('auto-mpg.csv',sep = ',')
print(data.columns);
print(data.isnull().sum())

data['horsepower'] = data['horsepower'].replace('?','100')
print(data['horsepower'].value_counts())

print('O maior MPG é ',data.mpg.max(),'milhoes por galao')
print('O menor MPG é',data.mpg.min(),'milhoes por galao')

f,ax = plt.subplots(1,2,figsize=(12,6))
sns.boxplot(data.mpg,ax=ax[0])
sns.distplot(data.mpg,ax=ax[1])

print("Skewness: ",data['mpg'].skew())
print("Kurtosis: ",data['mpg'].kurtosis())

corr = data.corr()
print(corr)

x = data.iloc[:,1:].values
y = data.iloc[:,0].values

lb = LabelEncoder()
x[:,7] = lb.fit_transform(x[:,7])


onehot = OneHotEncoder(categorical_features = x)
x = onehot.fit_transform(x).toarray()


xtrain,xtest,ytrain,ytest = train_test_split(x,y,test_size = 0.2,random_state = 0)


sc = StandardScaler()
x = sc.fit_transform(x)


rfr = RandomForestRegressor(n_estimators = 200,random_state = 0)
rfr.fit(xtrain,ytrain)

ypred_rfr = rfr.predict(xtest)
print('Accuracy of the random forest model:',round(r2_score(ytest,ypred_rfr)*100,2),'%')

So how can I handle this error?

2 Answers

Deprecated since version 0.20: The categorical_features keyword was deprecated in version 0.20 and will be removed in 0.22. You can use the ColumnTransformer instead.

See Scikit-learn 0.20: sklearn.preprocessing.OneHotEncoder for more details

And this stackoverflow question: 54345667 show how to rewrite with ColumnTransformer

from sklearn.compose import ColumnTransformer

ct = ColumnTransformer([
    ('<Name>', OneHotEncoder(), x)], remainder="passthrough")
ct.fit_transform(x)

From this code, I am not so sure if it makes sense to onehot encode all the columns. Including the numerical ones.

Let's say the aim is to convert the column car name to categorical and one hot encode.

import pandas as pd
from scipy.sparse import csr_matrix
from sklearn.preprocessing import LabelEncoder,OneHotEncoder
from sklearn.ensemble import RandomForestRegressor

data = pd.read_csv('auto-mpg.csv',sep = ',')
data.columns

Index(['mpg', 'cylinders', 'displacement', 'horsepower', 'weight',
       'acceleration', 'model year', 'origin', 'car name'],
      dtype='object')

As @Jacky1205 has pointed out in the other answer, this function is deprecated. And it will be better to use the data.frames rather than keep them in an array, if you want to use ColumnTransformer. For example:

from sklearn.compose import ColumnTransformer

ct = ColumnTransformer([
    ('one hot', OneHotEncoder(), ["car name"])], remainder="passthrough")
x = ct.fit_transform(data.iloc[:,1:])

You can also work at the array level, it can get messy and in this case since your data is not large, you can keep it as a dense matrix:

x = data.iloc[:,1:].values
y = data.iloc[:,0].values

lb = LabelEncoder()
x[:,7] = lb.fit_transform(x[:,7])

onehot = OneHotEncoder(sparse=False)
x = np.concatenate([x[:,:7],onehot.fit_transform(x[:,7].reshape(-1,1))],axis=1)
Related