38. Overfitting and Underfitting in Machine Learning
38.2 Spot It with Train vs Test Scores
Why. You spot the problem by comparing train vs test metrics — the habit from Chapters 31 and 37.
What. Fit once, score on X_train and X_test. A huge train–test gap often means overfitting. Both low often means underfitting (or bad features / too little signal).
How.
Compare two scores
- Train a model.
- Print accuracy (or other metric) on train and on test.
- Interpret the gap in one sentence.
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
df = pd.DataFrame({
"x1": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
"x2": [2, 1, 4, 3, 6, 5, 8, 7, 10, 9, 12, 11],
"y": [0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1],
})
X, y = df[["x1", "x2"]], df["y"]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=0)
deep = DecisionTreeClassifier(max_depth=None, random_state=0).fit(X_train, y_train)
print("deep train", accuracy_score(y_train, deep.predict(X_train)))
print("deep test ", accuracy_score(y_test, deep.predict(X_test)))
shallow = DecisionTreeClassifier(max_depth=1, random_state=0).fit(X_train, y_train)
print("shallow train", accuracy_score(y_train, shallow.predict(X_train)))
print("shallow test ", accuracy_score(y_test, shallow.predict(X_test)))
What you should see. A deep tree often nails train and may drop on test (overfit risk). A depth-1 tree may be weak on both (underfit risk). Exact numbers vary on this toy set — watch the pattern.