Train-test split

Also known as: training-validation-test split · holdout split

On this page 7

A train-test split is the practice of dividing a labeled dataset into disjoint subsets, one used to fit a model and another held back to estimate performance on unseen data. In the management of training data for artificial intelligence, it is the most basic safeguard against overfitting, because a model that is scored on the same examples it learned from will almost always look better than it really is.

The scheme is often extended into a training-validation-test split, also called a holdout split, where a third subset supports model selection. The core principle stays the same: the test set must stay untouched until the final evaluation, so that the reported score reflects generalization rather than memorization.

How a train-test split estimates generalization

A train-test split works by treating the held-out portion as a stand-in for future data. The model sees only the training subset while its parameters are learned, and the test subset then provides an unbiased estimate of the generalization error, provided both subsets come from the same underlying distribution.

The estimate is only as good as the separation. If information from the test subset reaches training, for example through feature scaling fitted on the full dataset or duplicated records, the score becomes optimistic. This failure is called data leakage, and it is the most common reason why a model that excels offline disappoints in production.

The table below summarizes the role of each subset in a three-way split.

SubsetPurposeUsed for decisionsTypical share
Training setFit model parametersYes, directly60-80%
Validation setTune hyperparameters and select modelsYes, indirectly10-20%
Test setReport final performance onceNo10-20%
Roles of the subsets in a holdout split

Choosing split ratios and sizes

Split ratios are a trade-off between learning and measuring. A larger training share gives the model more examples, while a larger test share narrows the confidence interval of the evaluation. Conventional ratios such as 80/20 or 70/15/15 are reasonable defaults for datasets of a few thousand to a few hundred thousand examples.

What matters for the test set is the absolute number of examples, not the percentage. With 100 million examples, a 1% test set still holds a million records, which is ample. With 500 examples, a 20% test set of 100 records gives an accuracy estimate with a standard error of several percentage points, and k-fold cross-validation is usually the better choice.

The following table gives practical starting points by dataset size.

Dataset sizeCommon splitRecommended practice
Under 1,000 examplesCross-validation plus small test setRepeated k-fold to reduce variance
1,000 to 100,00070/15/15 or 80/10/10Stratified holdout split
100,000 to 10 million90/5/5Fixed holdout with versioned indices
Over 10 million98/1/1 or smaller test shareTest set sized by required precision
Typical split choices by dataset size

Splitting strategies for different data structures

The correct splitting strategy depends on how the data were generated. A plain random split assumes that examples are independent and identically distributed, an assumption that many real datasets violate because records are grouped, ordered in time or heavily imbalanced.

Stratified splitting preserves class proportions in every subset, which is essential when a rare class makes up a few percent of the data. Group splitting keeps all records of one patient, user or document in the same subset. Temporal splitting trains on the past and tests on the future, which mirrors how the model will actually be used.

StrategyWhen it appliesRisk it prevents
Random splitIndependent, balanced recordsSimple selection bias
Stratified splitImbalanced class labelsMissing rare classes in the test set
Group splitSeveral records per entityIdentity leakage across subsets
Temporal splitTime-ordered dataTraining on information from the future
Out-of-distribution splitTesting robustness to new domainsOverstated real-world accuracy
Splitting strategies and when they apply

Train-test split compared with cross-validation

A single holdout split and k-fold cross-validation answer the same question with different costs. The holdout approach trains the model once, which suits large datasets and expensive models such as large neural networks. Cross-validation trains the model k times on rotating partitions, which gives a lower-variance estimate but multiplies compute cost.

In practice, the two are combined: cross-validation runs on the training portion for model selection, and a final untouched test set confirms the chosen configuration. For large pretraining corpora, a holdout split is the only realistic option, and the main concern shifts to deduplication between subsets, because near-identical web pages can appear in both.

Common mistakes and how to avoid them

Most evaluation errors come from subtle contamination rather than wrong ratios. The table below lists frequent symptoms, their causes and the standard fixes.

SymptomLikely causeFix
Test score far above production scorePreprocessing fitted on all dataFit scalers and encoders on training data only
Near-perfect accuracy on text or imagesDuplicates across subsetsDeduplicate before splitting
Score drops sharply after deploymentRandom split on time-ordered dataUse a temporal split
Results change with every runUnfixed random seed or tiny test setFix the seed and enlarge the test set
Test score improves after many experimentsRepeated tuning on the test setTune on validation data only
Frequent split errors and their remedies

The last row describes test set overfitting, a slow form of leakage that also affects public benchmarks. Each time a team checks the test score and adjusts the model, the test set leaks a little information. A strict rule of evaluating on the test set once per final model protects the estimate.

Explained without jargon

A train-test split works like studying for an exam. A student learns from practice questions. The real exam uses new questions the student has never seen. If the exam simply repeated the practice questions, a high score would prove memory, not understanding.

Machine learning follows the same idea. Most of the data is used for learning. A smaller part is locked away. At the end, the model answers the locked questions, and that score shows how well it will handle new cases.

A typical use case

A hospital data team builds a model that flags chest X-rays with signs of pneumonia. The dataset holds 60,000 images from 18,000 patients, and many patients have several scans. A naive random split would place images of the same patient in both subsets, so the model could recognize the patient rather than the disease.

The team therefore applies a patient-level group split with stratification by diagnosis: 70% of patients for training, 15% for validation and 15% for testing. All image normalization statistics are computed on the training set only, and exact and near-duplicate images are removed before the split.

Hyperparameters are tuned on the validation set, and the test set is scored once. The final sensitivity is about four percentage points lower than an earlier random-split experiment had suggested, and that lower figure matches performance in a later pilot at a partner clinic, which confirms that the careful split produced a realistic estimate.