← All writing
Evaluation · 5 min read · 23 Jul 2026

Nested Cross-Validation: Why One Loop Isn't Enough

A single cross-validation loop tells you how well a tuned model performs on the same folds used to tune it. That number is optimistic, and nested cross-validation is the fix.

Cover image for the article: Nested Cross-Validation: Why One Loop Isn't Enough

The quiet leak in a familiar workflow

Most people learn cross-validation as a single, tidy loop: split the data into k folds, train on k-1, test on the held-out fold, average the scores, done. It feels rigorous because you are no longer testing on the training set. But the moment you introduce hyperparameter tuning into that same loop, the rigour quietly leaks away. If you try ten values of a regularisation strength, pick the one with the best average cross-validation score, and then report that score as your estimate of generalisation, you have used the test folds twice: once to choose the model and once to judge it. The score you report is no longer an estimate of how the model will do on new data; it is an estimate of how well you can find a hyperparameter that happens to suit those particular folds.

This matters because the bias is not obvious from the numbers themselves. A single-loop cross-validation score of 0.87 accuracy looks exactly as trustworthy as a nested one of 0.83, but only one of them was earned honestly. The gap grows with the number of hyperparameter combinations you search and shrinks with dataset size, which is precisely why it catches people out on smaller, noisier datasets where careful evaluation matters most and where the temptation to squeeze out a better-looking number is strongest.

A worked intuition

Imagine a dataset of 300 samples split into five folds of 60 each. You want to tune a single hyperparameter with ten candidate values. In the single-loop approach, for each candidate you run the standard five-fold procedure, average the five test scores, and end up with ten average scores, one per candidate. You pick the highest, say 0.85, and report that as the model's expected accuracy.

The problem is that all five folds contributed to choosing which candidate looked best. Even if none of the candidates was truly superior, ordinary sampling noise across folds means one of the ten will look best by chance, and it can look best by a meaningful margin when the dataset is small and scores are noisy. You have effectively run ten experiments and reported only the winner, which is the same statistical sin as multiple testing without correction. The reported 0.85 is inflated by exactly the amount of luck involved in that selection.

Nested cross-validation separates the two decisions cleanly. An outer loop, say five folds, is reserved purely for evaluation. Within each outer training set, an inner loop, say another five folds, is used to search the ten candidate hyperparameters and pick the best one using only that inner data. The chosen hyperparameter is then retrained on the full outer training set and tested once on the outer test fold, which the inner loop never saw. This happens five times, once per outer fold, and the five outer test scores are averaged to give the final estimate. Note that the inner loop may pick a different hyperparameter value in each of the five outer iterations, which is expected and fine: you are not trying to find one single best hyperparameter here, you are trying to estimate how well your entire tuning procedure generalises.

laptop showing data spreadsheet grid

What you actually get from each loop

It helps to be precise about what each version of cross-validation answers. Single-loop cross-validation with tuning answers: given that I search this hyperparameter space on this data, what is the best score I can find. Nested cross-validation answers: if I apply this entire pipeline, including its tuning step, to a fresh sample of data, what score should I expect. Only the second question is the one you actually care about when you write a report, choose between two candidate modelling approaches, or decide whether a model is ready to be trusted.

A practical consequence is that nested cross-validation is not meant to hand you a final tuned model. It is meant to give you a trustworthy estimate of performance for a modelling pipeline as a whole. Once you are satisfied with that estimate, you still run one more hyperparameter search, this time using all the data you have, to produce the actual model you deploy. The nested procedure and the final model-fitting step are separate jobs with separate purposes, and conflating them is another common source of confusion.

The computational cost is real and worth naming honestly. With five outer folds and five inner folds searching ten candidates, you are fitting roughly 5 times 5 times 10, that is 250 models, plus the five final refits on the outer training sets, compared with 50 fits for the naive single-loop search. For expensive models this cost is a genuine constraint, and it is why people sometimes settle for a single held-out validation set plus a separate held-out test set instead of full nested cross-validation. That compromise can be reasonable when data is abundant, but on smaller datasets the extra variance from a single split usually outweighs the computational saving.

The practical takeaway

If your pipeline involves any choice, a hyperparameter, a feature selection threshold, a choice of model family, made by looking at cross-validation scores, then those same folds cannot also be used to report your final performance number without bias. Nested cross-validation is not an academic nicety; it is the mechanism that keeps the tuning decision and the evaluation decision from contaminating each other.

In practice, use an outer loop purely to hold out data for honest scoring, an inner loop purely to search hyperparameters, and only report the outer scores as your estimate of generalisation. When someone shows you an impressively high cross-validation score alongside a wide hyperparameter search, it is worth asking a simple question: was the search inside the loop or outside it. That one question often explains more about whether a result will replicate than any amount of scrutiny of the model architecture itself.

scientist writing on whiteboard with diagram
← All writing See the project case studies →