Feature Scaling: Normalization and Standardization
MinMaxScaler, StandardScaler, RobustScaler — when to use each and why it matters.
Feature Scaling: Normalization and Standardization is a free Learn AI with Python lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn AI with Python learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Scale Features?
Many algorithms are sensitive to feature MAGNITUDE. A feature ranging 0 to 1,000,000 will dominate one ranging 0 to 1 in distance- or gradient-based models. Scaling puts features on comparable ranges.
Who Needs Scaling
Scaling matters for KNN, SVM, k-means, PCA, and gradient-descent models like linear/logistic regression and neural networks. Tree-based models (random forests, gradient boosting) are scale-invariant and do not need it.
Normalization: MinMaxScaler
Min-max normalization rescales each feature to a fixed range, usually 0 to 1, with (x - min) / (max - min). It preserves the shape of the distribution.
from sklearn.preprocessing import MinMaxScaler
import numpy as np
X = np.array([[10.0], [20.0], [30.0], [40.0]])
scaler = MinMaxScaler()
print(scaler.fit_transform(X).ravel()) # [0. 0.333 0.667 1.]Standardization: StandardScaler
Standardization gives each feature zero mean and unit variance with (x - mean) / std. The result is a z-score; values are centered, not bounded.
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
Xs = scaler.fit_transform(X)
print(Xs.ravel())
print(Xs.mean(), Xs.std()) # ~0 and ~1MinMax vs Standard
Use MinMax when you need a bounded range (e.g. image pixels, neural net inputs). Use Standard when the algorithm assumes roughly zero-centered data (PCA, SVM, linear models). MinMax is more sensitive to outliers.
RobustScaler for Outliers
RobustScaler centers on the MEDIAN and scales by the IQR. Because both resist outliers, it is the right choice when extreme values would distort the other scalers.
from sklearn.preprocessing import RobustScaler
Xo = np.array([[1.0], [2.0], [3.0], [4.0], [100.0]])
print(RobustScaler().fit_transform(Xo).ravel())fit vs transform
Scalers LEARN parameters in fit (the min/max, or mean/std) and APPLY them in transform. fit_transform does both in one call.
scaler = StandardScaler()
scaler.fit(X) # learns mean and std
Xs = scaler.transform(X) # applies themThe Golden Rule: Fit on Train Only
Always fit on the TRAINING set, then only transform the test set with those learned parameters. Fitting on test data leaks information about it into your model.
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test) # transform only!Why Not Fit on Test
If you scale using statistics that include the test set, your evaluation sees information it should not have, this is data leakage. It produces optimistic, unreliable performance estimates.
Inverse Transform
Scalers can reverse the operation with inverse_transform, useful to convert scaled predictions back to original units for reporting.
original = scaler.inverse_transform(X_train_scaled)
# recovers the pre-scaled valuesScaling the Target
You usually scale FEATURES, not the target, in classification. In regression you may scale the target too, but then remember to inverse-transform predictions before reporting errors.
Quick Check
Test your scaling knowledge.
Recap
Scaling toolkit:
- Scaling matters for distance/gradient models; trees ignore it
MinMaxScaler-> bounded 0..1;StandardScaler-> zero mean, unit varianceRobustScaleruses median and IQR, robust to outliers- Always
fit_transformon train,transformonly on test (avoid leakage)
Frequently asked questions
Is the “Feature Scaling: Normalization and Standardization” lesson free?
Yes — the full text of “Feature Scaling: Normalization and Standardization” is free to read here on the web, and the Learn AI with Python course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn AI with Python course, upgrade to CoddyKit PRO.
What will I learn in “Feature Scaling: Normalization and Standardization”?
MinMaxScaler, StandardScaler, RobustScaler — when to use each and why it matters. You practise Learn AI with Python with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Learn AI with Python?
No prior experience is required. Learn AI with Python on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Feature Scaling: Normalization and Standardization” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Learn AI with Python lesson?
Yes. Every Learn AI with Python lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Outlier Detection and Removal
- Encoding Categorical Variables
- Feature Scaling: Normalization and Standardization
- Building Preprocessing Pipelines