Goal: Use three decision trees to discover what overfitting looks like.
Same dataset. Different levels of complexity.
| Tree | Training Accuracy | Test Accuracy |
|---|---|---|
| A | 82% | 80% |
| B | 94% | 89% |
| C | 100% | 71% |
Think: What makes this tree simple?
Think: What changed compared with Tree A?
Think: What happened?
The tree has become extremely good at fitting the training data…
…but performs much worse on new data.
max_depth?Simple tree
“Find broad patterns.”
Very complex tree
“Find increasingly specific rules for individual observations.”
The goal is not to maximize training accuracy at any cost.
The goal is to learn patterns that generalize to new data.
Decision trees have hyperparameters that control how complicated they can become.
max_depthMaximum number of levels in the tree.
DecisionTreeClassifier(max_depth=3)
Smaller max_depth → simpler tree
Larger max_depth → potentially more complex tree
min_samples_splitMinimum number of observations required to split a node.
DecisionTreeClassifier(
min_samples_split=10
)
If a node contains too few observations:
Don’t split it.
This prevents the tree from creating increasingly specific rules from tiny groups.
min_samples_leafMinimum number of observations allowed in a leaf.
DecisionTreeClassifier(
min_samples_leaf=5
)
Every terminal node must contain at least 5 observations.
“Don’t create a rule that applies to only one or two observations.”
Another way to control complexity is pruning.
Start with a tree and remove branches that don’t provide enough useful information.
Before pruning After pruning
A A
/ \ / \
B C B C
/ \ \ / \
D E F D E
/ \
G H
The result is a simpler tree that may generalize better.
Now reverse the goal.
Build a terrible decision tree.
Give each group a dataset.
Create a tree that:
Students may discover that they can keep splitting the data:
Feature?
/ \
/ \
Feature? Feature?
/ \ / \
... ... ... ...
Eventually, the tree can create leaves containing individual observations.
Training accuracy → 100%
But the tree may have learned:
“What happened to these exact observations?”
rather than:
“What general pattern exists in the data?”
If every additional split can improve training accuracy…
Why stop?
Because our real goal is not:
Fit the training data perfectly.
Our real goal is:
Perform well on unseen data.
This is the fundamental tradeoff behind model complexity.
| Underfitting | Good Fit | Overfitting |
|---|---|---|
| Simple | Balanced | Complex |
| Too few rules | Captures useful patterns | Memorizes details |
| Poor training accuracy | Good training accuracy | Excellent training accuracy |
| Poor test accuracy | Good test accuracy | Poor test accuracy |
Think of model complexity as a dial:
Low complexity High complexity
| |
v v
Underfitting -------- Good fit -------- Overfitting
The challenge is finding a level of complexity that captures meaningful patterns without memorizing noise.
If this is part of your course:
High bias
The model is too simple to capture the underlying pattern.
High variance
The model is overly sensitive to the particular training data.
Find a model that balances the two.
Start with:
from sklearn.tree import DecisionTreeClassifier
model = DecisionTreeClassifier(
max_depth=____
)
model.fit(X_train, y_train)
print(model.score(X_train, y_train))
print(model.score(X_test, y_test))
max_depthRun the model with:
max_depth = 1
max_depth = 2
max_depth = 3
max_depth = 5
max_depth = 10
Record your results.
max_depth |
Train Accuracy | Test Accuracy |
|---|---|---|
| 1 | ||
| 2 | ||
| 3 | ||
| 5 | ||
| 10 |
Look at the relationship between:
and
Ask yourself:
Does increasing
max_depthalways improve test accuracy?
Look for the point where:
That is an empirical demonstration of overfitting.
Training accuracy
^
| ______
| ____/
| ____/
| ____/
| ____/
|____/
+------------------------------> Complexity
Test accuracy
^
| ______
| __/ \__
| __/ \__
|_____ _/ \_
+------------------------------> Complexity
The model can continue getting better on training data even after its ability to generalize has peaked.
Answer these three questions individually.
Think about:
A decision tree has: 99% training accuracy but 68% test accuracy
Explain your reasoning in 1–2 sentences.
For example, think about:
max_depthmin_samples_splitmin_samples_leafDecision trees can become too ________.
A model that performs extremely well on training data but poorly on new data may be ________.
We can control tree complexity using ________.
The goal isn’t the biggest tree. The goal isn’t perfect training accuracy.
The goal is to find a model that learns useful patterns from the training data and generalizes to new observations.
Good machine learning is about generalization, not memorization.