When machine learning algorithms receive categorical data like "Red", "Blue", "Green" or "Dog", "Cat", "Bird", they cannot process text directly. They need numbers. But how we convert categories to numbers drastically affects model performance. Two fundamental approaches exist: label encoding (assigning integers) and one-hot encoding (creating binary indicator columns).
The critical question: Why does the conversion method matter? Because most ML algorithms interpret numbers as having magnitude and order. If we naively assign Red=0, Blue=1, Green=2, the model thinks Green > Blue > Red mathematically, creating spurious relationships.
Imagine you're sorting your Pokemon cards.
Label encoding is like giving them numbers based on how strong they are: Pikachu=1, Charizard=2, Mewtwo=3. The numbers mean something—higher number = stronger Pokemon.
One-hot encoding is like having separate piles: one pile for Fire type, one for Water type, one for Electric type. Each card goes in exactly one pile. The piles don't have an order—Fire isn't "bigger" than Water, they're just different types.
If you tell a computer "Pikachu=1, Charizard=2", it might think "Charizard is twice as much as Pikachu!" That's silly for things that don't have order. So for types (Fire, Water, Electric), we use the separate piles method instead. Each Pokemon gets a "yes" in one pile and "no" in all the others: [Fire: yes, Water: no, Electric: no] for Charizard.
Regularization: L1/L2 regularization helps with one-hot encoded high-cardinality features
Target Encoding: Alternative for high-cardinality nominal variables
Decision Trees: Handle label encoding naturally through recursive partitioning
Linear Regression: Requires one-hot for nominal, avoid dummy variable trap
Feature Engineering: Encoding is the first step in categorical feature engineering
#flashcards/ai-ml
What is label encoding? :: Mapping each unique category to a distinct integer (e.g., Red=0, Blue=1, Green=2). Creates a single integer column.
What is one-hot encoding?
Creating n binary columns for n categories, where exactly one column is 1 and others are 0 for each sample. Also called dummy variable encoding.
When should you use label encoding?
For ordinal categories with natural order (e.g., education level: High School < Bachelor's < Master's), or with tree-based models even for nominal data.
When should you use one-hot encoding?
For nominal categories without natural order (e.g., country, color) especially with linear or distance-based models.
Why is label encoding dangerous for nominal data with linear models?
The model interprets integers as having magnitude/order. If USA=0, India=1, Brazil=2, the model thinks Brazil = 2×India or that Brazil and India are more similar than USA and India, which is nonsense.
What is the dummy variable trap?
Including all n one-hot encoded columns creates perfect multicollinearity (columns sum to 1), making the design matrix singular in linear regression. Drop one column as reference.
Why are one-hot encoded categories equidistant?
Each category is a standard basis vector. Distance between any two: ||e_i - e_j||₂ = √2 for all i≠j. All categories are equally "far apart" geometrically.
What is the solution for high cardinality nominal variables?
Use target encoding (mean of target per category), frequency encoding, embedings, or group categories into broader classes. One-hot encoding creates too many dimensions.
Why do tree-based models work with label encoding even for nominal data?
Trees split on thresholds (if encoded_value ≤ 2.5) through recursive partitioning. They don't assume linear relationships between encoded integers and target.
In one-hot encoding with n categories, how many columns should linear regression use?
n-1 columns. Drop one as reference level to avoid multicollinearity. The dropped category effect is absorbed into the intercept.
What is ordinal data?
Categorical data with natural order/ranking (e.g., T-shirt sizes S< M < L < XL, or ratings Poor < Fair < Good < Excellent).
What is nominal data?
Categorical data without natural order (e.g., colors, countries, product names). Categories are just different labels.
Jab machine learning models ko categorical dataena ho jaise "Red", "Blue" ya "Dog", "Cat", toh directly text nahi samajh sakte. Unko numbers chahiye. Lekin yeh conversion kaisearo, yeh bohot matter karta hai! Do main tarike hain:
Label encoding mein har category ko ek integer assign karte hain—Red=0, Blue=1, Green=2. Yeh tab thek hai jab categories mein natural order ho, jaise T-shirt size (Small < Medium < Large). Lekin agar order nahi hai, toh model samajhega ki Green > Blue > Red mathematically, jo galat hai! Country names ke liye agar USA=0, India=1, Brazil=2 karo, toh model sochega Brazil aur India "zyada similar" hain USA ke comparison mein, jo nonsense hai.
One-hot encoding mein har category ke liye alag binary column banate hain. USA = [1,0,0], India = [0,1,0], Brazil = [0,0,1]. Ab sab countries equal distance pe hain geometrically (√2 distance), koi spurious ordering nahi hai! Linear models ke liye yeh zaroori hai. Lekin ek trick hai—agar 3 categories hain toh sirf 2 columns use karo (ek drop kar do), warna "dummy variable trap" mein fas jaoge jahan matrix invert nahi hogi.
Rule simple hai: agar order hai toh label encoding, agar sirf different categories hain (nominal) toh one-hot encoding. Tree-based models (Random Forest, XGBoost) label encoding handle karlete hain kyunki woh thresholds pe split karte hain, lekin linear regression ya KNN ke sath one-hot mandatory hai nominal data ke liye. High cardinality (bohot zyada categories) mein one-hot memory kharab karega, tab target encoding ya embedings use karo.