Sub topic 1
Why we hold data out
Prerequisites
This chapter assumes you can read basic Python and NumPy code. It also assumes you know what a feature matrix and a label vector are. Finally, you should know that a supervised model learns a mapping from features to labels. You do not need any scikit-learn experience yet.
The question behind every split
A model is useful only if it works on data it has not seen. A spam filter is judged on tomorrow's email, not on the messages it was trained on. This ability to perform well on new data drawn from the same source is called generalization.
The difficulty is that you cannot measure performance on future data before it exists. You need a stand-in. The standard stand-in is a held-out set: a portion of your labeled data that you set aside and never train on. You score the model on that portion, and the score estimates how it will do on new data.
Why training scores mislead
A model is fit by minimizing error on the training data. A flexible model can reduce that error by memorizing individual rows, noise included. Memorized noise does not repeat in new data, so a high training score does not tell you the model will generalize.
The following code shows this with a decision tree that has no depth limit. It is fit on half the data and scored on both halves.
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
X, y = load_breast_cancer(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.5, random_state=0
)
tree = DecisionTreeClassifier(random_state=0)
tree.fit(X_train, y_train)
print("Train accuracy:", tree.score(X_train, y_train))
print("Test accuracy:", tree.score(X_test, y_test))The training accuracy is exactly 1.0 because the tree grows until it fits every training row. The test accuracy is lower. The gap between the two numbers is the signal. Without the held-out half, you would have reported 100% and been wrong.
The workflow in one picture
The diagram shows the basic idea. The model only ever learns from one side of the split. The other side is used to score it.
What the gap tells you
Comparing the two scores gives a quick diagnosis.
| Training score | Held-out score | Likely situation |
|---|---|---|
| High | High | The model generalizes |
| High | Much lower | Overfitting: the model memorized training data |
| Low | Low | Underfitting: the model is too simple or the features are weak |
The next chapter covers overfitting and underfitting in more detail. For now, the point is that only the held-out score is trustworthy, and the gap is the thing to watch.
Common errors
- Scoring on training data and reporting it. This gives an optimistic number, often a perfect one for flexible models.
- Holding out data but then using it to make decisions. If you tune the model until the held-out score looks good, that score stops being an honest estimate. Later chapters cover validation sets and test-set discipline for this reason.
- Assuming the held-out set resembles the future. The estimate is only as good as the match between your held-out data and the data the model will see in use.
Exercise
A model scores 99% accuracy on its training data and 71% on held-out data. A teammate says the 99% figure shows the model is ready. What do you tell them, and which number should you report as the expected accuracy on new data?
Show answer
The 99% figure measures how well the model fits data it has already seen, so it says little about new data. The large gap suggests overfitting. Report the 71% held-out accuracy as the estimate for new data, and treat it as an estimate with some uncertainty.