Data & Feature Engineering

High-Cardinality Categoricals

Encoding categorical columns with thousands or millions of unique values without exploding memory or causing data leakage.

🟡 intermediate5 min readfeatures
High-Cardinality Categorical Features (e.g. User ID, IP Address, Merchant ID, ZIP Code) contain thousands to millions of distinct categories. Naive One-Hot Encoding causes extreme sparse matrix memory explosion and weakens decision tree split power. Modern solutions include Out-of-Fold Target Encoding with Bayesian smoothing, Frequency Encoding, Hashing Trick (FeatureHasher), and Neural Entity Embeddings (Guo & Berkhahn, 2016).

The High-Cardinality Challenge

When category count $C > 1,000$ (e.g. IP Address, User ID, ZIP Code):

High-Cardinality Toolkit

                             High-Cardinality Strategies
    ┌───────────────────────┬───────────────────────┬───────────────────────┐
    ▼                       ▼                       ▼                       ▼
Out-of-Fold Target     Frequency / Count     Hashing Trick         Entity Embeddings
     Encoding              Encoding         (FeatureHasher)          (Neural Nets)
Compresses C -> 1     Compresses C -> 1     Fixed N buckets       Compresses C -> k
(Must use smoothing!) (Simple & robust)     (Collision risk)      (Learns semantics)

Out-of-Fold Target Encoding with Bayesian Smoothing

For category $c$:

$$S_c = \frac{n_c \cdot \bar{y}c + m \cdot \bar{y}{\text{global}}}{n_c + m}$$

Mandatory Leakage Prevention (Out-of-Fold Split)

Never compute $\bar{y}_c$ on the full dataset! Compute target encoding means strictly within K-Fold cross-validation folds:

  Fold 1 (Val) ──► Encoded using target means from [Fold 2 + Fold 3 + Fold 4 + Fold 5]
  Fold 2 (Val) ──► Encoded using target means from [Fold 1 + Fold 3 + Fold 4 + Fold 5]

Entity Embeddings (Guo & Berkhahn, 2016)

Pass categorical integer IDs into a trainable embedding table:

$$E \in \mathbb{R}^{C \times k}, \quad \text{where Rule of Thumb } k = \min(50, \lfloor C^{0.5} \rfloor)$$

Category ID: 4021 (ZIP Code) ──► [ Embedding Lookup Table ] ──► Dense Vector [e1, e2, ..., e16]

Key Insight: Entity embeddings map discrete categories into continuous vector spaces where semantically similar categories (e.g. adjacent ZIP codes or similar merchants) cluster together naturally.

Say this out loud

"High-cardinality features contain thousands of unique categories, making One-Hot Encoding memory-prohibitive. For GBDTs, we use Out-of-Fold Target Encoding with Bayesian smoothing to shrink rare category means toward global averages without target leakage. For deep neural networks, we use Entity Embedding layers to map category IDs into learned dense k-dimensional vectors."

Follow-ups to expect

Check yourself

Question 1 of 3

Why does One-Hot Encoding high-cardinality features like Merchant ID (500,000 unique values) cripple Gradient Boosted Decision Trees (GBDT)?

More in Data & Feature Engineering

See all →
Feature Engineering Fundamentals4 minSQL Questions in ML Interviews5 minEncoding Categorical Variables4 min