Course map
Stage 1: Foundations
Stage 2: Machine Learning
Stage 3: Deep Learning
Stage 4: LLMs & Generative AI
Stage 5: Production & Specialization

Part 06 / Topic 5 of 7

Cross-validation

One split gives one number; k-fold cross-validation gives k numbers and their spread. The data splits into k folds; each takes a turn as the validation set.

# examples/02_5_fold_cross_validation_gives_a.py (abridged)
cv = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)
scores = cross_val_score(model, X, y, cv=cv, scoring="roc_auc")
print("ROC-AUC per fold:", scores.round(3))
print(f"mean = {scores.mean():.3f}, std = {scores.std():.3f}")
# ROC-AUC per fold: [0.79 0.832 0.789 0.819 0.805]
# mean = 0.807, std = 0.017

Because model is a Pipeline, the imputer and scaler refit inside every fold — Part 5's leakage protection working as designed. Report mean ± std; a difference between two models smaller than their stds is noise.

Done here? The practice set for Part 6 — 4 exercises with solutions — is on the part page.

Search the guide