Skip to main content
Albero Academy

Cross-Validation

A more reliable performance estimate than a single split.

Model Selection & Unsupervised9 min

A single train/test split is noisy — you might get a lucky or unlucky test set. Cross-validation splits the data multiple ways, trains and tests on each, and averages — giving a far more trustworthy estimate of performance.

K-fold cross-validation

cross_val_score
from sklearn.model_selection import cross_val_score
scores = cross_val_score(model, X, y, cv=5, scoring="accuracy")
scores.mean(), scores.std() # average performance and its variability

With cv=5, the data is split into 5 folds; each fold is the test set once while the other four train. You get five scores — their mean is your estimate, their spread tells you how stable it is.

Key takeaways

  1. 01Cross-validation averages performance over multiple train/test splits for a reliable estimate.
  2. 02K-fold: each of k folds is the test set once; average the k scores.
  3. 03The score spread tells you how stable the estimate is.
  4. 04Pass a pipeline so preprocessing is re-fit per fold (no leakage); stratify for classification.

Finished this chapter?

Mark it complete to track your progress through Scikit-learn.