Skip to content
Preptima
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

Further reading