I am trying to do ordinal encoding using:
from sklearn.preprocessing import OrdinalEncoder
I will try to explain my problem with a simple dataset.
X = pd.DataFrame({'animals':['low','med','low','high','low','high']})
enc = OrdinalEncoder()
enc.fit_transform(X.loc[:,['animals']])
array([[1.],
[2.],
[1.],
[0.],
[1.],
[0.]])
It is labelling alphabetically, but if I try:
enc = OrdinalEncoder(categories=['low','med','high'])
enc.fit_transform(X.loc[:,['animals']])
Shape mismatch: if n_values is an array, it has to be of shape (n_features,).
Which I do not understand. I would like to be able to decide how the labelling is done.
I considered doing this:
level_mapping={'low':0,'med':1,'high':2}
X['animals']=data['animals'].replace(level_mapping)
However, I have large number of features in my dataset which have similar categories.
Thanks.