Part 05 / Topic 3 of 7
Encoding categorical data
Models need numbers; contract is text. One-hot encoding gives each category its own 0/1 column:
enc = OneHotEncoder(handle_unknown="ignore", sparse_output=False)
enc.fit([["month-to-month"], ["one-year"], ["two-year"]])
enc.transform([["one-year"]]) # [0.0, 1.0, 0.0]
enc.transform([["weekly"]]) # [0.0, 0.0, 0.0] — unseen category: all zeros
handle_unknown="ignore" is the production-safety line: a category that never appeared in training encodes as all zeros instead of crashing the service the day marketing invents a "weekly" plan.
🧪 Quick quiz
- What does
OneHotEncoder(handle_unknown="ignore")do with a category it never saw in training?
Show the answer
Encodes it as all zeros — keeping production predictions working when a new category appears.
Done here? The practice set for Part 5 — 4 exercises with solutions — is on the part page.