devnotes.

Robust, Max-Abs, Normalizer and Power Transforms

Besides Min-Max and Standardization, scikit-learn provides scalers for special situations.

1. Robust scaling

Uses the median and the interquartile range (IQR), so outliers barely influence it.

X_scaled = (X - median) / IQR

from sklearn.preprocessing import RobustScaler

rb = RobustScaler()
X_train_rb = rb.fit_transform(X_train[num_cols])
X_test_rb = rb.transform(X_test[num_cols])

Use it when the data contains outliers that you decided to keep.

2. Max-Abs scaling

Divides each feature by its maximum absolute value, mapping data to [-1, 1] without shifting or centering it. Zeros stay zeros, so it is ideal for sparse data.

from sklearn.preprocessing import MaxAbsScaler

ma = MaxAbsScaler()
X_train_ma = ma.fit_transform(X_train[num_cols])
X_test_ma = ma.transform(X_test[num_cols])

3. Unit-vector normalization (Normalizer)

Scales each row (sample) to have unit norm (length 1). It changes the sample, not the feature. Common for text vectors and cosine similarity.

from sklearn.preprocessing import Normalizer

norm = Normalizer(norm="l2")               # each row gets length 1
X_norm = norm.fit_transform(X_train[num_cols].fillna(0))

4. Power transforms

Make skewed features more Gaussian, stabilizing variance.

  • Box-Cox: positive values only.
  • Yeo-Johnson: works for positive, zero and negative values.
from sklearn.preprocessing import PowerTransformer

pt = PowerTransformer(method="yeo-johnson")
X_train_pt = pt.fit_transform(X_train[["Fare"]])
X_test_pt = pt.transform(X_test[["Fare"]])

5. Quantile transform

Maps values to a uniform or normal distribution based on their rank. It is very robust against outliers but distorts distances between values.

from sklearn.preprocessing import QuantileTransformer

qt = QuantileTransformer(n_quantiles=100, output_distribution="normal", random_state=42)
X_train_qt = qt.fit_transform(X_train[["Fare"]])
X_test_qt = qt.transform(X_test[["Fare"]])

Comparison

ScalerFormula ideaOutputOutlier sensitivityTypical use
MinMaxScaler(x - min)/(max - min)[0, 1]HighNeural nets, images
StandardScaler(x - mean)/stdMean 0, std 1MediumSVM, logistic regression, PCA
RobustScaler(x - median)/IQRUnboundedLowData with outliers
MaxAbsScalerx / max(abs(x))[-1, 1]HighSparse data
Normalizerrow / row-normUnit length per rowNot applicableText, cosine similarity
PowerTransformerBox-Cox / Yeo-JohnsonApproximately GaussianMediumSkewed features
QuantileTransformerRank-based mappingUniform or normalVery lowHeavy outliers, non-linear data
Robust, Max-Abs, Normalizer and Power Transforms - Feature Scaling | Data Preparation | DevNotes