mediumConceptCodingMidSenior
One of your features is a postcode with about 40,000 distinct values. How do you encode it?
5 min readUpdated 2026-08-15
Answer
Short answer
One-hot is unusable at that width, so the options are frequency encoding, smoothed target encoding, hashing, or a library's native categorical handling. Target encoding is the strongest and the most dangerous, because it must be fitted inside each fold or it leaks the label.
Was this answer helpful?
Likely follow-ups
- What do you do with a postcode that appears for the first time in production?
- Why does target encoding leak even when you compute it only on the training split?
- When is hashing preferable to target encoding, and what do you lose?
- The postcode is geographic. What better features could you derive from it?
Related questions
- A churn model scores 0.94 AUC offline and barely beats the baseline in production. One of its strongest features is populated by a process that runs after the customer has already churned. Explain what happened and how you would fix it.mediumAlso on data-leakage and feature-engineering5 min
- What is target leakage, and how would you catch it before the model ships?mediumAlso on data-leakage and feature-engineering5 min
- What does point-in-time correctness mean, and how does a naive join break it?hardAlso on data-leakage5 min
- How would you design a validation strategy that respects the structure of the data you have?hardAlso on data-leakage5 min