Course contentsShow
← Machine Learning and Deep Learning
Lesson 419 of 3,53810. Feature EngineeringFree lesson

Label Encoding for Ordinal Variables

Mapping ordered categories to integers while preserving their natural ranking (e.g., low=0, medium=1, high=2).

Label Encoding for Ordinal Variables

What You'll Learn

You'll learn how to convert ordered categories into numbers that preserve their natural ranking, making them usable in machine learning models.

The Core Idea

Remember from the previous lesson that ordinal variables have a meaningful order—like t-shirt sizes (Small, Medium, Large) or education levels (High School, Bachelor's, Master's, PhD). Label encoding maps these categories to integers in a way that respects their ordering.

Think of it like assigning medal rankings: Bronze gets 3, Silver gets 2, and Gold gets 1. The numbers reflect the hierarchy.

How It Works

For ordinal variables, you assign consecutive integers that match the natural order:

  • Education level: High School → 0, Bachelor's → 1, Master's → 2, PhD → 3
  • Priority: Low → 0, Medium → 1, High → 2
  • Rating: Poor → 1, Fair → 2, Good → 3, Excellent → 4

The key is that the numerical gap matters. If you encode Low=0, Medium=1, High=2, you're telling the model that the "distance" between Low and Medium equals the distance between Medium and High. The model can now understand that High > Medium > Low mathematically.

Why This Matters

Unlike nominal categories (which we'll encode differently), ordinal variables benefit from this numeric representation because machine learning algorithms can use the ordering in their calculations. A linear model can learn that "each step up in education level increases salary by $X," for example.

Important Consideration

Make sure your encoding truly reflects equal intervals. If the categories aren't evenly spaced in reality, the model might misinterpret the relationships—but for most ordinal features, simple integer encoding works well.

Key Takeaway: Label encoding transforms ordered categories into integers (0, 1, 2, ...) that preserve their natural ranking, allowing models to mathematically understand and use the ordering in predictions.