4

I have a column in my Used cars price prediction dataset named "Owner_Type". It has four unique values which are ['First', 'Second', 'Third', 'Fourth']. Now the order that makes the most sense is First > Second > Third > Fourth as the price decreases with respect to this order. How can I give this order to the values with OrdinalEncoder()? Please help me, thank you!

Vipul Sarode
  • 43
  • 1
  • 4

1 Answers1

10

OrdinalEncoder have a parameter categories which can accept list of array of categories. here a code example:

from sklearn.preprocessing import OrdinalEncoder
enc = OrdinalEncoder(categories=[['first','second','third','forth']])
X = [['third'], ['second'], ['first']]
enc.fit(X)
print(enc.transform([['second'], ['first'], ['third'],['forth']]))
Ghassen Sultana
  • 1,223
  • 7
  • 18
  • Why do you have to fit the encoder to X if you are already supplying the categories? – Maturin Aug 17 '23 at 23:14
  • if you apply transform without applying fit you get an error "AttributeError: 'OrdinalEncoder' object has no attribute 'categories_'" so you must aplly fit_transform or fit the model first and then apply transform. – Ghassen Sultana Aug 20 '23 at 18:00
  • I see, you're totally right. It's a bit silly, though, since the calling fit when the categories are already supplied does nothing other than create the attribute `categories_`. It seems that this attribute should be created automatically if categories are supplied. – Maturin Aug 21 '23 at 20:29