MediumFeature Engineering

Robust Scaling

Feature Engineering · Data Processing

Medium

Problem

Robust scaling centers values by their median and scales them by the interquartile range. These statistics are less sensitive to extreme values than the mean and standard deviation.

x_{\mathrm{scaled}} = \frac{x - Q_2}{Q_3 - Q_1}

Here, Q_2 is the median, Q_1 is the median of the lower half, and Q_3 is the median of the upper half. Exclude the overall median from both halves when the input length is odd. If the interquartile range is zero, return each value minus the median without division.

Theory

Robust scaling transforms features using statistics that are robust to outliers: the median and the interquartile range (IQR). Unlike standard scaling (which uses mean and standard deviation), robust scaling is not unduly influenced by extreme values, making it ideal for datasets with outliers.


Why Robust Scaling?

Outlier resistance: Mean and standard deviation are heavily affected by outliers. Median and IQR are not.

Preserves distribution shape: The bulk of the data is scaled consistently even with extreme values present.

No outlier removal needed: Can be applied directly without preprocessing to remove outliers.

Better for real-world data: Many real datasets contain measurement errors, data entry mistakes, or genuine extreme values.


The Robust Scaling Formula

For a feature column X with median Q_2 and interquartile range IQR = Q_3 - Q_1:

x_{scaled} = \frac{x - Q_2}{IQR}

Where:


Understanding Quartiles

Quartiles divide a sorted dataset into four equal parts:

Q1 (25th percentile): 25% of values are below this point

Q2 (50th percentile): The median; 50% of values are below

Q3 (75th percentile): 75% of values are below this point

IQR: The range containing the middle 50% of the data


Comparison with Standard Scaling

Standard (Z-score) scaling:

x_{scaled} = \frac{x - \mu}{\sigma}

Uses mean \mu and standard deviation \sigma, both sensitive to outliers.

Robust scaling: Uses median and IQR, resistant to outliers.

Example impact of outliers:

Data: [1, 2, 3, 4, 5, 100]

Standard scaling statistics:

Robust scaling statistics:


Worked Example

Data: [10, 20, 30, 40, 50, 60, 70, 80, 90, 1000]

Note: 1000 is a clear outlier.

Step 1 - Compute quartiles:

Step 2 - Compute IQR:

IQR = Q3 - Q1 = 82.5 - 27.5 = 55

Step 3 - Apply robust scaling:

For value 10:

x_{scaled} = \frac{10 - 55}{55} = \frac{-45}{55} = -0.82

For value 50:

x_{scaled} = \frac{50 - 55}{55} = \frac{-5}{55} = -0.09

For value 1000 (outlier):

x_{scaled} = \frac{1000 - 55}{55} = \frac{945}{55} = 17.18

Observation: The outlier (1000) gets a scaled value of 17.18, clearly marking it as extreme. The bulk of the data falls in a reasonable range around 0.


Properties of Robust Scaled Data

Centered at median: The median value maps to 0

Scaled by IQR: A value exactly at Q3 maps to 0.5; exactly at Q1 maps to -0.5

Unbounded: Unlike min-max scaling, values are not constrained to a specific range

Outliers remain outliers: Extreme values are clearly identifiable as far from 0


Handling Zero IQR

When Q1 = Q3 (all middle 50% of values are identical):

IQR = 0 \Rightarrow \frac{x - Q_2}{0} = \text{undefined}

Solutions:


Computing Percentiles

Two main interpolation methods for percentiles:

Linear interpolation: Interpolate between adjacent values when the percentile falls between data points

Nearest rank: Round to the nearest data point

Different methods can give slightly different results, especially for small datasets.


Centering Options

The formula can be modified:

With centering (default):

x_{scaled} = \frac{x - Q_2}{IQR}

Without centering:

x_{scaled} = \frac{x}{IQR}

The centered version is more common as it places the median at 0.


When to Use Robust Scaling

Good for:

Less suitable for:


Column-wise Application

Like other scaling methods, robust scaling is applied independently to each feature:

Steps for 2D data:

  1. For each column, compute Q1, Q2 (median), Q3
  2. Compute IQR for each column
  3. Apply formula to each element using its column statistics

Result: Each feature is centered at its median and scaled by its IQR


Train-Test Split Considerations

Important: Compute Q1, Q2, Q3 from training data only

Apply to test data using training statistics:

x_{test,scaled} = \frac{x_{test} - Q_{2,train}}{IQR_{train}}

Rationale: Prevents data leakage from test set


Robust Scaling vs Winsorization

Robust scaling: Transforms all values, outliers remain but are clearly extreme

Winsorization: Caps outliers at specified percentiles, then scales

Combined approach: Winsorize first to cap extreme values, then apply robust scaling


Where Robust Scaling Shows Up

Examples

Example 1

Input
values = [1, 2, 3, 4, 5]
Output
[-0.6667, -0.3333, 0.0, 0.3333, 0.6667]
Explanation
The median is 3, the lower and upper quartiles are 1.5 and 4.5, and the interquartile range is 3.

Example 2

Input
values = [10, 20, 30, 40]
Output
[-0.75, -0.25, 0.25, 0.75]

Hints

  1. Write a small helper that returns the median of an already sorted list.
  2. Form the lower and upper halves before computing their medians.

Requirements

Constraints

Starter Code

def robust_scaling(values: list) -> list:
    """
    Returns values centered by the median and scaled by the interquartile range.
    """
    # Write code here
    pass

Test Cases

CaseMatches
oddpublic
evenpublic