Data Science · Chapter 27 of 43

Cross-Validation

CROSS-VALIDATION splits data into K folds and rotates through them — every fold is used for training and testing.

More reliable than a single split, especially on small datasets.

Example 1 (python)
from sklearn.model_selection import cross_val_score
scores = cross_val_score(model, X, y, cv=5)
print(scores.mean(), scores.std())
Output
0.85 0.02

Mean ± std across 5 folds.

Example 2 (python)
from sklearn.model_selection import StratifiedKFold
kf = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)

Preserves class ratios per fold.

Key points

  • K-fold rotates train/test.
  • More robust than a single split.
  • Stratified CV preserves class ratios.
  • CV replaces validation — not the held-out test.
💡 Note: Don't reuse the same CV folds to select AND report — for the final number, evaluate once on a fully held-out set.

📝 Quick Quiz

1. 5-fold CV trains the model:

2. StratifiedKFold preserves:

3. CV is more useful when data is: