When to use?
- Machine Learning course
- Beginner
- Lesson 78/112
- 5 min
Scaling is important for distance-based algorithms like KNN and SVM.
When to use which?
General rules:
- StandardScaler: the default choice; linear models, SVM, PCA, KNN.
- MinMaxScaler: when a bounded range is needed (neural network inputs, pixels) and there are no outliers.
- RobustScaler: when there are clear outliers.
- None: not needed for tree-based models.
If unsure, compare a few with cross-validation; the choice is a hyperparameter too.
The leakage rule
The scaler is fit only on the training data. Fitting on all data leaks the test statistics into training and artificially inflates the score:
# WRONG
sc = StandardScaler().fit(X) # includes the test
# RIGHT
sc = StandardScaler().fit(X_train)
X_train_s = sc.transform(X_train)
X_test_s = sc.transform(X_test)In test data the min/max may exceed the training ones; seeing values outside 0-1 after MinMax is normal.
Practice Out-of-range test value
Fit a MinMaxScaler on X_train only. Transform X_test and print it; see that the value outside the training range comes out above 1.
Starter code
import numpy as np
from sklearn.preprocessing import MinMaxScaler
X_train = np.array([[10.0], [20.0], [30.0]])
X_test = np.array([[25.0], [40.0]])
# buraya yazSample solution
import numpy as np
from sklearn.preprocessing import MinMaxScaler
X_train = np.array([[10.0], [20.0], [30.0]])
X_test = np.array([[25.0], [40.0]])
mm = MinMaxScaler().fit(X_train)
print(mm.transform(X_test))Expected output
[[0.75] [1.5 ]]
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 If income data includes a few billionaires, which scaler fits?
- RobustScaler Correct answer
- MinMaxScaler
- No scaling needed
- StandardScaler always
Explanation: The median and IQR aren't affected by outliers.
2 Learn the scaler on the training data only.
sc = StandardScaler().fit(____)
sc = StandardScaler().fit(X_train)
Explanation: Test data must not take part in learning the scale.
3 Match the situations with the choices.
| Term | Meaning |
|---|---|
Default |
StandardScaler |
Pixels 0-255 |
MinMaxScaler |
Outliers |
RobustScaler |
Random forest |
No scaling needed |