Working with Data
Categorical Data
8 min+75 XPStep 1/11
Categories aren't numbers
A color column holding red, green, and blue can't go into a model as text. But the obvious fix — red=1, green=2, blue=3 — is worse: it invents an ordering and a distance ('blue is bigger than green') that don't exist.
One-hot encoding gives each category its own dimension instead:
- red → [1, 0, 0]
- green → [0, 1, 0]
- blue → [0, 0, 1]
Now every category is equidistant from every other — no fake order, no fake distances.
warning
ZIP code 90210 is a label, not a quantity — it isn't 'larger' than 02134. Any number that names a thing rather than measures it is categorical.