ML Notes
Complete guide to encoding categorical variables for ML including one-hot encoding, label encoding, target encoding, and handling high-cardinality features.
Machine learning algorithms work with numbers, not text. Categorical features like "color", "country", or "product_type" must be converted to numerical representations. The encoding method you choose can significantly impact model performance.
Types of Categorical Variables
| │ ├── Color | red, blue, green |
| │ ├── Country | USA, UK, India |
| │ └── Gender | male, female, other |
| ├── Education | high school < bachelor < master < PhD |
| ├── Size | S < M < L < XL |
| └── Rating | poor < average < good < excellent |
Label Encoding
Assigns integers to categories. Use ONLY for ordinal data or tree-based models.
One-Hot Encoding
Creates binary columns for each category. The standard for nominal data.
Target Encoding (Mean Encoding)
Replaces categories with the mean of the target variable. Powerful but risky (can overfit).
Handling High Cardinality
When a feature has many unique values (e.g., zip codes, product IDs):
Encoding Comparison
| Method | Best For | Pros | Cons |
|---|---|---|---|
| One-Hot | Nominal, low cardinality | No false ordering | Many columns, sparse |
| Label/Ordinal | Ordinal features, trees | Compact, single column | False ordering for nominal |
| Target | High cardinality | Compact, powerful | Overfitting risk |
| Frequency | High cardinality | Simple, no overfitting | Loses category meaning |
| Binary | Moderate cardinality | Compact | Less interpretable |
Interview Questions
- Why can't you use label encoding for nominal categorical features with linear models?
Label encoding assigns arbitrary integers (red=0, blue=1, green=2), which implies a mathematical relationship (blue > red, green > blue). Linear models would learn false patterns from this ordering.
- What is the dummy variable trap?
If you one-hot encode without dropping one category, you create perfect multicollinearity (columns sum to 1). Linear models can't handle this. Solution: use drop_first=True.
- How do you handle a categorical feature with 10,000 unique values?
Options: target encoding with smoothing, frequency encoding, embedding layers (for neural nets), hashing trick, or grouping rare categories into "Other".
- When would you use target encoding over one-hot encoding?
When cardinality is high (>20 categories) and you need a compact representation. Always use with cross-validation to prevent overfitting.
- How do you handle unseen categories in production?
Set handle_unknown='ignore' in OneHotEncoder (encodes as all zeros), use a fallback value for target encoding, or map to "Other" category.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Encoding Categorical Data.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Machine Learning topic.
Search Terms
machine-learning, machine learning, machine, learning, data, preprocessing, encoding, categorical
Related Machine Learning Topics