Skip to content
C

Encoding Categorical Data

Complete learning notes


1. Introduction

Most ML algorithms are fundamentally mathematical — they work with numbers, not text categories like "Red," "Blue," or "Green." Encoding categorical data is the process of converting these text-based categories into numeric form so that ML algorithms can actually use them.


2. What is Encoding Categorical Data?

Simple definition: Encoding categorical data means converting text-based category values into numbers, so ML algorithms (which require numeric input) can process them.

Technical explanation: Categorical encoding transforms non-numeric categorical variables into a numeric representation using techniques such as Label Encoding (assigning each category an integer) or One-Hot Encoding (creating separate binary columns for each category), chosen based on whether the categories have a meaningful order.


3. Why is it Important?

  • Nearly every ML algorithm in Scikit-learn requires numeric input — categorical (text) columns will cause errors if left unencoded.
  • Choosing the wrong encoding method can accidentally introduce a false sense of "order" or "magnitude" into categories that don't actually have one.
  • Almost every real-world dataset contains at least some categorical columns (e.g., city, gender, product category).

4. Prerequisites

Comfort with Pandas (Module 1, Topic 6) and Features & Labels (Module 2, Topic 9).


5. Core Concepts

  1. Label Encoding
  2. One-Hot Encoding
  3. Ordinal vs Nominal categorical data
  4. pd.get_dummies() and Scikit-learn's OneHotEncoder/LabelEncoder

6. Detailed Explanation

a) Label Encoding

Label Encoding assigns each unique category an integer (e.g., "Red" → 0, "Blue" → 1, "Green" → 2). This is simple, but it implies an ordering (0 < 1 < 2) that may not actually exist among the categories.

b) One-Hot Encoding

One-Hot Encoding creates a separate binary (0/1) column for each category, avoiding any false sense of order. For example, a "Color" column with values Red/Blue/Green becomes three new columns: Color_Red, Color_Blue, Color_Green, each containing 1 if that row matches that color, and 0 otherwise.

c) Ordinal vs Nominal Data

Ordinal data has a meaningful natural order (e.g., "Low," "Medium," "High") — Label Encoding can be appropriate here, as long as the assigned numbers respect that order. Nominal data has no inherent order (e.g., city names, colors) — One-Hot Encoding is generally preferred here, to avoid implying a false ranking.

d) Tools for Encoding

pd.get_dummies() is a quick, convenient way to apply one-hot encoding directly in Pandas. Scikit-learn's LabelEncoder and OneHotEncoder classes provide similar functionality with better integration into ML pipelines (especially important for consistently encoding both training and test data).


7. How It Works

  1. Identify which columns in your dataset are categorical (text-based).
  2. Determine whether each column is ordinal (has meaningful order) or nominal (no inherent order).
  3. Apply Label Encoding for ordinal columns, and One-Hot Encoding for nominal columns.
  4. Verify the resulting numeric columns are correctly formatted before feeding them into an ML model.

8. Real-World Example

Consider a dataset predicting customer satisfaction, with a "Satisfaction Level" column containing "Low," "Medium," "High" (ordinal — clear order) and a "City" column containing "Delhi," "Mumbai," "Pune" (nominal — no inherent order). Label Encoding "Satisfaction Level" as 0, 1, 2 makes sense since it preserves genuine meaning. But Label Encoding "City" as 0, 1, 2 would falsely suggest Pune (2) is somehow "greater than" Delhi (0) — which makes no real-world sense, so One-Hot Encoding is the better choice here.


9. Python Example

python
import pandas as pd from sklearn.preprocessing import LabelEncoder data = { "city": ["Delhi", "Mumbai", "Pune", "Delhi"], "satisfaction": ["Low", "High", "Medium", "Medium"] } df = pd.DataFrame(data) # One-Hot Encoding for nominal data ("city") df_one_hot = pd.get_dummies(df, columns=["city"]) print("One-Hot Encoded 'city':") print(df_one_hot) # Label Encoding for ordinal data ("satisfaction") satisfaction_order = {"Low": 0, "Medium": 1, "High": 2} df["satisfaction_encoded"] = df["satisfaction"].map(satisfaction_order) print("\nLabel Encoded 'satisfaction' (respecting order):") print(df[["satisfaction", "satisfaction_encoded"]])

Expected Output:

text
One-Hot Encoded 'city': satisfaction city_Delhi city_Mumbai city_Pune 0 Low True False False 1 High False True False 2 Medium False False True 3 Medium True False False Label Encoded 'satisfaction' (respecting order): satisfaction satisfaction_encoded 0 Low 0 1 High 2 2 Medium 1 3 Medium 1

