Kilograms and litres live on totally different scales — your model needs them normalised before it can compare them fairly.
Save
Complete lesson & earn 250 PX
EXERCISE
1When one feature is in kilograms and another is in litres, comparing them is meaningless — scaling puts them on a level playing field.
Save
EXERCISE
2StandardScaler transforms every value using the same formula: subtract the mean, divide by the standard deviation.
Save
EXERCISE
3When your model trains on scaled data, new predictions must also be scaled — the model only speaks "standardized language" now.
Save
Imagine comparing a basketball player''s height (2.1 meters) to their salary ($2,100,000). The salary number is a million times bigger, but that does not mean it is a million times more important. Scaling brings both to the same range so the model treats them fairly.
The problem — different scales:
import pandas as pd
df = pd.read_csv("cars2.csv")
print(df[["Volume", "Weight"]].head(5))
# Output:
# Volume Weight
# 0 1.0 790
# 1 1.2 1160
# 2 1.0 929
# 3 0.9 865
# 4 1.5 1140
# Volume: 0.9 – 2.5 (tiny numbers)
# Weight: 790 – 1746 (huge numbers)
# The model might think weight matters 1000x more — wrong!
The solution — standardization:
z = (x - mean) / standard_deviation
from sklearn.preprocessing import StandardScaler
import pandas as pd
df = pd.read_csv("cars2.csv")
X = df[["Weight", "Volume"]]
scaler = StandardScaler()
scaled = scaler.fit_transform(X)
print(f"Before: Weight={X.iloc[0,0]}, Volume={X.iloc[0,1]}")
# Output: Before: Weight=790, Volume=1.0
print(f"After: Weight={round(scaled[0][0],2)}, Volume={round(scaled[0][1],2)}")
# Output: After: Weight=-2.10, Volume=-1.59
# Now both are on the same scale!
> 💡 Key Insight: After standardization, each feature has mean = 0 and standard deviation = 1. A value of -2.1 means "2.1 standard deviations below the mean." Now you can fairly compare -2.1 (weight) with -1.59 (volume) — the weight is unusual in both cases, but weight is MORE unusual.
StandardScaler is like converting every student''s score to "how many standard deviations above or below the class average." An A student in a hard class and an A student in an easy class get different raw scores, but similar scaled scores.
Manual calculation vs StandardScaler:
import numpy
# Manual: scale the first weight value (790)
weights = [790,1160,929,865,1140,929,1109,1365,1112,1150,
980,990,1112,1252,1326,1330,1365,1280,1119,1328,
1584,1428,1365,1415,1415,1465,1490,1725,1523,1705,
1605,1746,1235,1390,1405,1395]
mean_w = numpy.mean(weights)
std_w = numpy.std(weights)
scaled_790 = (790 - mean_w) / std_w
print(f"Manual scaled: {round(scaled_790, 2)}")
# Output: Manual scaled: -2.10
Using StandardScaler (the ML way):
from sklearn.preprocessing import StandardScaler
import pandas as pd
df = pd.read_csv("cars2.csv")
X = df[["Weight", "Volume"]]
scaler = StandardScaler()
scaledX = scaler.fit_transform(X)
print(f"Scaled first row: {[round(v, 2) for v in scaledX[0]]}")
# Output: Scaled first row: [-2.1, -1.59]
# Verify: mean ≈ 0, std ≈ 1
print(f"Mean after scaling: {[round(v, 4) for v in scaledX.mean(axis=0)]}")
# Output: Mean after scaling: [0.0, 0.0]
print(f"Std after scaling: {[round(v, 4) for v in scaledX.std(axis=0)]}")
# Output: Std after scaling: [1.0, 1.0]
> 💡 Key Insight: fit_transform() does two things. fit() learns the mean and std from your . transform() applies the formula. On NEW data, you only call transform() — using the mean and std from training. Using fresh statistics on test data is a classic data leakage bug.
Once you teach someone in French, you cannot ask them questions in English and expect French answers. If your model trained on scaled , every new input must be scaled the same way before predicting.
Complete workflow with scaling:
import pandas as pd
from sklearn import linear_model
from sklearn.preprocessing import StandardScaler
# Load and prepare
df = pd.read_csv("cars2.csv")
X = df[["Weight", "Volume"]]
y = df["CO2"]
# Scale the features
scaler = StandardScaler()
scaledX = scaler.fit_transform(X)
# Train on scaled data
regr = linear_model.LinearRegression()
regr.fit(scaledX, y)
# Predict: weight=2300kg, volume=1.3L
# MUST scale the new input using the SAME scaler
new_data = [[2300, 1.3]]
scaled_new = scaler.transform(new_data)
predicted = regr.predict(scaled_new)
print(f"Predicted CO2: {round(predicted[0], 2)} g/km")
# Output: Predicted CO2: 107.21 g/km
Common mistake — forgetting to scale new data:
# ❌ WRONG: feeding raw values to a model trained on scaled data
# predicted = regr.predict([[2300, 1.3]]) # Wildly wrong answer!
# ✅ RIGHT: scale first, then predict
# scaled_input = scaler.transform([[2300, 1.3]])
# predicted = regr.predict(scaled_input)
> 💡 Key Insight: The (107.21 g/km) is exactly the same whether you scale or not — because is scale-invariant. So why bother? Because other algorithms (K-Nearest Neighbours, SVMs, neural networks) are NOT scale-invariant. Building the habit of scaling now will save you from mysterious bugs later. Always scale. Always.
StandardScaler transforms each feature to have mean 0 and standard deviation 1, making different units directly comparable.