Standard Scaler Without Data Leakage

easy
Checkpoint challenge
Solve this to show you know the material and jump straight to Unit 4. Everything before it counts as cleared.

Many models need features on the same scale. Standardisation rescales each column to mean 0 and standard deviation 1. The catch: the mean and standard deviation must come from the training data only. Computing them on all the data leaks information from the test set and inflates your scores.

Implement a StandardScaler class like scikit-learn's:

  • fit(X) learns self.mean_ and self.scale_ (per-column population standard deviation, ddof=0) and returns self. Columns with zero variance get a scale of 1.0, so you never divide by zero.
  • transform(X) returns (X - mean_) / scale_ using the fitted statistics.
  • fit_transform(X) does both.
  • inverse_transform(Z) undoes it.
  • Calling transform before fit raises RuntimeError.
#preprocessing#leakage#numpy3 visible + 3 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Standard Scaler Without Data Leakage · TechGradify