The aim of learning is generalisation: good performance on new data. To estimate this honestly, the available data is divided into separate parts with separate jobs.
The Three Sets
The training set is the data from which the model learns its parameters, for example the slope and intercept of a line. The validation set is used while developing the model, to compare different algorithms or to tune hyperparameters (settings chosen by us, such as the number of neighbours k in KNN). The test set is kept locked away until the very end and is used once, to report the final performance. Because the test set never influenced any choice, its score is an unbiased estimate of performance on future data.
Typical Splits
Common splits are 60 percent training, 20 percent validation and 20 percent test, or 70/15/15. With little data, a simple 80/20 train and test split is used together with cross-validation instead of a fixed validation set. Data should be shuffled before splitting, unless it is a time series, in which case the test set must come after the training period. For classification, a stratified split keeps the class proportions the same in every part.
Data Leakage
Data leakage happens when information from the test set influences training, for example by scaling the whole dataset before splitting, or by tuning the model on the test score. Leakage makes results look better than they will be in real use. The rule is simple: fit every step, including scaling and feature selection, on the training data only, then apply it to the validation and test data.
Definition: Generalisation. The ability of a trained model to perform well on new, unseen data drawn from the same source as the training data.
Table 2.1: Roles of the three data sets
| Set | Purpose | Used when | Typical share |
|---|---|---|---|
| Training | Learn the parameters | During training | 60 to 70 percent |
| Validation | Choose the model and tune settings | During development | 15 to 20 percent |
| Test | Final unbiased estimate | Once, at the end | 15 to 20 percent |
Example 2.1: A 60/20/20 split of 100 records
Problem: Split 100 records into training, validation and test sets in the ratio 60 : 20 : 20 using two calls to a split function.
Solution:
- First hold out 20 percent as the test set: 20 records are tested later and 80 remain.
- The validation set must be 20 of the original 100, which is 20 / 80 = 25 percent of the remaining 80.
- Split the 80 records with a validation share of 0.25: 20 go to validation and 60 stay for training.
Result: Training 60, validation 20, test 20 records.
from sklearn.model_selection import train_test_split
data = list(range(100))
rest, test = train_test_split(data, test_size=0.2, random_state=0)
train, val = train_test_split(rest, test_size=0.25, random_state=0)
print("train", len(train), "validation", len(val), "test", len(test))
print("overlap:", len(set(train) & set(test)))
Output
train 60 validation 20 test 20
overlap: 0
Common mistakes
- Tuning the model on the test set, which turns it into a validation set.
- Scaling or selecting features on the full dataset before splitting.
- Randomly shuffling a time series so that the future leaks into the past.
- Using a test set too small to give a stable estimate.
Key takeaways
- Train to learn parameters, validate to tune, test once to report.
- A typical split is 60/20/20 or 70/15/15.
- Fit every preprocessing step on training data only.
- Stratify so that class proportions are kept in each part.
Practice questions
- What is the purpose of the test set?
- What is a hyperparameter?
- What is data leakage?
Answers to the practice questions
- To give a final, unbiased estimate of performance on unseen data.
- A setting chosen before training, such as k in KNN.
- Test information influencing training, which makes results too optimistic.
Sources
- Official BCA syllabus Introduction to Machine Learning, course code 26BCA404DSE21, Unit II
