Feature Engineering
You have already done this twice
Lesson 2.4 created rent_per_sqft by dividing rent by size_sqft. Lesson 3.4 created size_category by bucketing size_sqft into Small, Medium and Large. Neither column existed in the original data. Both were built out of columns that did exist, using a bit of judgement about what would actually help a model, or a person, understand the flats better. That is feature engineering, and this lesson is about doing it on purpose instead of stumbling into it.
The idea
A model can only find patterns that are visible in the columns you give it. Sometimes a pattern is sitting right there in two existing columns, just divided, multiplied or combined the right way, and the model would have to work much harder to find it on its own, if it ever does. Feature engineering is using what you know about the problem to hand the model a more useful version of the data.
rent_per_sqft is the clearest example. rent alone mixes together how big a flat is and how expensive its location is. Divide by size_sqft and you get something closer to pure "how expensive is this location", which is exactly the pattern you saw in lesson 2.4: furnished flats cost noticeably more per square foot, a fact that was buried inside raw rent and size_sqft separately.
Building a new feature: size per bedroom
A 1,000 square foot 1BHK and a 1,000 square foot 3BHK are very different flats to live in, even with identical size_sqft. Dividing size by bhk gives you a feel for how roomy each bedroom actually is:
1import pandas as pd
2
3df = pd.read_csv("https://cod-e.vercel.app/data/pune-flats.csv")
4df["size_per_bhk"] = (df["size_sqft"] / df["bhk"]).round(1)
5df[["bhk", "size_sqft", "size_per_bhk"]].head(5)bhk size_sqft size_per_bhk 0 3 1275 425.0 1 1 675 675.0 2 2 875 437.5 3 1 525 525.0 4 1 625 625.0
Row 0 and row 2 have fairly close size_sqft, 1,275 against 875, but row 0 splits that across three bedrooms while row 2 splits it across two, so their size_per_bhk tells a more honest story about how spacious each individual room feels, something size_sqft and bhk separately never say directly.
Building a new feature: bucketing into categories
size_category from lesson 3.4 is a different kind of engineering: turning a continuous number into a small number of meaningful groups. This is called binning or discretisation, useful when the exact number matters less than which rough tier something falls into.
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"].value_counts()size_category Medium 10 Small 7 Large 4 Name: count, dtype: int64
Ten flats are Medium, seven Small, four Large. A model can sometimes find this kind of "roughly which tier" pattern on its own, but handing it over directly, especially to a simpler model, often helps, and it makes your own analysis easier to read too: "furnished Large flats" is a sentence a person can reason about; "furnished flats with size_sqft above 1247.3" is not.
A binary flag, built from a rule you know
Sometimes the useful new feature is a simple yes/no question you already know the answer to. Say, from experience, ground floor flats (floor 0 or 1) rent for less because people prefer height and light:
1df["is_ground_floor"] = (df["floor"] <= 1).astype(int)
2df[["floor", "is_ground_floor"]].head(5)floor is_ground_floor 0 2 0 1 5 0 2 9 0 3 1 1 4 6 0
This is domain knowledge turned into a column. Nothing here is mathematically clever, it is one comparison, but if being on the ground floor really does move rent, this feature says so directly instead of hoping the model rediscovers "floor near 1 tends to mean lower rent" from the raw floor number on its own.
Where people get confused
Key Points
- "More engineered features are always better." Every new column is one more thing the model can overfit to, especially on a small dataset. A handful of well-reasoned features beats a pile of ones you added just in case, a problem Chapter 5 covers properly under overfitting.
- "I can build a feature out of anything, including the target." Never build a feature using the column you are trying to predict. A feature built from rent to help predict rent is not a shortcut, it is a leak of the answer into the question, and it will look great in training and fail completely the moment you use it on a flat whose rent you do not already know.
- "Feature engineering needs advanced maths." The most useful ones here were a division, a comparison and a bucket. The skill is knowing the domain, not knowing harder formulas.
Quick check
Why is rent_per_sqft a more useful feature than rent and size_sqft on their own, for comparing how expensive a location is?
rent alone mixes together the size of the flat and the cost of its location. Dividing by size_sqft removes most of the size effect, leaving something closer to a pure price-per-location comparison.
You are predicting whether a loan will default. Why would "months_since_last_missed_payment" be a dangerous feature to engineer?
If that value is only known after the loan has already defaulted or not, it leaks information from the outcome you are trying to predict, the same mistake as building a feature out of the target column itself.
What is binning, and why bucket size_sqft into Small, Medium and Large instead of leaving it as a raw number?
Binning groups a continuous number into a small number of meaningful categories. It can make a rough pattern easier for a simpler model to use, and easier for a person to reason about, at the cost of losing the exact value.
Recap
Key Points
- Feature engineering means building new, more useful columns out of the ones you already have, using what you know about the problem.
- Combining columns (rent_per_sqft, size_per_bhk) can surface a pattern that was buried inside raw numbers.
- Binning (size_category) turns a continuous number into meaningful groups.
- A simple rule you already know (is_ground_floor) can be worth handing to the model directly rather than hoping it finds the pattern alone.
- Never engineer a feature from the column you are trying to predict. That is target leakage, not a shortcut.
The flats data is now clean, encoded and engineered. One thing left before any model touches it: making sure you can honestly tell whether the model actually learned anything, instead of just memorising these 21 flats. That is the train/test split, and it closes this chapter.
Theia
Can you explain this concept?
Practise Feature Engineering 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.