Skip to content

Cross-Validation

For people

How k-fold cross-validation gives a more reliable estimate of model performance, and the variants for grouped and time-series data.

Editorial team 2 min read

Share X LinkedIn Reddit Bluesky Email

A single train/validation split can be lucky or unlucky. Cross-validation averages over several splits for a steadier estimate.

k-Fold Cross-Validation

  1. Split the training data into k equal parts (folds), often 5 or 10.
  2. Train on k−1 folds and evaluate on the remaining fold.
  3. Repeat k times, holding out each fold once.
  4. Report the mean and spread of the k scores.

The spread matters: if scores vary widely between folds, the model's performance is unstable.

Variants

  • Stratified k-fold keeps class proportions the same in every fold — important for imbalanced data.
  • Group k-fold keeps all rows from one group (a patient, a customer, a store) in the same fold, preventing leakage between related records.
  • Time-series split always trains on earlier data and validates on later data.
  • Leave-one-out uses every single row as a validation set once; thorough but expensive.

Using It for Tuning

Cross-validation is the standard way to compare models and tune hyperparameters (for example with GridSearchCV). Keep a separate test set untouched until the very end.

Avoid Leakage

Any preprocessing learned from data — scaling, imputation, feature selection — must happen inside each fold. Wrapping steps in a scikit-learn Pipeline ensures this.

from sklearn.model_selection import cross_val_score
scores = cross_val_score(pipeline, X, y, cv=5, scoring="f1")
print(scores.mean(), scores.std())

More in Machine learning

All Machine learning guides →
Machine learning Guide · 2 min

Linear Regression Explained

The simplest predictive model: how linear regression fits a line through data, how to read its coefficients, and when it breaks down.

Machine learning 2 min read 25 Sep 2026 For people

Machine learning Guide · 2 min

Logistic Regression for Classification

Despite its name, logistic regression is a classification method. How it produces probabilities and why it remains a strong baseline.

Machine learning 2 min read 24 Sep 2026 For people

Machine learning Guide · 2 min

Decision Trees

How decision trees split data with simple questions, why they are easy to explain, and why single trees overfit.

Machine learning 2 min read 23 Sep 2026 For people

Machine learning Guide · 2 min

Random Forests

Why averaging many randomised decision trees produces a robust, accurate model with little tuning.

Machine learning 2 min read 22 Sep 2026 For people

Discussion 0

Use @handle to bring somebody in, and #tag to file it.

Nothing here yet. Ask a question, or say how you used it.

Sign in to join the discussion. Sign in