High-Cardinality Categoricals

What to do with a column that has a million different values.

🟡 intermediate5 min readfeatures
Some categories have huge numbers of possible values, like user ID, postcode or merchant. One-hot encoding them creates millions of mostly empty columns, which wastes memory and makes trees work poorly. Better options include target encoding, which replaces each category with its average outcome, worked out on held-out data so it does not leak the answer. You can also use how often each category appears, hash categories into a fixed number of buckets, or learn a compact embedding for each one.

The High-Cardinality Challenge

When category count C>1,000C > 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 cc:

Sc=nc⋅yˉc+m⋅yˉglobalnc+mS_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 yˉc\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∈RC×k,where Rule of Thumb k=min⁡(50,⌊C0.5⌋)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