Skip to content

Lab A: pick a model without peeking

The reference's first lab, step by step. Whole groups go to a final test, five group folds choose a ridge penalty with every step fitted inside the fold, and the test set is scored once. Then do the same in Predict.

Applied ~15 min8 steps

What runs in CodeSplash

You will be able to

  • Lay out a selection experiment with a group-level final test and group folds
  • Keep every learned step inside the fold during selection
  • Say what the fold scores are for, and what they are not
  • Explain why a shared fixture, not a shared seed, makes Python and R comparable

Read first

01

The design

Read

Laboratory A predicts a simulated continuous outcome from eight features. Some values are missing, and rows come in groups: 100 groups of four rows each, like four visits per patient.

  1. Reserve the final test first: 20 whole groups, never split.
  2. Make five folds from the other 80 groups, each group in exactly one fold.
  3. Inside each training fold, fit mean imputation, then standardization, then ridge regression, and score the held-out fold.
  4. Choose the penalty with the best average fold score.
  5. Refit the chosen pipeline on all 80 development groups.
  6. Score the final test once.

The selection code receives development arrays only. The test rows aren't in its reach.

02

What ridge minimizes

Read

With standardized features ziz_i and an intercept that isn't penalized, ridge regression chooses coefficients to minimize

∑i(yi−β0−zi⊤β)2+λ∥β∥22.\sum_i \left(y_i - \beta_0 - z_i^\top \beta\right)^2 + \lambda \lVert \beta \rVert_2^2 .

Larger λ\lambda shrinks the coefficients toward zero. The lab tries six values of λ\lambda and keeps the one with the lowest cross-validated error.

Standardizing uses the training fold's mean and SD. Python and R must agree on the SD convention (population or sample), or the same λ\lambda means a slightly different penalty in each.

03

Why every step sits inside the fold

Predict

Predict first

Suppose you imputed missing values with each column's mean over all 80 development groups, once, before making the folds. What happens to the fold scores?

The fold scores would look better than the test scoreThe fold scores would be unaffected
Reveal

They look better than they should. Each held-out fold helped set the fill-in values its own rows receive. The effect is small for a mean, but the same mistake with feature selection or an encoder can be large. The lab's assertions check that imputation used training rows only.

04

What the fold scores are for

Read

The five fold scores choose the penalty. They are not five independent experiments on new patients: the folds share groups' neighbors, the same simulated population and the same selection. Their average, read for the winning λ\lambda, is also optimistic, because it was the best of six.

The final test is the estimate. Once you have read it, it is spent. If you change the model because of it and score the test again, you can't call that test untouched.

This lab is deliberately not nested cross-validation. It uses one frozen external test; nested cross-validation, coming up, scores the whole selection procedure instead.

05

A shared fixture, not a shared seed

Read

The reference's Python program writes its simulated data and the split to a CSV file, and the R program reads that file. Setting the same integer seed in NumPy and in R would not have worked: the two random number generators draw different numbers from the same seed.

CodeSplash runs this lab as its own check in both engines and reproduces the reference's final-test RMSE in Python and in R. The agreement comes from identical inputs and matched conventions, not from luck with seeds.

06

Run Lab A in Predict

In the app

The same design, in the app:

  • Setup: choose the outcome and features, and set Groups (optional) to the group column, so a group's rows stay in one fold.
  • Validation: keep a Locked test set; with a Groups column, whole groups go to it. The split plan shows each fold's training and validation rows, and the entity counts show groups in training and in the test set.
  • Learners: use Elastic net with L1 share (ρ) at 0 for pure ridge, and tune Penalty (λ). Imputation and scaling run inside each fold.
  • Evaluate: freeze the plan, then reveal the test set once.

The walkthrough practices tuning on a model of {{target}}: folds score each setting, and the test stays untouched.

07

Check: Lab A

Checkpoint

Check yourself

Put Lab A's steps in order.

  1. 1Make group folds from the remaining groups
  2. 2Choose the penalty by the average fold score
  3. 3Fit imputation, scaling and ridge inside each training fold
  4. 4Score the final test once
  5. 5Reserve whole groups as the final test
  6. 6Refit the chosen pipeline on all development groups

Show the answer

The order:

  1. Reserve whole groups as the final test
  2. Make group folds from the remaining groups
  3. Fit imputation, scaling and ridge inside each training fold
  4. Choose the penalty by the average fold score
  5. Refit the chosen pipeline on all development groups
  6. Score the final test once

The test is set aside before anything is learned, and it is used once, after the choice is final.

Check yourself

Why does Lab A put whole groups, not single rows, into the final test?

Show the answer

Answer: b

The question is how the model does on new groups. A group with rows on both sides makes the test easier than the real task.

Check yourself

The winning penalty's average fold error is lower than its final-test error. What is the likeliest reason?

Show the answer

Answer: b

Picking the best of several scores on the same rows favors the lucky one. The once-scored final test isn't part of that choice.

Check yourself

After reading the final test, you switch from ridge to elastic net and score the test again. How should the report describe the second score?

Show the answer

Answer: b

The first reading shaped the second model. Report the history; a fresh test set is the only way back to an untouched estimate.

Check yourself

Why does the R version of Lab A read the Python program's CSV instead of simulating its own data with the same seed?

Show the answer

Answer: a

Different generators, different draws. Comparing engines needs identical inputs; matching conventions then make the outputs agree.

Check yourself

Is Lab A nested cross-validation?

Show the answer

Answer: b

Nested cross-validation scores the whole selection procedure on outer folds. Lab A uses one external test instead, which needs enough independent groups to spare.

08

Recap

Recap
  • Reserve whole groups for the final test before anything is fitted.
  • Choose with group folds, with imputation, scaling and the model all fitted inside each fold.
  • Fold scores choose; the once-read test estimates. A reread test is no longer untouched.
  • Python and R agree on Lab A because they read the same fixture, not because they share a seed.

Concepts in this lesson