Training, Validation and Test Sets Explained
A machine learning model that scores perfectly on the data it was trained on tells you almost nothing. What matters is how it performs on data it has never seen. To measure that fairly, practitioners divide their data into separate parts, each with its own role.
The three standard parts are the training set, the validation set and the test set. Understanding what each one is for, and keeping them apart, is one of the most important habits in the field.
The training set
The training set is the portion of data the model learns from. The algorithm looks at these examples, adjusts its internal parameters and gradually improves its predictions.
It is normally the largest piece, because more examples usually give the model more to learn from. But performance on the training set is a poor guide to quality. A model can memorize its training data, including noise, and score impressively there while failing on anything new. This problem is known as overfitting.
The validation set
The validation set is held back during training and used to make choices about the model. After each round of training, you check how the model does on this separate data. It answers questions such as:
- Which algorithm works best for this problem?
- What settings, called hyperparameters, should I use, such as tree depth or learning rate?
- When should training stop?
- Which features are worth keeping?
Because the model does not learn its parameters from validation data, results there give a more honest signal than training results. If training performance keeps improving while validation performance stalls or falls, you are probably overfitting.
The test set
The test set is the final exam. It is kept completely out of the process until you have made all your decisions. You evaluate once, on the chosen model, to estimate how it will behave on genuinely new data.
The reason for a separate test set is subtle. Every time you adjust something based on validation results, you leak a little information about the validation set into your choices. After many rounds, the validation score becomes slightly optimistic, because you effectively tuned to it. The test set, never used for tuning, gives an unbiased check.
If you peek at the test set repeatedly and keep adjusting, it stops being a test set and becomes another validation set.
A simple analogy
Picture a student preparing for an exam. The textbook problems are the training set. Practice exams used to find weak spots and adjust study habits are the validation set. The real exam, seen only once at the end, is the test set. If the student saw the real exam questions while studying, the final grade would say nothing about their ability.
How to split the data
Typical splits give most of the data to training and smaller shares to the other two, such as roughly sixty to eighty parts training, with the rest divided between validation and test. There is no universal rule. With very large datasets, smaller validation and test shares can still be big enough to be reliable. With small datasets, the split matters more and holds trade-offs.
from sklearn.model_selection import train_test_split
X_temp, X_test, y_temp, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
X_train, X_val, y_train, y_val = train_test_split(
X_temp, y_temp, test_size=0.25, random_state=42
)
Several details deserve attention:
- Shuffle the data before splitting, unless order matters.
- Stratify classification splits so each part keeps the same class proportions, which matters when one class is rare.
- Respect time. For forecasting, split by date so training uses the past and testing uses the future. Random shuffling would let the model peek ahead.
- Group related records. If the same customer or patient appears many times, keep all of their rows in one set so the model is not tested on people it already saw.
- Fix a random seed so the split can be reproduced.
Cross-validation for small datasets
When data is limited, setting aside a validation set wastes precious examples. Cross-validation offers an alternative. The training data is divided into several folds. The model trains on all but one fold, validates on the held-out fold, and the process rotates until every fold has served as validation. The scores are averaged for a more stable estimate.
You still keep a separate test set outside this process for the final check.
Common mistakes
Watch out for these:
- Preprocessing before splitting. Fitting scalers, imputers or encoders on the entire dataset lets information from validation and test data leak into training. Fit them on training data only, then apply them to the others.
- Duplicates across sets. Identical or near-identical rows in both training and test sets inflate scores.
- Tuning on the test set. Looking at test results to guide decisions defeats its purpose.
- Unrepresentative splits. If the test data does not resemble real-world use, the score will mislead.
- Ignoring variability. A single split can be lucky or unlucky. Repeating with different splits shows how stable your results are.
After the final test
Once you have chosen a model and measured it on the test set, some teams retrain on training and validation data combined, then deploy. That can squeeze out a bit more performance, though the test score then describes a slightly different model. In production, keep monitoring, since real data can drift away from what the model saw.
Frequently Asked Questions
Can I use the validation set and test set interchangeably?
No. The validation set guides choices and so becomes slightly familiar to the process. The test set must stay untouched until the end to give an unbiased estimate. Mixing them removes that protection.
What split ratio should I use?
There is no single correct ratio. Common choices give most data to training and smaller parts to validation and test. Larger datasets can afford smaller held-out shares, while small datasets often benefit from cross-validation.
Why does my model score well in testing but poorly in real use?
Frequent causes are data leakage, test data that does not match real conditions, or data that changed over time. Review how features were built and how the split was done, and compare the test data with live data.