Machine Learning · Chapter 23 of 40
Cross-Validation
CROSS-VALIDATION (CV) splits data into K folds, trains on K-1 and evaluates on the remaining fold — repeated K times.
More reliable than a single train/test split, especially on smaller data.
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.02Mean ± std across 5 folds.
Example 2 (python)
# For classification with imbalanced classes:
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 CV averages performance over K splits.
- Reduces variance in the estimate.
- Stratified CV preserves class ratios.
- Typical K: 5 or 10.
💡 Note: Never do CV on the test set — CV replaces the validation set, not the final held-out test.
