Summary - Scaling
- Machine Learning course
- Beginner
- Lesson 80/112
- 6 min
In this group you learned StandardScaler, MinMaxScaler, and Pipeline.
Summary: scaling
In this group:
- Features in different units break distance- and gradient-based models; trees aren't affected.
StandardScaler(z-score),MinMaxScaler([0, 1]),RobustScaler(median/IQR).- The scaler is fit only on training data;
inverse_transformconverts back. - A
Pipelinecombines preprocessing and the model, prevents leakage and is tuned withstep__parameter.
Practice Choosing a scaler
For x, which contains an outlier, print the MinMaxScaler and RobustScaler results with ravel().round(2) on separate lines.
Starter code
import numpy as np
from sklearn.preprocessing import MinMaxScaler, RobustScaler
x = np.array([[10.0], [12.0], [11.0], [13.0], [90.0]])
# buraya yazSample solution
import numpy as np
from sklearn.preprocessing import MinMaxScaler, RobustScaler
x = np.array([[10.0], [12.0], [11.0], [13.0], [90.0]])
print(MinMaxScaler().fit_transform(x).ravel().round(2))
print(RobustScaler().fit_transform(x).ravel().round(2))Expected output
[0. 0.03 0.01 0.04 1. ] [-1. 0. -0.5 0.5 39. ]
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which causes data leakage?
- Fitting the scaler on X_train
- Training a tree model without scaling
- Fitting the scaler on all data and splitting afterwards Correct answer
- Using a pipeline
Explanation: The test set's statistics mix into the scale; the score comes out optimistic.
2 Create a pipeline that names the steps automatically.
pipe = ____(StandardScaler(), LogisticRegression())
pipe = make_pipeline(StandardScaler(), LogisticRegression())
Explanation: make_pipeline uses the lowercase class names as step names.
3 Match the formulas.
| Term | Meaning |
|---|---|
StandardScaler |
(x - mean) / std |
MinMaxScaler |
(x - min) / (max - min) |
RobustScaler |
(x - median) / IQR |
inverse_transform |
Back to original unit |