Cross-Validation Techniques for Hyperparameter Tuning
Cross-validation estimates performance under a resampling assumption. More folds do not repair a wrong assumption: the split unit must match how future data arrives.
1. K-fold and stratified K-fold
KFold suits approximately IID regression or balanced classification. StratifiedKFold preserves class proportions and stabilizes classification metrics, but it does not prevent the same patient, customer, device, or document family from appearing on both sides.
Shuffling is appropriate only when order carries no deployment information. Fit preprocessing and feature selection inside every fold through a Pipeline.
2. Grouped and temporal data
Use GroupKFold or StratifiedGroupKFold when entities repeat. Keep every entity in one side of a split. For forecasting or drift-sensitive evaluation, use chronological splits such as TimeSeriesSplit and ensure features use only information available at prediction time. Ordinary random folds can leak future conditions.
Leave-one-out CV has high computational cost and can have high variance; it is not automatically the most accurate choice. Repeated K-fold often gives a more useful view of split sensitivity for IID data.
Choose the splitter from the prediction unit, not from class balance alone:
| Deployment question | Appropriate starting point | Main failure to prevent |
|---|---|---|
| New IID rows from the same population | shuffled K-fold, stratified for classification | unstable class proportions or fold-local preprocessing leakage |
| New observations from previously seen entities | an entity-aware design matching that contract | accidental evaluation of a different deployment task |
| Entirely new patients, customers, devices, or sites | grouped folds | the same entity appearing in train and validation |
| Future periods | expanding or rolling chronological splits | training on information from the future |
Stratification is a variance-control device, not a replacement for grouping or chronology. When several constraints coexist, use a splitter that preserves the most important deployment boundary and document any remaining mismatch.
3. Nested cross-validation
The inner loop selects hyperparameters. The outer loop evaluates that entire selection procedure on data not seen by the inner search. The result is an estimate of the procedure, not one final production model.
import numpy as np
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import GridSearchCV, StratifiedKFold, cross_validate
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
X, y = load_breast_cancer(return_X_y=True)
pipeline = Pipeline(
[("scale", StandardScaler()), ("svc", SVC(kernel="rbf"))]
)
search = GridSearchCV(
pipeline,
param_grid={
"svc__C": [0.1, 1.0, 10.0],
"svc__gamma": [0.001, 0.01, 0.1],
},
scoring="roc_auc",
cv=StratifiedKFold(n_splits=4, shuffle=True, random_state=1),
n_jobs=1,
)
outer = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)
result = cross_validate(
search,
X,
y,
scoring={"auc": "roc_auc", "balanced_accuracy": "balanced_accuracy"},
cv=outer,
n_jobs=1,
return_estimator=True,
)
print("Outer ROC AUC:", np.round(result["test_auc"], 3))
print("Mean outer ROC AUC:", round(result["test_auc"].mean(), 3))
print(
"Mean outer balanced accuracy:",
round(result["test_balanced_accuracy"].mean(), 3),
)
assert result["test_auc"].shape == (5,)
assert len(result["estimator"]) == 5
Each returned estimator contains an independently selected best_params_; variation across outer folds is informative. After evaluation, choose a documented final search protocol and fit it on all available development data. Keep a separate temporal or operational test when the application requires one.
4. Early stopping inside CV
Vendor boosting estimators often require an explicit eval_set. Passing one fixed validation set into every GridSearchCV fit exposes the same observations to all folds and is not nested early stopping. Use a fixed round count during ordinary CV, or implement an inner train/stopping split inside each fold. The outer fold must remain invisible to both tuning and stopping.
5. Metrics and uncertainty
Predeclare the primary metric. For classification, distinguish ranking, probability quality, and thresholded decisions. Report outer-fold results rather than the optimistic inner best_score_; folds overlap in training data, so avoid treating a naive standard error as a rigorous confidence interval.
Outer-fold variation combines training-set changes, validation-set changes, and possibly different selected hyperparameters. That variation is useful evidence about procedure stability. Inspect whether one group, time period, or rare class drives the spread, and report the individual outer results when the number of folds is small rather than hiding them behind excessive decimal precision.