Python / Python Mathematical Intuition and Scikit Learn Interview Questions
Mathematically, why does RobustScaler handle outliers better than StandardScaler?
StandardScaler transforms features using the mean and standard deviation: z = (x - μ)/σ. Both the mean and standard deviation are heavily influenced by extreme values — a single huge outlier can shift the mean substantially and dramatically inflate the standard deviation (since it involves squared deviations from the mean). This means StandardScaler can compress the majority of "normal" data points into a very narrow range near zero while the outlier dominates the scale, distorting the relative spacing among typical values.
RobustScaler instead uses the median and the interquartile range (IQR = Q3 - Q1): z = (x - median)/IQR. The median and IQR are robust statistics — by definition, they depend only on the middle portion of the sorted data and are unaffected by how extreme the tail values are (moving the maximum value further out doesn't change the median or IQR at all, as long as it stays in the same tail). This makes RobustScaler's transformation insensitive to the presence of outliers, preserving meaningful relative differences among the bulk of the data.
import numpy as np from sklearn.preprocessing import StandardScaler, RobustScaler # Data with one extreme outlier data = np.array([10, 12, 11, 13, 12, 11, 1000]).reshape(-1, 1) # 1000 is an outlier ss = StandardScaler() rs = RobustScaler() print('StandardScaler:', ss.fit_transform(data).ravel()) # The outlier dominates: normal values get squashed close together near 0 print('RobustScaler:', rs.fit_transform(data).ravel()) # Normal values retain meaningful spread; outlier is scaled but doesn't # distort the relative positions of the typical values print('Mean:', data.mean(), 'Std:', data.std()) # both skewed by outlier print('Median:', np.median(data)) # robust to the outlier q75, q25 = np.percentile(data, [75, 25]) print('IQR:', q75 - q25) # also robust
More Related questions...