🎓 A1Learner
🔍
0 of 50 lessons completed

Python ML Overfitting and Underfitting

Overfitting is a model that memorises the training rows instead of learning the pattern. Underfitting is a model that misses the pattern altogether.

A deep tree memorises

A tree with no depth limit scores 1.0 on the training rows. Its test score is lower, so the gap is the warning sign.

from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
data = load_breast_cancer()
X_train, X_test, y_train, y_test = train_test_split(data.data, data.target, test_size=0.2, random_state=42)
from sklearn.tree import DecisionTreeClassifier
tree = DecisionTreeClassifier(random_state=42)
tree.fit(X_train, y_train)
print(round(tree.score(X_train, y_train), 3))
print(round(tree.score(X_test, y_test), 3))

A shallow tree misses the pattern

max_depth=2 keeps the tree too simple. Both scores sit at 0.93, so there is no gap, yet the scores are low.

from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
data = load_breast_cancer()
X_train, X_test, y_train, y_test = train_test_split(data.data, data.target, test_size=0.2, random_state=42)
from sklearn.tree import DecisionTreeClassifier
tree = DecisionTreeClassifier(max_depth=2, random_state=42)
tree.fit(X_train, y_train)
print(round(tree.score(X_train, y_train), 3))
print(round(tree.score(X_test, y_test), 3))

The middle depth does better

max_depth=5 learns more than the shallow tree. Its training score rises, and its test score stays close.

from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
data = load_breast_cancer()
X_train, X_test, y_train, y_test = train_test_split(data.data, data.target, test_size=0.2, random_state=42)
from sklearn.tree import DecisionTreeClassifier
tree = DecisionTreeClassifier(max_depth=5, random_state=42)
tree.fit(X_train, y_train)
print(round(tree.score(X_train, y_train), 3))
print(round(tree.score(X_test, y_test), 3))

Example

from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
data = load_breast_cancer()
X_train, X_test, y_train, y_test = train_test_split(data.data, data.target, test_size=0.2, random_state=42)
deep = DecisionTreeClassifier(random_state=42)
deep.fit(X_train, y_train)
shallow = DecisionTreeClassifier(max_depth=2, random_state=42)
shallow.fit(X_train, y_train)
print("Deep tree train accuracy:", round(deep.score(X_train, y_train), 3))
print("Deep tree test accuracy:", round(deep.score(X_test, y_test), 3))
print("Shallow tree train accuracy:", round(shallow.score(X_train, y_train), 3))
print("Shallow tree test accuracy:", round(shallow.score(X_test, y_test), 3))

Key points

  • Overfitting: a model that memorises training rows and fails on new rows
  • Underfitting: a model too simple to catch the pattern in the data
  • The symptom is the gap between training score and test score
  • A deep tree overfits. A very shallow tree underfits.
  • Use max_depth to move a tree between the two extremes
  • Compare train and test scores on the same split to see the gap
  • A good model keeps the gap small and the test score high

In this example, the breast cancer table is the test ground again, with benign or malignant as the label. The split holds back a fifth of the rows for testing. A deep tree with no depth limit scores 1.0 on the training rows and 0.947 on the test rows, so it memorised the training set. A shallow tree with max_depth=2 scores 0.93 on both sets, so it missed part of the pattern. The gap between the two scores is the symptom of overfitting.

Note

More training rows shrink the gap, but they do not fix a tree that is far too deep.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Python Playground

Exercise

What is the clearest symptom of overfitting?