Robust transformations
Quick answer
Robust transformations reduce the influence of extreme values on fitted preprocessing statistics. RobustScaler centers using the training median and, by default, divides by the interquartile range (IQR), the difference between the 75th and 25th percentiles. It does not remove or cap extreme values. All examples use synthetic inputs and local scikit-learn 1.8.0 where imported; ByteMind package execution is unverified.
Inspect median and IQR scaling
The fitted center and scale depend on central quantiles, not the largest value.
from sklearn.preprocessing import RobustScalers = RobustScaler().fit([[1.],[2.],[3.],[4.],[100.]])print(s.center_.tolist(), s.scale_.tolist())print(s.transform([[3.],[100.]]).ravel().tolist())Expected output:
[3.0] [2.0][0.0, 48.5]The large value remains large; robust scaling does not truncate it.
How it works
For [1,2,3,4,100], the median is 3 and the default linear-interpolation quartiles are 2 and 4, giving IQR 2. An extreme 100 is still transformed to 48.5. Quantile conventions vary, so document the algorithm. A zero IQR needs a safe scale policy (RobustScaler handles zero scales). Clipping replaces values outside a lower and upper bound with those bounds; use domain rules or training quantiles and retain flags if useful. An outlier can be a valid rare event, incorrect unit or sensor error: investigate before changing it. Log1p maps nonnegative x to log(1+x), compressing large values and preserving zero; it is not defined as a finite real value at x <= -1. A log transform changes the geometry and interpretation, and transformed targets need careful inverse transformation. Choose and validate preprocessing using training and validation data; compare subgroup error and monitoring effects.
Clip only with explicit bounds
These are illustrative approved sensor limits, not inferred universal outlier cutoffs. Preserve a flag for changed values.
import numpy as npx = np.array([-2, 0, 5, 12])y = np.clip(x, 0, 10)print(y.tolist())print((x != y).astype(int).tolist())Expected output:
[0, 0, 5, 10][1, 0, 0, 1]The bounds are inclusive, and only the two outside values change.
Common mistakes
Do not delete all rows outside 1.5 IQR without a domain and validation rationale. Robust scaling does not guarantee robustness of the estimator or fairness. Do not compute quantile thresholds from holdout data. Document whether clipping masks rare but important failures; track counts and raw-value availability under the data retention policy.
Practice and recap
Try clip values and keep an audit flag. Input [lo, hi, values], integer bounds with lo <= hi and integer values. Return [clipped, flags] in original order, flag 1 only if the original value was strictly outside the inclusive bounds. Empty values return [[], []]. Then explain the preprocessing assumptions before choosing a transformation. Next: Sparse feature pipelines.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Robust transformations — knowledge check
Apply the preprocessing concept to small synthetic data and diagnose mistakes.
What statistics does default RobustScaler use?
Does robust scaling automatically cap an extreme value?
When should a legitimate rare measurement be deleted?
Clipping to [0,10], what flag should value 10 receive under a changed-value policy?
Which input is safe for finite real log1p under the nonnegative feature contract?