Skip to content

Pick a model without peeking

Which penalty does honest cross-validation choose, and what is its error on test groups nobody looked at while choosing?

Capstone 17~40 minadvanced Lab A selection data (100 sites, simulated)

The story

A lab measures 400 samples from 100 sites and wants a model that predicts a reading y from eight measurements, some of them missing. A previous analyst reported a wonderful test error, but they imputed and scaled with all the rows, tried penalties until the test error looked best, and split rows at random, so samples from one site sat on both sides.

This time the protocol is fixed in advance and the data already carry it: the split column puts whole sites into five cross-validation folds and locks 20 sites as the test set. Imputation and scaling are learned inside each fold. The ridge penalty is chosen from six values written down before any fit. The test set is sealed, the evaluation plan is frozen first, and the test score is revealed once. Follow it in Predict and check that you get the reference's number.

Data: Lab A selection data (100 sites, simulated) · Simulated. ML expansion reference bundle, Laboratory A selection fixture (simulated); split column added by CodeSplash.

Stage 1 · model · ~11 min

State the task

Start an experiment on the sample, predict y, and use only the eight measurements x1–x8 as features. The bookkeeping columns (group, phase, fold, split) describe the design, and a model that sees them would learn the protocol instead of the data.

Which columns are features, and why must the design columns stay out?

  1. Open Predict in the sidebar.
  2. Press New experiment, choose lab_a (sample) and press Create.
  3. Set Target to y.
  4. Keep only x1 to x8 as features. Clear the list, then tick the eight measurements.A column that encodes the split or the site lets the model learn the protocol, so its score stops measuring prediction.
  5. Press Continue.

Stage 2 · model · ~9 min

Import the split, fit inside the folds

Use the split column as the plan: its fold numbers keep each site in one fold, and test marks the locked sites. Set imputation to the mean and scaling to standardize, learned on each fold's training rows only, and seal the test set before anything is trained.

Why must imputation and scaling be learned inside each fold rather than on all the rows?

  1. Set Split column to split. The split plan should show five folds and a locked test set.The split comes from the study design, written down before any fit, so no choice can drift toward a better test score.
  2. Under Preprocessing for every model, set Missing values to the mean and Scaling to standardize.Means and standard deviations computed on all rows carry information from the validation and test rows into training.
  3. Turn on the test seal.
  4. Press Continue.

Stage 3 · model · ~11 min

Choose the penalty from a fixed grid

Ridge regression is Elastic net with an L1 share of 0. Tune only its penalty, over the reference's six values written down in advance: 3.125e-6, 3.125e-5, 3.125e-4, 0.003125, 0.03125 and 0.3125 (the reference's 0.001 to 100, divided by its 320 development rows). The folds choose; the test set stays sealed.

Which penalty do the folds choose, and why does choosing it on the test set make the test score useless?

  1. Select Elastic net and set L1 share (ρ) to 0 on its card, which makes it ridge regression.
  2. Press Tune on the card, pick only Penalty (λ), and open Edit the search space.
  3. Choose Exact values for the penalty, type the six values from the brief, choose Grid search, and press Start tuning.A grid fixed before any fit can't be bent toward the answer you hoped for, and every value gets the same folds.
  4. Open the tuning run and wait for it to finish. Its search should be one hyperparameter, Penalty (λ), at exactly the six values, by grid search.Checking what was searched before reading the winner shows the penalty was chosen from the values fixed in advance, and nothing else.
  5. Read which penalty won on the folds, then press Final model.

Stage 4 · model · ~9 min

Freeze the plan, look once

Write down how the model will be judged before seeing the test set: freeze the evaluation plan with the final model as its leader, then reveal the test set once. Then compare the test RMSE with the reference's number.

What is the final model's test RMSE, and how many times was the test set revealed?

  1. Open the Evaluate step.
  2. Check that the final model leads the plan and press Freeze plan.A frozen plan records the metric and the runs before the test set is seen, so the result can't choose its own yardstick.
  3. Reveal the test set once.
  4. Read the final model's test RMSE. It should match the reference's 1.1498.
Hints
  • The design columns (group, phase, fold, split) are not measurements. Keep them out of the features.
  • Every choice that learns from data (imputation, scaling, the penalty) is made on the folds' training rows.
  • Freeze the plan, then reveal once. The ledger counts every reveal.

appendix

Selection protocol and result

Write the methods appendix: the unit of splitting, what was learned inside the folds, the grid and how it was fixed, how the penalty was chosen, and the single test result with the number of reveals.

  1. Unit and split
  2. Inside the folds
  3. Grid and selection
  4. The one test result
Rubric
CriterionPoints
Says whole sites were kept within one fold and the test sites were locked.2
States that imputation and scaling were learned on each fold's training rows only.2
Gives the six-value grid and says it was fixed before any fit.2
Reports the test RMSE from one reveal under a frozen plan.2
Notes that the result reproduces the reference's test RMSE.1

Model answer

Show the model answer

Unit and split. The unit is the site: the split column keeps all samples from a site in one of five folds, and 20 sites form a locked test set, so validation always predicts sites the model never trained on.

Inside the folds. Missing values were filled with the training rows' means and every measurement standardized with the training rows' means and standard deviations, refitted in every fold and once more for the final model. The design columns stayed out of the features.

Grid and selection. Ridge regression (Elastic net with an L1 share of 0) was tuned over six penalties written down in advance, the reference's 0.001 to 100 on its own scale, which is the same grid divided by the 320 development rows on glmnet's scale. Grid search on the folds chose the fourth value (the reference's α = 1), and the final model refitted it on all development rows.

The one test result. With the plan frozen first, the test set was revealed once. The test RMSE is 1.1498 on 80 test samples, the reference's own result. A random row split, preprocessing on all rows or a penalty picked on the test set would each have produced a better-looking and less honest number.