Cross-validation
Cross-validation estimates how well a model will work on new data by repeatedly training it on part of the data and testing it on the part held back, then averaging the results.
Testing a model on the same data it learned from tells you nothing, because a model that simply memorised its training data would score perfectly and fail on anything new. The standard answer is to hold some data back as a test set. But if you also use held-back data to tune the model's settings, information about it leaks in, and a single split wastes data and makes the result depend on which records happened to land where. Cross-validation addresses both problems.
The most common form is k-fold cross-validation. The training data is split into k equal parts, or folds. The model is trained on all but one fold and tested on the one left out, and this is repeated until every fold has been the test fold once. The average score is the estimate. scikit-learn's documentation uses five folds in its examples and notes that most authors recommend five or ten. Its classic example, a classifier on the iris flower dataset, scores between 96% and 100% across the five folds, and the spread itself is useful information.
Variants handle awkward data. Stratified k-fold keeps each class's share the same in every fold; in scikit-learn's example of 45 negative and 5 positive cases, plain folds produced test folds with no positives at all. Group-aware splits keep all records from, say, one patient in the same fold, so the model is not tested on someone it has already seen. For data ordered in time, the split must always train on the past and test on the future, which is the idea behind backtesting.
Two rules prevent misleading results. Any data preparation, such as scaling or feature selection, must be learned from the training folds only, or test information leaks in; scikit-learn recommends wrapping these steps in a pipeline. And a final test set should still be kept aside for one last check. Because k-fold training repeats the work k times, it is used mostly with models that are cheap to train, and much less with very large models.
The data is divided into k parts. The model is trained k times, each time leaving out a different part and testing on it, and the k scores are averaged. With k set to 5, each record is used for testing once and for training four times. A single split wastes data that could be used for training, and the result depends on which records happened to fall in the test set. Cross-validation uses all the data for both training and testing across different rounds, giving a steadier estimate. Yes. Cross-validation is usually used to compare models and tune their settings, which means its results have influenced choices. A separate test set, untouched until the end, gives an unbiased final check. Five or ten folds are the common choices. More folds use more data for training each time but take longer to run; leave-one-out, which uses one record per fold, is expensive and often gives noisy estimates.Common questions
What is k-fold cross-validation?
Why not just use a single train and test split?
Do you still need a test set if you use cross-validation?
How many folds should you use?
Search for "k-fold cross-validation", "stratified cross-validation" or "cross-validation data leakage". The scikit-learn user guide, statistics and machine learning textbooks and university course notes give clear, tested explanations. In research papers, check whether data preparation was done inside each fold and whether a separate test set was kept, since these are common sources of inflated results.Finding more
Working out what this means for your business?
Aileron Shift builds the systems behind this site: AI newsrooms that research, write and publish on their own, marketing automation and WordPress hosting for companies that need to move faster than their competitors.