Sub topic 3
Train, validation, and test sets
This chapter assumes you know supervised learning basics (features, labels, a loss function) and scikit-learn's fit and predict workflow. It builds on the idea that a model's job is to generalise, meaning to predict well on data it has not seen.
Why one dataset is not enough
Training error tells you how well a model fits the data it learned from. It does not tell you how well the model will do on new data. A very flexible model can drive training error close to zero by memorising noise. To estimate generalisation, you must measure error on examples the model never trained on.
That leads to three roles for data:
| Set | Used for | Used how often |
|---|---|---|
| Training | Fitting parameters such as weights | Every training run |
| Validation | Comparing models and choosing hyperparameters | Many times |
| Test | Reporting the final performance estimate | Once, at the end |
A hyperparameter is a setting you choose before training, such as polynomial degree or regularisation strength. Parameters are learned. Hyperparameters are tuned.
Why the validation set and test set are separate
Each time you pick a model because it scored best on the validation set, you fit your choices to that set a little. After many comparisons, the best validation score is slightly optimistic. The test set stays untouched during all of this, so it gives an honest final number.
If you tune on the test set, it becomes a second validation set. Your reported score is then biased, and you have no clean data left to check it.
Splitting in practice
A common split is about 60% training, 20% validation, and 20% test. With little data, you may use a smaller validation share and rely on cross-validation, covered later. With very large data, a few percent each can be enough.
scikit-learn's train_test_split makes one split at a time, so call it twice to get three sets. The first call holds out the test set. The second call splits the remainder into training and validation.
import numpy as np
from sklearn.model_selection import train_test_split
rng = np.random.default_rng(0)
X = rng.uniform(-3, 3, size=(100, 1))
y = np.sin(X).ravel() + rng.normal(0, 0.2, size=100)
X_rest, X_test, y_rest, y_test = train_test_split(
X, y, test_size=0.2, random_state=0
)
X_train, X_val, y_train, y_val = train_test_split(
X_rest, y_rest, test_size=0.25, random_state=0
)
print(len(X_train), len(X_val), len(X_test))This prints 60 20 20. The second split uses test_size=0.25 because 25% of the remaining 80 examples is 20.
Common errors
- Random splits on ordered data. For time series, a random split lets the model train on the future and predict the past. Split by time instead, so validation and test data come after the training data.
- Imbalanced classes. A random split can leave a rare class almost absent from one set. For classification, pass
stratify=ytotrain_test_splitto keep class proportions equal. - Duplicates and groups. If the same patient, user, or document appears in several rows, put all of its rows in one set. Otherwise the model is scored on data it has effectively seen.
- Fitting preprocessing on everything. Computing scaling statistics on the full dataset before splitting lets information from validation and test data reach training. This is a form of data leakage, and a later chapter covers it.
- Peeking at the test set. Looking at test results and then changing the model counts as tuning on the test set. If you do it, treat the score as validation.
Reading the numbers
Compare training and validation error to see how the model behaves. Low training error with much higher validation error points to overfitting. High error on both points to underfitting. The next chapters use these comparisons to diagnose models.
Exercise
You have 1,000 examples. You try five polynomial degrees, pick the one with the lowest error on the test set, and report that error as your expected performance on new data. What is wrong, and what should you do instead?
Show answer
You used the test set to choose the degree, so it acted as a validation set and the reported error is optimistically biased. Hold out the test set first, split the rest into training and validation (or use cross-validation), choose the degree using validation error, and then score the chosen model once on the test set.