Encoding Categorical Variables
Converting non-numeric strings and categories into model-ready numerical matrices without introducing false ordinal rankings.
Categorical Encoding Decision Tree
Is the Categorical Feature Ordered (Ordinal)?
/ \
Yes No
/ \
Use Ordinal Encoding Is Cardinality High (> 15 values)?
('Small'=0, 'Med'=1, 'Large'=2) / \
No Yes
/ \
Use One-Hot Encoding Target / Frequency Encoding
([1,0,0], [0,1,0]) / Entity Embeddings
Encoding Methods Comparison
| Encoding Method | Mechanics | Best For | Output Dim | Primary Risk |
|---|---|---|---|---|
| One-Hot | Binary 1/0 column per category | Low-cardinality nominal data | $C$ columns | Sparse matrix explosion for large $C$ |
| Ordinal | Assigns integer 0 to $C-1$ | Ordered categories (Education, Size) | 1 column | Imposes false distance on nominal data |
| Frequency / Count | Replaces category with frequency count | Medium/High cardinality features | 1 column | Collapses categories with identical counts |
| Target Encoding | Replaces category with target mean $\bar{y}_c$ | High-cardinality features in GBDTs | 1 column | Target Leakage / Overfitting |
| Entity Embeddings | Learned dense vector via Neural Net | High-cardinality features in Deep Learning | $k$-dim vector | Requires neural net training step |
The Dummy Variable Trap in Linear Regression
When using One-Hot encoding with a bias intercept in Linear Regression:
$$\sum_{j=1}^C x_{\text{onehot}, j} = 1.0 = \text{Bias Intercept Vector}$$
This creates perfect multicollinearity ($\text{rank}(X) < d$). Invertibility fails for $(X^T X)^{-1}$.
Fix: Drop one binary column (Dummy Variable Encoding), retaining $C-1$ columns. The baseline dropped category is absorbed into the bias intercept $b$.
Say this out loud
"Categorical encoding converts discrete strings into model inputs. We use Ordinal Encoding only when natural ordering exists (e.g. education level). For un-ordered categories, we use One-Hot Encoding for low-cardinality features, dropping one column for linear models to avoid the dummy variable trap. For high-cardinality features like ZIP codes, we use Out-of-Fold Target Encoding or learned Entity Embeddings."
Follow-ups to expect
- How does Hashing Trick (FeatureHasher) work? Maps high-cardinality categorical strings to a fixed number of $N$ buckets using a hash function
hash(string) % N. Eliminates dictionary lookups and handles unseen categories, at the cost of potential hash collisions. - How does CatBoost handle categoricals natively? CatBoost computes online target statistics on-the-fly over random permutations of training data, preventing target leakage without requiring manual pre-encoding.
Check yourself
Why is applying Ordinal Encoding (mapping 'Red'=0, 'Green'=1, 'Blue'=2) dangerous for linear models and neural networks?