How would you encode merchant ID (2 million values) for a fraud model, and what are the leakage risks?
Answer
Model answer, written in a candidate's voice
In short
- One-hot is infeasible at 2M values; use hashing, frequency, embeddings, or target/mean encoding depending on model.
- Compute target/mean encoding out-of-fold or on past-only windows, smoothed towards the global rate.
- Keep an explicit unseen/rare bucket so new or low-volume merchants do not get noisy or missing encodings.
- Use time-aware aggregates such as merchant fraud rate and volume over the last 30 days, computed only from data available at prediction time.
- Leakage risks: full-sample target encoding, future-window aggregates, current-transaction inclusion, non-time-aware splits, hashing collisions, and rare-merchant overfitting.
In full
I would not one-hot 2 million merchant IDs; I would use a model-appropriate encoding: feature hashing for a simple high-cardinality categorical, frequency encoding for volume/popularity, learned embeddings for neural models, and smoothed target/mean encoding for tree-based fraud models. The key is to compute the merchant fraud rate out-of-fold or strictly on past-only windows, such as the last 30 days, and shrink it towards the global fraud rate so low-volume merchants do not get extreme values. I would also keep an explicit unseen/rare bucket for merchants with no or too little history, and make sure the same encoding is applied at training and serving time. The main leakage risks are using the full sample for target encoding, including the current transaction or future outcomes in the 30-day aggregate, splitting data randomly instead of by time, and letting rare merchants or hash collisions carry misleading signal. In practice I would validate with a time-based holdout and compare encodings by lift/calibration, not just AUC.
What a strong answer covers
- One-hot is infeasible. Options: feature hashing (collisions), frequency encoding, learned embeddings.
- Target or mean encoding computed out-of-fold or on past-only windows, with smoothing.
- An unseen/rare bucket.
- Time-aware aggregates (merchant fraud rate over the last 30 days).
Common mistakes
- target encoding on the full training set (leakage)
- no strategy for unseen merchants at serving.