Turning Categories Into Numbers
area and furnished are still text
Back in lesson 2.4, when you built X for the first time, you left out area and furnished on purpose: "models only understand numbers." That was true then and it is still true now, missing values or not. Before either column can go anywhere near scikit-learn, "Baner" and "yes" have to become numbers, and how you do that turns out to matter as much as whether you do it.
The idea
Turning a category into a number sounds like it should have one obvious answer: just count 0, 1, 2, 3 and move on. The problem is that a number carries meaning a category does not. 3 is bigger than 1. If you encode Baner as 0 and Wakad as 5, you have just told the model that Wakad is five times something Baner is once of, which is nonsense, area has no natural order. Get this wrong and the model spends its effort learning a pattern that only exists because of how you numbered things, not because it is real.
There are three honest ways to do this, and which one is right depends entirely on whether the category actually has an order.
When there is no real order: one-hot encoding
area has six values, Baner, Hadapsar, Hinjewadi, Kothrud, Viman Nagar, Wakad, and none of them is "more" than another. The fix: instead of one column with six possible numbers, make six columns, one per area, each holding True or False for whether that row is in that area.
1import pandas as pd
2
3df = pd.read_csv("https://cod-e.vercel.app/data/pune-flats.csv")
4pd.get_dummies(df["area"], prefix="area").head(3)area_Baner area_Hadapsar area_Hinjewadi area_Kothrud area_Viman Nagar area_Wakad 0 False True False False False False 1 False False True False False False 2 True False False False False False
Row 0 was Hadapsar, so only area_Hadapsar is True. This is one-hot encoding: "hot" meaning on, and only one column is ever on per row. No column is bigger or smaller than another, so there is no false order to learn.
The cost is more columns. One text column became six. Glue them onto the table and drop the original:
1area_dummies = pd.get_dummies(df["area"], prefix="area")
2df_encoded = pd.concat([df.drop(columns=["area"]), area_dummies], axis=1)
3df_encoded.shape(21, 11)
Six columns became eleven: the original five (bhk, size_sqft, floor, furnished, rent) plus six new area columns. That is fine for six categories. If area had five hundred possible values, one-hot encoding would create five hundred columns, mostly False, and that becomes its own problem, more memory, slower training, and a model that struggles to learn much from any single rare category. Watch for that when a categorical column has dozens or hundreds of unique values, not just a handful.
Tip:For linear models specifically, drop one of the dummy columns with pd.get_dummies(df["area"], prefix="area", drop_first=True). If you keep all six, a row where all six are False is impossible, since every flat is in exactly one area, so the columns are not fully independent, which confuses some linear models. Dropping one leaves five columns, and "all five are False" now correctly means the dropped area.
When there are only two options: label encoding
furnished only ever says "yes" or "no". One-hot encoding it would make two columns that always disagree, furnished_no is True exactly when furnished_yes is False. That is wasted space for no benefit. For exactly two categories, one column of 0s and 1s carries the same information:
1df["furnished_num"] = df["furnished"].map({"no": 0, "yes": 1})
2df[["furnished", "furnished_num"]].head(4)furnished furnished_num 0 no 0 1 no 0 2 yes 1 3 yes 1
This is label encoding: each category gets one whole number. It works cleanly here because there is no order problem to create, "yes" is not more than "no", there are simply two states, and 0/1 says exactly that. scikit-learn also has LabelEncoder for this, but a plain .map() is just as correct and lets you see exactly which number means what.
When there is a real order: ordinal encoding
Sometimes a category genuinely is ordered, and pretending otherwise throws away real information. Say you bucket each flat's size_sqft into Small, Medium and Large, the same kind of new column lesson 3.6 will show you how to build:
1df["size_category"] = pd.cut(
2 df["size_sqft"],
3 bins=[0, 800, 1200, float("inf")],
4 labels=["Small", "Medium", "Large"],
5)
6df["size_category"].head(5)0 Large 1 Small 2 Medium 3 Small 4 Small Name: size_category, dtype: category Categories (3, object): ['Small' < 'Medium' < 'Large']
Unlike area, Small, Medium and Large really do have an order, Large genuinely is more than Medium. Here, encoding it as 0, 1, 2 does not invent a false pattern, it captures a real one:
1size_order = {"Small": 0, "Medium": 1, "Large": 2}
2df["size_category_num"] = df["size_category"].map(size_order)
3df["size_category_num"].head(5)0 2 1 0 2 1 3 0 4 0 Name: size_category_num, dtype: int64
This is ordinal encoding: numbers that carry a genuine order but are not meant to be added or averaged the way real numbers are. 2 minus 0 does not mean "twice as large" the way it would for size_sqft itself; it just means "two steps further along the scale."
The one rule that decides which technique to use
Key Points
- No real order, more than two categories: one-hot encoding. Example: area.
- Exactly two categories: label encoding with a single 0/1 column. Example: furnished.
- A genuine order, three or more categories: ordinal encoding, numbers that follow that order. Example: size_category.
Every mistake in this lesson comes from picking the wrong one of these three, almost always by label-encoding a category, like area, that has no real order.
Where people get confused
Key Points
- "I will just label-encode everything, it is simpler." Label-encoding area as 0 to 5 tells the model Wakad (5) is five times Hadapsar (0). That false order actively misleads models that use numeric distance, like linear regression or KNN. Trees can sometimes shrug it off; do not rely on that.
- "One-hot encoding always makes too many columns." Only for high-cardinality columns, dozens or hundreds of categories. Six categories, like area here, is completely normal.
- "Ordinal encoding is the same as label encoding." The numbers are the same shape, but the intent is different: ordinal numbers are meant to preserve a real order, label-encoded numbers on an unordered category are just arbitrary IDs that happen to be numbers.
Quick check
You have a "size" column with values S, M, L, XL. Which encoding fits, and why?
Ordinal encoding, mapped in order, e.g. S=0, M=1, L=2, XL=3, because the sizes have a genuine order and that order is worth keeping.
Why is it wrong to one-hot encode a "payment_method" column that has 400 different values?
It would create 400 mostly-False columns, ballooning the size of the dataset and giving the model very little data per category to actually learn from.
What specifically goes wrong if you label-encode area as Baner=0, Hadapsar=1, ... Wakad=5 and feed it straight into linear regression?
The model treats the difference between areas as a real numeric distance, effectively assuming areas further down the list are "more" of something, when area has no natural order at all.
Recap
Key Points
- Models only understand numbers, so every text column needs encoding before training.
- One-hot encoding (pd.get_dummies) fits categories with no order and a manageable number of them.
- Label encoding fits a category with exactly two values, one 0/1 column, no order implied.
- Ordinal encoding fits a category with a real order, and the numbers you choose should follow that order.
- Picking the wrong one does not just look untidy, it actively teaches the model a pattern that is not real.
Every column is finally numeric. But look at rent (in the tens of thousands) next to furnished_num (0 or 1), those scales are wildly different, and some models care about that a lot. Scaling is next.
Theia
Can you explain this concept?
Practise Turning Categories Into Numbers with realistic interview questions. Choose spoken or typed answers, get feedback on your reasoning, then return to the material that needs work.
Sign in to practise this conceptThe session stays focused on this lesson. Your selected interview level sets the bar.