10. Code Explanation

  • pd.get_dummies(df, columns=["city"]) creates a new binary column for each unique city value, correctly avoiding any implied ordering among Delhi, Mumbai, and Pune.
  • satisfaction_order = {"Low": 0, "Medium": 1, "High": 2} manually defines the correct order for an ordinal column.
  • df["satisfaction"].map(satisfaction_order) applies this custom mapping, correctly preserving the natural order of the satisfaction levels — notice how this differs from LabelEncoder, which would assign integers alphabetically rather than by true meaning, potentially misordering "High," "Low," and "Medium."
  • This example demonstrates why understanding your data (ordinal vs nominal) matters more than blindly applying a single encoding method everywhere.

11. Advantages

  • Enables categorical (text) data to be used directly in mathematical ML algorithms.
  • One-Hot Encoding avoids introducing false ordinal relationships among nominal categories.
  • Label Encoding (used correctly, for genuinely ordinal data) is simple and memory-efficient.

12. Limitations

  • One-Hot Encoding can create a very large number of new columns if a categorical column has many unique values ("high cardinality"), increasing dataset size and complexity.
  • Label Encoding used incorrectly on nominal data can mislead ML models into assuming a false order or magnitude relationship.
  • Encoding must be applied consistently between training and test data to avoid mismatched columns.

13. Common Mistakes

  • Using Label Encoding on nominal (unordered) categorical data, accidentally implying a false ranking.
  • Forgetting that Scikit-learn's default LabelEncoder assigns integers alphabetically, which may not match the column's true logical order for ordinal data.
  • Applying pd.get_dummies() separately to training and test sets, potentially resulting in mismatched columns if categories differ between the two.
  • Not considering the increased dimensionality caused by One-Hot Encoding a high-cardinality column (e.g., a column with hundreds of unique cities).

14. Best Practices

  • Determine whether each categorical column is ordinal or nominal before choosing an encoding method.
  • For ordinal data, manually define the correct order rather than relying on default alphabetical encoding.
  • For nominal data with many unique categories, consider whether One-Hot Encoding is practical, or whether alternative techniques might be needed.
  • Ensure consistent encoding is applied to both training and test datasets.

15. Real-World Applications

  • Encoding product categories, customer regions, or payment methods in e-commerce datasets.
  • Encoding survey response categories (e.g., satisfaction levels, education levels) that often have a natural order.
  • Preparing categorical medical or demographic data for predictive healthcare models.

16. Interview-Oriented Points

  • Be ready to explain the difference between Label Encoding and One-Hot Encoding, and when to use each.
  • Understand the distinction between ordinal and nominal categorical data.
  • Be able to explain the risk of using Label Encoding on nominal data.

17. Exam-Oriented Points

  • Label Encoding assigns integers to categories; best suited for ordinal (ordered) data.
  • One-Hot Encoding creates separate binary columns per category; best suited for nominal (unordered) data.
  • pd.get_dummies() is a common Pandas tool for One-Hot Encoding.

18. Comparison Table — Label Encoding vs One-Hot Encoding

AspectLabel EncodingOne-Hot Encoding
OutputSingle column of integersMultiple binary (0/1) columns
Best suited forOrdinal data (has a meaningful order)Nominal data (no inherent order)
RiskMay imply false order/magnitude if misusedCan significantly increase dataset dimensionality
Common toolLabelEncoder, .map() with custom orderpd.get_dummies(), OneHotEncoder

19. Quick Revision

  • Encoding converts categorical (text) data into numeric form so ML algorithms can use it.
  • Label Encoding assigns integers to categories — best for genuinely ordinal (ordered) data.
  • One-Hot Encoding creates separate binary columns per category — best for nominal (unordered) data.
  • Misapplying Label Encoding to nominal data can falsely suggest an order or magnitude that doesn't exist.

Mock Test

  • Encoding Categorical Data — Quick Test

    A 10-question multiple-choice check on Encoding Categorical Data.

    10 questions · 10 min · Easy
    Start Mock Test

Coding Problems

  • Problem 1: Apply One-Hot Encoding to a Column
    Easy · python
    Solve Problem
  • Problem 2: Apply Label Encoding with a Custom Order
    Easy · python
    Solve Problem
  • Problem 3: Encode Multiple Categorical Columns
    Easy · python
    Solve Problem
  • Problem 4: Compare Original and Encoded Data
    Easy · python
    Solve Problem