Implement a RobustScaler from scratch using the median and IQR

Quick Overview

Build a RobustScaler class in Python that centers each feature on its training median and divides it by its interquartile range. It tests NaN-aware statistics, constant features with a zero IQR, matching sklearn's output, and keeping fit and transform separate so test data never leaks into the scaling.

Implement a RobustScaler from scratch using the median and IQR

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement a `RobustScaler` class. `fit(X)` learns each feature's median (`center_`) and IQR (`scale_`), and `transform(X)` returns `(X - center_) / scale_` using those training statistics. The class must meet these requirements: - Median and quartiles use NumPy's default linear interpolation, ignoring NaN (`IQR = q75 - q25`). Results must match sklearn's `RobustScaler`. - A zero IQR is replaced by `1.0`, so that feature is only centered and never produces NaN or inf. - NaN inputs pass through `transform` unchanged. - A DataFrame input returns a `float64` DataFrame with the same columns and index. An array input returns a `float64` ndarray. - Never mutate the input. `fit` returns `self`. Example: ```python train = [[0, 7], [10, 7], [20, 7], [30, 7], [40, 7]] sc = RobustScaler().fit(train) sc.center_ # [20.0, 7.0] sc.scale_ # [20.0, 1.0] (zero IQR -> 1.0) sc.transform([[60, 9], [-20, 7]]) # [[ 2.0, 2.0], [-2.0, 0.0]] ``` ```hint Where the statistics live Look for NumPy reductions that skip NaN, and compute every statistic in `fit`. `transform` should only apply numbers the scaler already stores. ``` ### Constraints & Assumptions - `X` is 2-D numeric data (ndarray or DataFrame) with shape `(n_samples, n_features)`. - Each training column has at least one non-NaN value. ### What a Strong Answer Covers - How `fit` computes the per-column median and quartiles with NaN ignored and the same interpolation as sklearn, and why those values are frozen for `transform`. - What happens to a constant feature whose IQR is zero. - Input handling: a private `float64` copy, the 2-D check, a feature-count check in `transform`, and DataFrame columns and index kept on output. - Behavior on NaN inputs and on calling `transform` before `fit`. - The time and space cost of fitting and transforming. ### Follow-up Questions - Why prefer median and IQR scaling over mean and standard deviation scaling, and when would you still choose standard scaling? - A feature is 0 in 90% of rows with rare large spikes. What does this scaler do to it, and what would you do instead? - How would you add a configurable quantile range such as (10, 90) and an option to skip centering? - How do you use this scaler inside cross-validation without letting validation rows shape the statistics?

Overview: Build a RobustScaler class in Python that centers each feature on its training median and divides it by its interquartile range. It tests NaN-aware statistics, constant features with a zero IQR, matching sklearn's output, and keeping fit and transform separate so test data never leaks into the scaling.

General logo
General
Nov 25, 2026
easyMachine Learning EngineerOtherMachine Learning
0
0

Implement a RobustScaler class. fit(X) learns each feature's median (center_) and IQR (scale_), and transform(X) returns (X - center_) / scale_ using those training statistics.

The class must meet these requirements:

  • Median and quartiles use NumPy's default linear interpolation, ignoring NaN ( IQR = q75 - q25 ). Results must match sklearn's RobustScaler .
  • A zero IQR is replaced by 1.0 , so that feature is only centered and never produces NaN or inf.
  • NaN inputs pass through transform unchanged.
  • A DataFrame input returns a float64 DataFrame with the same columns and index. An array input returns a float64 ndarray.
  • Never mutate the input. fit returns self .

Example:

train = [[0, 7], [10, 7], [20, 7], [30, 7], [40, 7]]
sc = RobustScaler().fit(train)
sc.center_  # [20.0, 7.0]
sc.scale_   # [20.0, 1.0]   (zero IQR -> 1.0)
sc.transform([[60, 9], [-20, 7]])
# [[ 2.0, 2.0], [-2.0, 0.0]]

Constraints & Assumptions

  • X is 2-D numeric data (ndarray or DataFrame) with shape (n_samples, n_features) .
  • Each training column has at least one non-NaN value.

What a Strong Answer Covers Guidance

  • How fit computes the per-column median and quartiles with NaN ignored and the same interpolation as sklearn, and why those values are frozen for transform .
  • What happens to a constant feature whose IQR is zero.
  • Input handling: a private float64 copy, the 2-D check, a feature-count check in transform , and DataFrame columns and index kept on output.
  • Behavior on NaN inputs and on calling transform before fit .
  • The time and space cost of fitting and transforming.

Follow-up Questions Guidance

  • Why prefer median and IQR scaling over mean and standard deviation scaling, and when would you still choose standard scaling?
  • A feature is 0 in 90% of rows with rare large spikes. What does this scaler do to it, and what would you do instead?
  • How would you add a configurable quantile range such as (10, 90) and an option to skip centering?
  • How do you use this scaler inside cross-validation without letting validation rows shape the statistics?