Cross-validation is a technique that repeatedly holds back part of a dataset from training and uses it to test the model, giving a fairer estimate of how it will perform on new data. Instead of trusting a single train and test split, it rotates which data is held out and averages the results.
This page is for data scientists and technical leaders who need dependable model estimates. It covers why the technique exists, the main methods, how to choose between them, and how to avoid data leakage.
The scikit-learn cross-validation guide calls it a methodological mistake to learn a model's parameters and test it on the same data: a model that just repeated the labels it had seen would score perfectly yet predict nothing useful on unseen data. This is overfitting. The simplest fix is to hold out a test set. Tuning settings against that test set, however, lets knowledge about it leak into the model, so the metrics no longer report true generalisation.
A separate validation set solves the tuning problem, but the guide notes that splitting data into three sets drastically reduces the samples available for learning and that results can depend on one random choice. Cross-validation addresses both issues.
In k-fold cross-validation the training data is split into k smaller sets. The model is trained on k minus one of them and tested on the remaining one, and this repeats until each fold has served as the test set. The guide states that the reported performance is the average of the values computed in the loop. By default, scikit-learn does not shuffle the data for integer fold counts, so shuffling must be requested when the order of rows carries no meaning.
| Method | How it splits the data | Best suited to |
|---|---|---|
| K-fold | k folds, each used once as the test set | General data with independent rows |
| Stratified k-fold | Keeps about the same share of each class in every fold | Imbalanced classification |
| Leave-one-out | One sample is the test set each round | Very small datasets, at higher cost |
| Group k-fold | Keeps the same group out of both train and test | Repeated records per patient or customer |
| Time series split | Trains on earlier folds and tests on the next one | Time ordered data |
The guide cautions that leave-one-out builds n models from n samples, often gives a high variance estimate of test error, and that most authors and evidence suggest 5 or 10 folds should be preferred. For time series, it explains that shuffling time ordered data is likely to lead to an overfit model and an inflated validation score.
Preprocessing such as standardisation or feature selection must be learned from the training portion only and then applied to held-out data. The guide recommends wrapping preprocessing and model in a pipeline so that, under cross-validation, the scaler is refitted inside each fold. Fitting a scaler on all the data before splitting leaks test fold statistics into training.
| Approach | Strength | Limitation |
|---|---|---|
| Single hold-out split | Fast and simple | Result depends on one random choice |
| Cross-validation | Uses all data for both training and testing across rounds | Costs several training runs |
Teams building AI agents with machine learning components use the same discipline: test the underlying models on data they have not seen, and split by group or by time so that agents are not judged on records they learned from. Platforms such as ElixirClaw, XenonStack's AI agents computing platform, sit around models whose quality should be established this way.
To map one decision your agents make, and the data and policy it depends on, book a Context Workshop. See what happens in the workshop.
It is a way of testing a model on data it was not trained on, by rotating which part of the dataset is held out and averaging the results.
The data is split into k folds. The model trains on k minus one folds and tests on the remaining one, repeating until each fold has been the test set. The scores are then averaged.
Use it for classification with imbalanced classes. Each fold keeps about the same percentage of samples of each class as the full dataset.
Not usually. It builds one model per sample, costs more and often has high variance. Most authors suggest 5 or 10 folds instead.
Learn preprocessing from the training portion only. A pipeline refits steps such as scaling inside each fold so test data never influences training.