Leakage-Free Cross-Validation

medium
Unit 18 · Online Learning0/2 to milestone

Scaling the whole dataset before cross-validation leaks test-fold statistics into training. The fix is to fit preprocessing inside each fold.

Implement cv_mse(X, y, k): split rows into k contiguous folds (np.array_split(np.arange(n), k)); for each fold:

  1. compute mean and std (ddof=0, replace 0 with 1) on the training rows only and standardise both parts with them
  2. fit ridge-free least squares with an intercept on the training part
  3. record the MSE on the held-out fold

Return the mean of the fold MSEs.

#validation#data-leakage#pipelines2 visible + 1 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Leakage-Free Cross-Validation · TechGradify