Sub topic 1
Training Score versus Validation Score
This chapter covers the first idea in the plan: why a model has two scores, how they differ, and what the gap between them tells you. Later chapters build learning curves and validation curves on this idea.
Prerequisites
This chapter assumes you know the following:
- Supervised learning basics: features, labels, and the split into training and test sets.
- Python and NumPy basics.
- scikit-learn estimators and their
fit,predict, andscoremethods. - A working idea of overfitting and hyperparameters.
You do not need to know about learning curves, validation curves, or cross-validation internals yet. Cross-validation is introduced briefly here and covered in its own chapter.
Two scores for one model
A fitted model can be scored on two kinds of data.
- The training score is the model's score on the same samples it was fitted on.
- The validation score is the model's score on samples it did not see during fitting.
Both scores use the same metric, such as accuracy. They answer different questions. The training score tells you how well the model reproduces the data it learned from. The validation score estimates how well the model will do on new data, which is what you care about in practice.
The training score is almost never a fair estimate of future performance. The model's parameters were chosen to fit those exact samples, so the score is biased upward. A model that memorizes its training set can reach a perfect training score and still fail on new data.
Why the gap matters
The difference between the two scores is called the generalization gap:
gap = training score − validation score
The size of the gap, together with the level of the validation score, tells you what is wrong with a model.
| Training score | Validation score | Gap | Typical reading |
|---|---|---|---|
| Low | Low | Small | The model is too simple, or the features lack signal. |
| High | Low | Large | The model fits noise in the training data. |
| High | High | Small | The model fits well and generalizes. |
The first row is underfitting. The second is overfitting. The third is the target. The next chapter treats these terms formally. Here, the point is that you cannot diagnose either problem from one number. A validation score of 0.80 means different things when the training score is 0.81 and when it is 1.00.
A first look in code
The following example fits a decision tree on the digits dataset and prints both scores. It holds out part of the data with train_test_split so that the validation score comes from unseen samples.
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, stratify=y
)
for depth in (2, 5, None):
model = DecisionTreeClassifier(max_depth=depth, random_state=0)
model.fit(X_train, y_train)
train_score = model.score(X_train, y_train)
val_score = model.score(X_val, y_val)
print(
f"max_depth={depth} train={train_score:.3f} "
f"validation={val_score:.3f} gap={train_score - val_score:.3f}"
)For classifiers, score returns accuracy. You should see three patterns:
- With
max_depth=2both scores are low, so the tree is too simple. - With
max_depth=5the training score rises and the validation score rises with it. - With
max_depth=Nonethe tree grows until it classifies every training sample correctly. The training score is 1.000 and the validation score is clearly lower, so the gap is the largest of the three.
Exact numbers depend on your scikit-learn version, but the pattern holds. One hyperparameter, max_depth, moved the model along the whole range from too simple to too flexible. A validation curve, covered later, automates this sweep.
How the two scores move with model flexibility
The training score rises, or at least does not fall, as a model gets more flexible. The validation score usually rises first and then falls once the model starts fitting noise. The diagram shows this typical shape.
The best model is usually near the middle stage, where the validation score peaks. It is not the model with the highest training score.
Why a single split is not enough
The example above used one split. A single validation score has two weaknesses.
- It depends on which samples landed in the validation set. A different random seed can shift the score by several points, especially on small datasets.
- It uses only part of the data for fitting, and only part for evaluation.
Cross-validation addresses both weaknesses. It splits the data into k folds. For each fold, it fits the model on the other k−1 folds and scores it on the held-out fold. You get k validation scores, and you can report their mean and spread. Scikit-learn's cross_validate can also return the training scores for each fold if you ask for them:
from sklearn.datasets import load_digits
from sklearn.model_selection import cross_validate
from sklearn.tree import DecisionTreeClassifier
X, y = load_digits(return_X_y=True)
results = cross_validate(
DecisionTreeClassifier(random_state=0),
X,
y,
cv=5,
scoring="accuracy",
return_train_score=True,
)
print("train :", results["train_score"].round(3))
print("validation:", results["test_score"].round(3))
print("mean gap :", (results["train_score"] - results["test_score"]).mean().round(3))Note the key name. Scikit-learn calls the held-out score test_score in the output, but in this context it is the validation score. Treat it as validation data, not as a final test set. The final test set is a separate hold-out that you touch only once, after all tuning is finished.
Common errors
These mistakes appear often when people first compare the two scores.
- Reading the training score as performance. The training score is useful only as a comparison against the validation score. On its own it says little.
- Reusing the validation data to make many decisions. Each time you pick a model or hyperparameter because of its validation score, the validation score becomes slightly optimistic. After many rounds, it no longer estimates new-data performance. Keep a separate test set for the final report.
- Fitting preprocessing on all the data. If you scale or select features using the full dataset before splitting, information from the validation samples leaks into training. This inflates the validation score. Put preprocessing inside a pipeline so it is refit on each training split.
- Ignoring the metric. A high accuracy on an imbalanced dataset can hide a model that never predicts the rare class. Choose a metric that matches the goal.
- Comparing scores from different splits. The gap is only meaningful when both scores come from the same model fit and the same metric.
What small and large gaps do not tell you
A small gap does not mean a good model. Two low scores with a small gap describe a model that is consistently poor. A large gap does not always mean you must simplify the model. More data often shrinks the gap, and stronger regularization can also help. Choosing among these remedies is the job of the learning curve and validation curve, which the following chapters build.
Also keep in mind that the training score can sometimes be lower than the validation score in a single fold. This happens by chance on small folds, or when training uses regularization, data augmentation, or dropout that makes the training pass harder than the evaluation pass. It does not by itself indicate an error.
Summary
- The training score measures fit to seen data. The validation score estimates performance on unseen data.
- The gap between them, read together with the level of the validation score, separates underfitting from overfitting.
- A single split gives a noisy validation score. Cross-validation gives several scores and a spread.
- Keep the final test set out of all tuning decisions.
Exercise
You fit a model and get a training accuracy of 0.99 and a validation accuracy of 0.71. A second model gets 0.64 on training and 0.62 on validation. Which model shows a large generalization gap, and what does the second model's result suggest?
Show answer
The first model has a gap of 0.28, so it is overfitting: it fits the training data much better than new data. The second model has a gap of only 0.02, but both scores are low, so it is underfitting. It is too simple or lacks informative features, and more training data alone is unlikely to fix it.
Exercise
In the cross_validate example, why does the output key test_score hold validation scores, and why should you still keep a separate final test set?
Show answer
Scikit-learn uses test_score as its generic name for the score on the held-out fold of each split. In a tuning workflow that fold plays the role of validation data. Because you choose models and hyperparameters based on these scores, they become optimistic over repeated use. A separate final test set, used once at the end, gives an unbiased estimate of performance on new data.