Dimas Mukhlas Widiantoro

Welcome To dimasmukhlas.com

I am Dimas Mukhlas Widiantoro, a seasoned growth expert with 12 years of experience growing Brainly. I share my expertise in growth strategies, user acquisition, and product development. View my complete portfolio at codify.my.id

And now what is Ordinal Encoder


What OrdinalEncoder Does (Simple English)


OrdinalEncoder turns categories into numbers, but keeps the order the same as you define it (or the order they appear).


It converts something like this:

Small
Medium
Large

into this:

0
1
2

So each category gets a numeric value.


🧠 

Why do we need it?


Machine learning models work with numbers, not text.


If you have a column with ordered categories, such as:

  • Education level

  • Size

  • Income brackets

  • Ratings (good, medium, bad)


OrdinalEncoder helps the model understand that:

Small < Medium < Large
Basic < Intermediate < Advanced
Low income < Middle income < High income

There is meaningful order between them.


🔹 

Example

from sklearn.preprocessing import OrdinalEncoder

enc = OrdinalEncoder(categories=[['Low','Medium','High']])
X = enc.fit_transform([['Low'], ['High'], ['Medium']])
print(X)

Output:

[[0]
 [2]
 [1]]


⚠️ 

When you SHOULD use it


Use OrdinalEncoder when:


✔ The categories have a natural order

✔ The order carries real meaning

✔ Moving from one category to the next is a step


Examples:

  • Education level (High School < Bachelor < Master < PhD)

  • Product size (Small < Medium < Large)

  • Satisfaction (Bad < OK < Good < Excellent)


⚠️ 

When you should NOT use it


Do not use OrdinalEncoder when the categories do not have order, like:

  • Country

  • ZIP code

  • Color

  • Type of job

  • Brand

  • Gender


For those cases you need OneHotEncoder, not OrdinalEncoder.


Because encoding:

Red = 0
Blue = 1
Green = 2

would incorrectly tell the model:

Red < Blue < Green

—which is not true!


🟢 

Simple Summary


OrdinalEncoder turns ordered categories into numbers that preserve the order.
Use it only when the order has real meaning.


Post a Comment

0 Comments