Stop 06 · Data Preparation & Feature EngineeringTopics · 4/10

Encoding Categorical Variables

Part of the Machine Learning Roadmap

In short

Models only understand numbers, but your data says 'pune', 'delhi' and 'premium'. How you turn words into numbers can quietly help or wreck a model.

Models need numbers, so text categories must be encoded: one-hot encoding makes a column per category, while ordinal encoding maps ordered categories like low, medium, high to numbers. The right encoding depends on whether the categories have an order.

The mascot ticks one of three boxes on a slip next to a cup of chai, a coffee mug and a green tea cup.

Think of it like this

The office pantry order slip

The pantry slip has three tick boxes: chai, coffee, green tea. You tick one. Nobody writes 'chai = 1, coffee = 2', because coffee isn't 'more' than chai. One-hot encoding is that tick-box slip, one box per option.

Watch it happen

Turning a city column into numbers

pune
0
delhi
1
pune
2
goa
3

1/4A text column. The model can't do maths on words.

The big ideas

What you need to know

One-hot for no order

Cities, colours, payment methods. One 0/1 column per value, no fake ranking.

Ordinal for real order

Sizes S < M < L, or education levels. Map them to 0, 1, 2 so the order is kept.

Many values need care

A column with 5,000 pin codes makes 5,000 columns. Group rare values or try target or frequency encoding.

Plan for new values

Test data may contain a city training never saw. Set handle_unknown='ignore' so it doesn't crash.

See it in code

Walk through the code

python
1import pandas as pd2from sklearn.preprocessing import OneHotEncoder3 4df = pd.DataFrame({"city": ["pune", "delhi", "pune", "goa"],5                   "size": ["S", "L", "M", "S"]})6 7df["size_code"] = df["size"].map({"S": 0, "M": 1, "L": 2})   # real order8 9enc = OneHotEncoder(handle_unknown="ignore", sparse_output=False)10city = enc.fit_transform(df[["city"]])11print(enc.get_feature_names_out())   # ['city_delhi' 'city_goa' 'city_pune']12print(city)

1/4Two text columns: city (no order) and size (has an order).

Ordinal mapping for size, which has a real order, and one-hot encoding for city, which doesn't.

Save yourself the pain

Common mistakes

Oops

Label-encoding cities and feeding them to linear regression.

Do this instead

Use one-hot for unordered categories in linear models. Tree models cope better with label codes.

Oops

Using pd.get_dummies separately on train and test.

Do this instead

Columns may not match. Fit one OneHotEncoder on train and reuse it on test.

Your mission

Encode a real column

~20 min

Quick check

A column 'payment_method' has UPI, card and cash. Which encoding fits best for logistic regression?

Go deeper

  1. 1One-hot encode a city column with OneHotEncoder(handle_unknown='ignore').
  2. 2Use OrdinalEncoder for an education-level column with an explicit category order.
  3. 3Watch out for columns with hundreds of categories, which explode in size when one-hot encoded.

Also part of these roadmaps

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge