Encoding Categorical Features
Encoding Categorical Features — Turn categories into numbers models can use.
Models eat numbers, not words. Encoding turns categories like “red, green, blue” into numeric form — but the wrong encoding can invent a fake ordering that misleads the model.
Design matrix — 8 × 1
- Apple
- Apple
- Banana
- Banana
- Cherry
- Cherry
- Date
- Date
Model error by encoding (click one to use it)
Encoding controls
The idea in plain words
Models eat numbers, not words. Encoding turns categories like “Red, Green, Blue” into numeric form — but the wrong choice invents structure that isn’t there. Label encoding assigns 0, 1, 2, which tells the model the categories are ordered and evenly spaced.
One-hot encoding instead gives each category its own binary column, so every pair is equally distant. For unordered categories that’s the honest representation.
Now, the math
Under label encoding the model reads a false distance and ordering:
One-hot makes every distinct pair equidistant:
▸ Show the derivation
A linear model multiplies the encoded value by a weight, so label codes force the effect of “Blue” to be exactly twice that of “Green.” One-hot lets each category get its own independent weight, removing the artificial order — at the cost of one extra column per category.
Trace it by hand
Four data rows with one categorical feature, city, taking three values: Delhi, Paris, Tokyo. Every number below is exact — no rounding involved.
Step 1 — the raw column
| row | city |
|---|---|
| 1 | Delhi |
| 2 | Paris |
| 3 | Tokyo |
| 4 | Delhi |
A model can’t multiply “Tokyo” by a weight, so the strings must become numbers.
Step 2 — label encoding invents an order
Assign Delhi = 0, Paris = 1, Tokyo = 2. The codes immediately claim Tokyo is “twice” Paris and that Tokyo is farther from Delhi than Paris is:
A linear model multiplies the code by one weight w, so Tokyo’s effect is forced to be exactly 2w — double Paris’s w — purely because of an arbitrary alphabetical numbering. city = 2 > city = 1 is a fake ordering.
Step 3 — one-hot encoding: one column per category
| row | city_Delhi | city_Paris | city_Tokyo |
|---|---|---|---|
| 1 | 1 | 0 | 0 |
| 2 | 0 | 1 | 0 |
| 3 | 0 | 0 | 1 |
| 4 | 1 | 0 | 0 |
Now every pair of distinct cities sits at the same distance √2 and gets its own independent weight. Drop-first removes one redundant column: if city_Paris and city_Tokyo are both 0, the row must be Delhi.
What just happened: the same four rows produced two different geometries. Label encoding smuggled in the claim Tokyo > Paris > Delhi with Tokyo twice as far from Delhi; one-hot spent k = 3 columns to make every city equidistant and independently weighted — the honest choice for unordered categories.
Now Break It
Try this: Label encoding unordered categories invents a fake numeric order the model treats as meaningful.
Control: Encoding selector (set to label encoding)
What happens: Fake ordering! Label encoding tells the model red < green < blue — an order that doesn’t exist.
Where encoding categorical features is used
Encoding categorical features turns labels like country, product category, or browser type into numbers a model can process. One-hot encoding is the safe default for nominal categories with no inherent order: a color feature becomes separate red, green, and blue indicator columns, so the model never assumes green is somehow between red and blue. For high-cardinality columns such as zip code or user id, one-hot encoding explodes the feature count, so practitioners turn to alternatives like target encoding, hashing, or learned embeddings. Recommendation systems and large-scale ad-click models routinely embed millions of categorical ids into dense vectors, which is far more compact and lets the model learn similarity between categories.
The classic mistake is applying integer label encoding to unordered categories and feeding them to a linear model or distance-based method. Mapping cat to 1, dog to 2, and bird to 3 falsely tells the model that bird is three times cat and that dog sits between them, injecting a false ordering. Label encoding is appropriate for genuinely ordinal features like small, medium, large, and it is also fine for tree models that only compare thresholds. A second pitfall is target encoding computed on the full dataset, which leaks the label into the feature; it must be fit inside cross-validation folds. Finally, always plan for categories in the test set that never appeared in training so the pipeline does not crash on unseen values.
Frequently asked questions
When should I use one-hot encoding versus label encoding?
How do I handle categorical features with very many unique values?
What is target encoding and why is it risky?
What happens when a category appears in the test set but not in training?
Does one-hot encoding cause any problems for linear models?
Written & reviewed by the ML Visualization team · Last updated .