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.
What runs in CodeSplash
- Elastic net Verified in Python and R
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
The design
ReadLaboratory 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.
- Reserve the final test first: 20 whole groups, never split.
- Make five folds from the other 80 groups, each group in exactly one fold.
- Inside each training fold, fit mean imputation, then standardization, then ridge regression, and score the held-out fold.
- Choose the penalty with the best average fold score.
- Refit the chosen pipeline on all 80 development groups.
- Score the final test once.
The selection code receives development arrays only. The test rows aren't in its reach.
What ridge minimizes
ReadWith standardized features and an intercept that isn't penalized, ridge regression chooses coefficients to minimize
Larger shrinks the coefficients toward zero. The lab tries six values of 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 means a slightly different penalty in each.
Why every step sits inside the fold
PredictPredict 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?
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.
What the fold scores are for
ReadThe 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 , 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.
A shared fixture, not a shared seed
ReadThe 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.
Run Lab A in Predict
In the appThe 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.
Check: Lab A
CheckpointCheck yourself
Put Lab A's steps in order.
Show the answer
The order:
- Reserve whole groups as the final test
- Make group folds from the remaining groups
- Fit imputation, scaling and ridge inside each training fold
- Choose the penalty by the average fold score
- Refit the chosen pipeline on all development groups
- 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.
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