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
- Label Encoding
- One-Hot Encoding
- Ordinal vs Nominal categorical data
pd.get_dummies()and Scikit-learn'sOneHotEncoder/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
- Identify which columns in your dataset are categorical (text-based).
- Determine whether each column is ordinal (has meaningful order) or nominal (no inherent order).
- Apply Label Encoding for ordinal columns, and One-Hot Encoding for nominal columns.
- 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
pythonimport 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:
textOne-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 fromLabelEncoder, 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
LabelEncoderassigns 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
| Aspect | Label Encoding | One-Hot Encoding |
|---|---|---|
| Output | Single column of integers | Multiple binary (0/1) columns |
| Best suited for | Ordinal data (has a meaningful order) | Nominal data (no inherent order) |
| Risk | May imply false order/magnitude if misused | Can significantly increase dataset dimensionality |
| Common tool | LabelEncoder, .map() with custom order | pd.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.