Quant Memo
Foundational

Ordinal and Label Encoding Pitfalls

Encoding categories as plain integers can accidentally tell a model that categories have an order or distance relationship that doesn't actually exist, silently biasing predictions.

Label encoding assigns each category in a categorical column a distinct integer — say, "red" → 0, "blue" → 1, "green" → 2 — just so the data can be fed into a model that only accepts numbers. The pitfall: most models built on linear structure (linear/logistic regression, distance-based methods like k-NN, and neural networks with plain dense layers) will interpret those integers literally, assuming "green" is somehow "greater than" or "more" than "red," and that the distance between blue and green (1 unit) means something similar to the distance between red and blue.

This works fine when the categories genuinely are ordered — encoding "small/medium/large" as 0/1/2 correctly captures real ordinal structure, so it's called ordinal encoding in that case. It becomes a bug when applied to unordered (nominal) categories like color, country, or product ID, where any assigned order is arbitrary and the model will still try to learn from it, producing spurious relationships.

Integer-encode a category only if a genuine order exists — for unordered categories, integer codes invent a fake ranking that models built on distance or linear structure will wrongly try to exploit.

Worked example

A dataset has a "city" column encoded as New York = 0, Chicago = 1, Miami = 2. A linear model fit on this data might learn a coefficient implying that "more Miami-ness" predicts higher sales — a meaningless artifact of assignment order, not a real signal. Tree-based models (like gradient-boosted trees) are largely immune to this specific issue, since they split on thresholds rather than treating the numeric distance as meaningful, but for anything else, one-hot or target encoding is the safer default for nominal categories.

Related concepts

Practice in interviews

Further reading

  • Kuhn & Johnson, Feature Engineering and Selection (ch. on categorical predictors)
ShareTwitterLinkedIn