Sub topic 10
Choosing between StandardScaler, MinMaxScaler and alternatives
This chapter assumes you know how fit and transform work, why scalers are fitted on training data only, and how outliers affect each scaler. Here you will use that knowledge to pick a scaler for a given model and dataset.
Start with the model
First check whether the model needs scaling at all. Tree-based models such as decision trees, random forests and gradient-boosted trees split on thresholds. Rescaling a column does not change which rows fall on which side of a split, so scaling is usually unnecessary for them.
Scaling matters for these model families:
- Distance-based models, such as k-nearest neighbors and k-means, where a large-range column dominates the distance.
- Gradient-based models, such as neural networks and logistic regression trained with gradient descent, which converge faster and more stably on comparable scales.
- Regularized linear models, such as ridge and lasso, where the penalty treats all coefficients equally and so depends on feature units.
- Models that rely on variance or covariance, such as PCA and SVMs with an RBF kernel.
Compare the data, not only the model
After the model, look at each column's distribution. The same three questions come up every time:
- Does the column have extreme values that are real but rare?
- Does the model need a bounded range, such as pixel intensities fed to a neural network?
- Is the data sparse, meaning mostly zeros?
The alternatives
RobustScaler subtracts the median and divides by the interquartile range (IQR, the distance between the 25th and 75th percentiles). Both statistics ignore the extreme tails, so a few outliers barely change the result for ordinary values.
MaxAbsScaler divides each column by its largest absolute value. It does not shift the data, so zeros stay zeros and sparse matrices stay sparse. The output lies in [-1, 1].
PowerTransformer and QuantileTransformer do more than rescale. They change the shape of the distribution, making a skewed column closer to normal. Use them when skew is the problem, not just scale. They are less interpretable, and QuantileTransformer discards the distances between values.
Normalizer is different in kind. It rescales each row to unit length instead of each column. It suits text vectors and similarity measures, not column-wise feature scaling.
See the difference
This example uses a column with one outlier and compares three scalers on the same data.
import numpy as np
from sklearn.preprocessing import MinMaxScaler, StandardScaler, RobustScaler
x = np.array([[1.0], [2.0], [3.0], [4.0], [5.0],
[6.0], [7.0], [8.0], [100.0]])
for scaler in (MinMaxScaler(), StandardScaler(), RobustScaler()):
out = scaler.fit_transform(x).ravel()
print(type(scaler).__name__, np.round(out[:8], 2))The printout shows only the eight ordinary values. MinMaxScaler squeezes them into roughly 0 to 0.07. StandardScaler places them between about -0.5 and -0.3. RobustScaler gives -1.0 to 0.75, because the median is 5 and the IQR is 4. The outlier itself becomes 23.75 under RobustScaler, so it stays visible as an outlier instead of compressing everyone else.
Summary table
| Scaler | Output | Sensitive to outliers | Keeps sparsity | Typical use |
|---|---|---|---|---|
| StandardScaler | Mean 0, standard deviation 1, unbounded | Yes | No (centering fills zeros) | Default for linear models, SVMs, PCA |
| MinMaxScaler | Fixed range, default [0, 1] | Very | No, unless the minimum is 0 | Neural networks, bounded inputs, image data |
| RobustScaler | Median 0, IQR 1, unbounded | Little | No | Data with real outliers |
| MaxAbsScaler | [-1, 1] | Yes, through the maximum | Yes | Sparse features |
| PowerTransformer | Roughly normal | Moderate | No | Skewed columns |
Common mistakes
- Treating min-max scaling as a safe default. One extreme value in the training data shrinks every other value, and new data outside the training range lands outside [0, 1].
- Scaling features for a random forest and expecting better accuracy. It costs a step and changes nothing useful.
- Removing outliers silently just so a scaler behaves. If the outliers are real, a robust scaler keeps them and avoids dropping information.
- Choosing by test score alone. Compare candidate scalers inside a
Pipelinewith cross-validation, so each fold fits its own scaler and the comparison is fair.
Exercise
A dataset has 50,000 rows and 20,000 columns of word counts, mostly zeros, and you plan to train a linear model on it. Which scaler fits best, and why would StandardScaler be a poor choice?
Show answer
MaxAbsScaler. It divides by the maximum absolute value without shifting, so zeros remain zeros and the matrix stays sparse. StandardScaler subtracts the mean from every entry, which turns the zeros into nonzero values and would make the matrix dense and very large in memory. Scikit-learn's StandardScaler on sparse input needs with_mean=False for this reason.
Exercise
A column of salaries contains a few executives paid 100 times the median. You need a scaler for a k-nearest neighbors model. Which scaler do you choose, and what happens to those executives' scaled values?
Show answer
RobustScaler. It uses the median and IQR, so the typical salaries keep a useful spread. The executives still receive very large scaled values, so they remain distinguishable as outliers, but they no longer squash everyone else into a narrow band, as MinMaxScaler would.