Training and Test Data: 4 Simple Rules for Honest Results

โšก TL;DR: Never judge a model on the data it learned from. Split your training and test data randomly into training (70โ€“80%) and test (20โ€“30%), keep the test set untouched until the very end, and add a validation set if you plan to compare more than one model.

Separating training and test data is the first rule of machine learning that actually has teeth. Break it and every number you report will be flattering and wrong. This page explains why, and how to split data properly.

The exam analogy

๐Ÿงช The intuition

A student revises using a practice paper with the answers printed at the back. They work through it repeatedly until they can reproduce every answer perfectly.

Do they understand maths? You cannot tell. They might have grasped the methods โ€” or memorised twenty specific answers. The only way to find out is to hand them a different paper.

That is exactly the situation with a trained model. Its accuracy on training data tells you almost nothing, because a high score there can be achieved two ways: by learning the real pattern, or by memorising the examples. Those look identical from the inside and behave completely differently on new data.

The basic training and test data split

70โ€“80%
Training set โ€” the model learns from these
20โ€“30%
Test set โ€” locked away until the end
Training and test data split shown as a bar divided into 70 percent training, 15 percent validation and 15 percent test
A typical training and test data split. The test portion stays untouched until the very end.

The essential property is that the test set must be genuinely unseen. The moment you train on it, it stops measuring anything useful โ€” you have handed the student the exam paper in advance.

โš ๏ธ Shuffle before you cut

If your spreadsheet happens to be sorted by price, taking the first 80% trains the model only on cheap houses and tests it only on expensive ones. Always randomise before splitting.

Where the validation set comes in

There is a subtler trap. Suppose you train a model, test it, get 82%, adjust a setting, retrain, test again, get 84% โ€” and repeat this twenty times, keeping whichever scores best.

You have now leaked the test set. Not by training on it directly, but by using it repeatedly to make decisions. Your final 91% is optimistic, because you selected the model that happened to suit those particular rows.

SetSizeUsed forHow often
Training60โ€“70%Adjusting the model’s parametersConstantly
Validation15โ€“20%Comparing settings and modelsMany times
Test15โ€“20%The final honest estimateOnce

The validation set absorbs the wear of repeated decisions. The test set stays clean because you touch it exactly once, when everything is finalised.

Cross-validation, when training and test data is scarce

If you only have 300 rows, carving off 20% leaves 60 test rows โ€” too few to mean much, since the result might swing wildly depending on which 60 you happened to pick.

โœ… K-fold cross-validation: Split the data into k equal parts (5 is common). Train k times โ€” each round, one fold is held out for testing and the rest are used for training. Every row gets to be test data exactly once. You end up with k scores.

Report their average and their spread. Scores of 84, 83, 85, 84, 83 mean the model is stable. Scores of 91, 68, 88, 72, 85 mean something is wrong โ€” your dataset is probably too small or too varied, and any single number you quote would be an accident of the split.

Four ways a training and test data split goes wrong

โš ๏ธ Leakage checklist

  • Scaling before splitting. Computing a mean across the whole dataset leaks test information into training. Split first โ€” see feature scaling.
  • Duplicate rows. The same record in both sets means you tested on training data. De-duplicate first.
  • Random splits on time series. A random split lets the model train on Thursday and Saturday to predict Friday โ€” it has seen the future. Split chronologically instead.
  • Grouped data. Forty photos of each of fifty patients, split randomly, puts the same patient on both sides. The model recognises the patient, not the condition. Split by group.

Reading your training and test data scores together

The gap between training and test scores is diagnostic:

TrainingTestWhat it means
HighHighWorking. Ship it.
HighLowOverfitting โ€” memorised rather than learned
LowLowUnderfitting โ€” model too simple, or features too weak
LowHighAlmost always a bug in your split or evaluation code

That last row surprises people, but a test score meaningfully above the training score is a red flag rather than good luck. Check for leakage or a mistake in how you compute the metrics.

A note on accuracy

Splitting correctly gives you an honest number. It does not guarantee the number is meaningful. On imbalanced data โ€” fraud, rare diseases โ€” a model that always predicts “no” can be 99% accurate and useless. Once your split is right, learn to read the confusion matrix, then precision and recall.

๐Ÿ”‘ Key Takeaways

  • Split your training and test data randomly, before you do anything else to it.
  • The test set is used once. Use a validation set for every decision before that.
  • With little data, use k-fold cross-validation and report the spread, not just the average.
  • Time series split chronologically; grouped data splits by group.
  • A test score above your training score means a bug, not luck.

Further reading

In practice you will split training and test data with a library rather than by hand: see scikit-learn’s train_test_split and its cross-validation guide, which covers grouped and time-series splitting properly.

Where to go next

Scroll to Top