Feature Scaling
rent lives in the thousands. furnished_num lives in 0 and 1.
After last lesson, every column in the flats data is a number. But look at the scales: rent runs from ₹13,500 to ₹54,000, size_sqft from 525 to 1,575, and furnished_num is either 0 or 1. To a lot of algorithms, a bigger number simply means a bigger distance, and rent will dwarf furnished_num in every calculation, not because rent matters more to predicting the outcome, but because its numbers happen to be larger.
Feature scaling fixes that: putting every numeric column onto a comparable range, without changing what it means, only the units it is measured in.
Which models actually care
Not every model is bothered by scale, so it helps to know which ones are before you spend time on this.
Key Points
- Cares a lot: anything based on distance between points (K-Nearest Neighbours, K-Means) or on gradient descent (linear and logistic regression trained the way lesson 4.3 will show you, neural networks). rent alone would dominate any distance calculation.
- Cares somewhat: models with a regularisation penalty (Ridge, Lasso, covered in Chapter 5), because the penalty is applied to the size of each coefficient, and an unscaled feature gets penalised unfairly compared to a scaled one.
- Does not care: decision trees, random forests, gradient boosting. They split data on thresholds like "is size_sqft above 900", and a threshold works exactly the same whether size_sqft is measured in square feet or scaled to 0-to-1.
Tip:When in doubt, scale anyway. It practically never hurts a model that does not need it, and it can badly hurt one that does.
Min-max scaling: squeeze into 0 to 1
The idea: find the smallest and largest value in a column, and stretch everything to fit between 0 and 1.
1scaled = (value - minimum) / (maximum - minimum)For rent, the minimum is ₹13,500 and the maximum is ₹54,000. Take the first flat, rent ₹26,500: (26500 − 13500) / (54000 − 13500) = 13000 / 40500 ≈ 0.321. That flat sits about 32% of the way from the cheapest to the most expensive rent in the dataset. The cheapest flat becomes exactly 0, the most expensive becomes exactly 1.
scikit-learn does this for a whole column at once:
1import pandas as pd
2from sklearn.preprocessing import MinMaxScaler
3
4df = pd.read_csv("https://cod-e.vercel.app/data/pune-flats.csv")
5scaler = MinMaxScaler()
6df[["rent_scaled", "size_scaled"]] = scaler.fit_transform(df[["rent", "size_sqft"]]).round(3)
7df[["rent", "rent_scaled", "size_sqft", "size_scaled"]].head(5)rent rent_scaled size_sqft size_scaled 0 26500 0.321 1275 0.714 1 14500 0.025 675 0.143 2 29500 0.395 875 0.333 3 17500 0.099 525 0.000 4 20500 0.173 625 0.095
Row 3 has the smallest size_sqft in these five rows and lands at 0.000, since 525 is the true minimum across the whole dataset. rent and size_sqft are now both squeezed into the same 0-to-1 range, directly comparable in a way ₹17,500 and 525 square feet never were.
Tip:Min-max scaling is sensitive to outliers. One flat renting for ₹5,00,000 would stretch the whole 0-to-1 range so far that every ordinary flat gets squeezed into a tiny sliver near 0.
Standardisation: centre on 0, measure in standard deviations
The other common approach does not force a fixed range. Instead, it centres the column on its mean and measures every value in standard deviations away from that mean, the z-score from lesson 2.7.
1scaled = (value - mean) / standard_deviation1from sklearn.preprocessing import StandardScaler
2
3scaler = StandardScaler()
4df[["rent_z", "size_z"]] = scaler.fit_transform(df[["rent", "size_sqft"]]).round(3)
5df[["rent", "rent_z", "size_sqft", "size_z"]].head(5)rent rent_z size_sqft size_z 0 26500 -0.019 1275 0.996 1 14500 -1.219 675 -0.906 2 29500 0.281 875 -0.272 3 17500 -0.919 525 -1.381 4 20500 -0.619 625 -1.064
Row 0 has a rent_z of about -0.02, meaning its rent is almost exactly the average rent in the dataset. Row 1 has a rent_z of about -1.22, meaning it rents for about 1.2 standard deviations below the average, noticeably cheap. Unlike min-max scaling, there is no fixed floor or ceiling; a value can land below 0 or above 1 depending on how far it sits from the mean.
This is the default most people reach for. It is less thrown off by a single outlier than min-max scaling, because one very expensive flat shifts the mean only slightly, while it would drag the min-max range enormously.
One rule that matters more than which method you pick
Whichever scaler you use, fit it only on the training data, never on the whole dataset. fit_transform above is written for a clean demonstration on the full table; in a real pipeline, you would fit_transform on the training set and only .transform the test set with those same numbers. Lesson 3.7 covers the train/test split itself, and this exact mistake, fitting a scaler on data the model is not supposed to have seen yet, is one of the most common ways people accidentally leak test information into training. Keep it in mind; it will make more sense once the split is in front of you.
Where people get confused
Key Points
- "Scaling changes what the data means." It does not. Row 0 is still the same flat with the same rent; only the units used to represent that rent have changed.
- "I should scale everything, including furnished_num and one-hot columns." Usually left alone. They are already 0 or 1, and scaling a column that is already in a small, meaningful range adds nothing.
- "Tree-based models need this too." They generally do not, since a split threshold works the same regardless of scale. Scaling them costs you nothing, but it buys you nothing either.
- "Min-max and standardisation give roughly the same numbers." Compare rent_scaled (0.321) and rent_z (-0.019) for the same row above. They are built on different ideas and will not usually agree.
Quick check
Why would K-Nearest Neighbours give misleading results on unscaled rent and bhk together?
rent has a much larger numeric range than bhk, so distance calculations would be dominated almost entirely by rent, making bhk nearly irrelevant to which neighbours get picked, even if bhk actually matters.
Why is standardisation usually preferred over min-max scaling when a column has a few extreme outliers?
Min-max scaling stretches its whole range to fit the single largest and smallest value, so one extreme outlier squeezes every normal value into a tiny sliver. Standardisation is centred on the mean, which one outlier shifts only slightly.
Would you scale a decision tree's input features? Why or why not?
Not necessary. A decision tree splits on thresholds, and a threshold like "size_sqft above 900" behaves identically whether the values are raw or scaled.
Recap
Key Points
- Feature scaling puts every numeric column onto a comparable range, so no single feature dominates just because its raw numbers happen to be large.
- Distance-based and gradient-descent-based models need it. Tree-based models generally do not.
- Min-max scaling squeezes every value into 0 to 1, using the min and max, and is sensitive to outliers.
- Standardisation centres on the mean and measures in standard deviations (z-scores), and handles outliers more gracefully.
- Always fit the scaler on training data only, never on the whole dataset, to avoid leaking information from data the model should not have seen yet.
You already engineered one feature without calling it that, rent_per_sqft back in lesson 2.4. Making your own useful columns like that on purpose, deliberately, is next.
Theia
Can you explain this concept?
Practise Feature Scaling 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.