Reputation: 53
I'm having this error with onehotencoder where thecategorical_features attribute is missing, I'm using google colab.
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
le = LabelEncoder()
X = star.iloc[:,:6].values
y = star.iloc[:,-1].values
X[:,5] = le.fit_transform(X[:,5])
y[:] = le.fit_transform(y[:])
ohe = OneHotEncoder(categorical_features= [5])
X = ohe.fit_transform(X).toarray()
TypeError Traceback (most recent call last)
<ipython-input-47-93f73a1a04ad> in <module>()
----> 1 ohe = OneHotEncoder(categorical_features= [5])
2 X = ohe.fit_transform(X).toarray()
TypeError: __init__() got an unexpected keyword argument 'categorical_features'
Upvotes: 2
Views: 7966
Reputation: 143098
In documentation for OneHotEncoder there is no 'categorical_features'
Older documentation (0.20) for OneHotEncoder shows that 'categorical_features'
will be removed in 0.22 (and sklearn
latest version has number 0.22.1) and you have to use ColumnTransformer.
But I don't know how to use it. But maybe examples in User Guide can help to use it.
EDIT:
Sklearn 0.20
pip install -U scikit-learn==0.20
Code
import sklearn
print(sklearn.__version__)
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
import numpy as np
print('--- data ---')
X = [
['Male', 1],
['Female', 3],
['Female', 2]
]
X = np.array(X)
print(X)
print('--- LabelEncoder ---')
le = LabelEncoder()
X[:,0] = le.fit_transform(X[:,0])
print(X)
print('--- OneHotEncoder ---')
ohe = OneHotEncoder(categorical_features=[0])
X = ohe.fit_transform(X).toarray()
print(X)
Result:
0.20.0
--- data ---
[['Male' '1']
['Female' '3']
['Female' '2']]
--- LabelEncoder ---
[['1' '1']
['0' '3']
['0' '2']]
--- OneHotEncoder ---
[[0. 1. 1.]
[1. 0. 3.]
[1. 0. 2.]]
Sklearn 0.22
pip install -U scikit-learn==0.22
Code:
import sklearn
print(sklearn.__version__)
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
from sklearn.compose import ColumnTransformer
import numpy as np
print('--- data ---')
X = [
['Male', 1],
['Female', 3],
['Female', 2]
]
X = np.array(X)
print(X)
print('--- LabelEncoder ---')
le = LabelEncoder()
X[:,0] = le.fit_transform(X[:,0])
print(X)
print('--- OneHotEncoder ---')
ct = ColumnTransformer([('my_ohe', OneHotEncoder(), [0])], remainder='passthrough')
X = ct.fit_transform(X) #.toarray()
print(X)
Result:
0.22.2.post1
--- data ---
[['Male' '1']
['Female' '3']
['Female' '2']]
--- LabelEncoder ---
[['1' '1']
['0' '3']
['0' '2']]
--- OneHotEncoder ---
[['0.0' '1.0' '1']
['1.0' '0.0' '3']
['1.0' '0.0' '2']]
In 0.22 it works even without LabelEncoder
but 0.20 needs LabelEncoder
import sklearn
print(sklearn.__version__)
from sklearn.preprocessing import OneHotEncoder
from sklearn.compose import ColumnTransformer
import numpy as np
print('--- data ---')
X = [
['Male', 1],
['Female', 3],
['Female', 2]
]
X = np.array(X)
print(X)
print('--- OneHotEncoder ---')
ct = ColumnTransformer([('my_ohe', OneHotEncoder(), [0])], remainder='passthrough')
X = ct.fit_transform(X) #.toarray()
print(X)
Upvotes: 7