Sub topic 1
Training Score Versus Validation Score
This chapter covers the two scores that every learning curve and validation curve is built from. It explains what each one measures and why the difference between them matters.
Prerequisites
This chapter assumes you already know:
- Supervised learning basics: features, labels, and the split into training and test sets.
- Python and NumPy basics.
- How to use a scikit-learn estimator with
fit,predict, andscore. - A rough idea of overfitting and of what a hyperparameter is.
Two scores for one model
A model is fitted on one set of samples and judged on samples it has seen or has not seen. These give two different numbers.
- The training score is the model's score on the same data it was fitted on.
- The validation score is the model's score on held-out data that was not used for fitting.
The training score tells you how well the model can reproduce the data it was shown. The validation score estimates how well it will do on new data, which is what you care about. A model that memorizes its training set can have a perfect training score and a poor validation score.
Why the gap matters
The training score is almost always at least as high as the validation score. The size of the gap, and the level of both scores, tell you what is going wrong.
| Training score | Validation score | Gap | Typical meaning |
|---|---|---|---|
| Low | Low | Small | The model is too simple, or the features are weak |
| High | Low | Large | The model fits noise in the training data |
| High | High | Small | The model fits well and generalizes |
The next chapters give names to the first two rows (underfitting and overfitting). Here, the point is to look at both numbers together. A high training score alone says nothing about generalization.
Measuring both scores in code
You can see the gap with a single split. This example fits a decision tree on the digits dataset and scores it on both parts.
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
X, y = load_digits(return_X_y=True)
X_train, X_val, y_train, y_val = train_test_split(
X, y, test_size=0.25, random_state=0
)
model = DecisionTreeClassifier(random_state=0)
model.fit(X_train, y_train)
print("train:", model.score(X_train, y_train))
print("validation:", model.score(X_val, y_val))An unrestricted tree grows until it classifies every training sample correctly, so the training accuracy is 1.0. The validation accuracy is noticeably lower. That difference is the gap.
A single split is noisy, because the result depends on which samples landed in the validation part. Later chapters replace it with cross-validation, which repeats the split several times and averages the results.
Common errors
- Judging by the training score. Selecting a model because its training score is highest favors the model that memorizes the most.
- Reusing the test set. If you tune against the same data you report on, the validation score becomes optimistic. Keep a final test set that you use once.
- Comparing scores from different metrics. Both scores must use the same metric, such as accuracy, or the gap is meaningless.
Exercise
A model has a training accuracy of 0.99 and a validation accuracy of 0.71. Is the training score evidence that the model is good? What does the gap suggest?
Show answer
No. The training score only shows that the model reproduces the data it was fitted on. The large gap (0.28) suggests the model fits noise or details specific to the training set and does not generalize, so the validation score of 0.71 is the better estimate of performance on new data.