Nested CV
Quick answer
Nested cross-validation separates hyperparameter selection from evaluation. Each outer split holds out evaluation data; the inner splits tune the candidate configuration using only outer-training data. Static examples tested locally with Python 3.13.5, scikit-learn 1.8.0 SciPy 1.17.0 and NumPy 2.3.5 where imported. ByteMind package execution is unverified.
Count the nested search fits
outer=3;inner=2;candidates=4print(outer*inner*candidates,outer*(inner*candidates+1))Observed local output:
24 27The first count excludes refits; the second includes one refit for each outer fold.
How it works
For each outer fold: (1) set aside its validation indices, (2) construct an inner splitter over the remaining rows, (3) compare all prespecified configurations by inner validation scores, (4) refit the selected configuration on the entire outer-training subset, and (5) evaluate once on the outer holdout. Repeat for each outer fold. The outer scores estimate the performance of the selection procedure at that outer training size, not the performance of a single final parameter value. Different outer folds may choose different settings. For grouped observations both inner and outer splits must respect group boundaries, with inner indices relative to the outer subset. Pipelines ensure transformations fit independently. After evaluation, choose a final configuration by rerunning the same prespecified inner search on all development rows and refit there. A separate sealed final test estimates this final fitted model; do not use outer scores to repeatedly redesign the search without acknowledging additional selection. With 3 outer folds, 2 inner folds and 4 candidates, inner evaluation needs 24 fits plus 3 outer refits, excluding final search. Nested CV is more expensive than ordinary CV but avoids evaluating a configuration on the same scores that selected it.
Execute a small nested group search
import numpy as npfrom sklearn.model_selection import GroupKFold,GridSearchCVfrom sklearn.tree import DecisionTreeClassifierg=np.repeat(np.arange(6),2);X=np.tile([[0],[1]],(6,1));y=X.ravel()for tr,va in GroupKFold(3).split(X,y,g): search=GridSearchCV(DecisionTreeClassifier(random_state=7),{"max_depth":[1,2]},cv=GroupKFold(2),scoring="accuracy").fit(X[tr],y[tr],groups=g[tr]) print(search.best_params_["max_depth"],search.score(X[va],y[va]))Observed local output:
1 1.01 1.01 1.0Both depths solve this toy pattern; the first grid entry wins ties. Outer groups remain unseen.
Common mistakes
Do not fit once and reuse across outer folds, tune using outer validation labels, or index the global group vector with local inner positions. Do not report the maximum inner score as the outer estimate.
Practice and next steps
Count fits for 4 outer folds, 3 inner folds and 2 candidates, including outer refits. Explain how you would fit a final model without selecting the outer fold with highest accuracy. Complete the knowledge check and staged exercise. Next: Repeated CV and dependence.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Nested CV — knowledge check
Apply nested cv to new scenarios.
Which labels select hyperparameters?
What do outer scores estimate?
How should inner group CV receive groups?
How fit the final model?
What is the cost of 2 outer, 3 inner, 4 candidates plus outer refits?