Decision Trees, Bias, Variance, and Overfitting
About 26 min
Goal
By the end of this lesson, you can trace how a decision tree makes recursive splits, compare shallow and deep trees, and use training plus validation evidence to explain underfitting, overfitting, and tree instability.
Start with a visible decision process
Imagine deciding whether an outdoor event needs a tent. You might ask whether rain chance is above 40%, then whether wind speed is above 30 km/h.
A decision tree learns questions like these from data. At each node, it chooses a feature and a threshold that separate the current rows into smaller groups. Each child group can be split again. This repeated process is called recursive splitting.
A final leaf stores the prediction for rows that followed that path.
How does a tree choose a split?
A tree does not simply choose a question that “looks good.” It scores candidate splits using how mixed the resulting groups are.
Consider one feature x and six training rows:
| x | class |
|---|---|
| 1 | A |
| 2 | A |
| 3 | A |
| 4 | B |
| 5 | B |
| 6 | B |
For classification, one common mixing score is Gini impurity. A group containing only one class has impurity 0. A mixed group has a larger value.
Compare two candidate splits.
Split 1: x <= 3
- left:
A, A, A→ pure; - right:
B, B, B→ pure; - weighted impurity:
0.
Split 2: x <= 2
- left:
A, A→ pure; - right:
A, B, B, B→ mixed; - the right group's Gini impurity is
1 - (1/4)^2 - (3/4)^2 = 0.375; - because four of the six rows are on the right, the weighted impurity is
(4/6) × 0.375 = 0.25.
The first split is better under this criterion because it leaves less class mixing. Real trees evaluate many feature-and-threshold candidates, choose a strong current split, then repeat the process inside each child group. This greedy choice is useful, but it also helps explain why a small training-data change can alter the tree.
Tree depth controls how many questions can be chained
A depth-1 tree can make only one split. It may be too simple to capture an important pattern.
A deeper tree can carve feature space into many smaller regions. That extra flexibility can help, but it can also fit accidental details of the training sample.
This connects trees to bias and variance:
- a very shallow tree can have high bias and underfit;
- a very deep tree can have high variance and overfit;
- useful depth depends on data and evaluation evidence.
Read train and validation scores together
A shallow tree scoring 61% on training and 59% on validation data has a small gap, but both scores are weak.
A deep tree scoring 100% on training and 73% validation has a much larger gap. That is evidence that training-specific detail may not transfer.
Neither pattern is proof by itself. Leakage, tiny evaluation sets, distribution shift, and label noise can also create strange gaps.
Change tree depth in the Lab
- Click Run with
depths = [1, 3, None]. - Read
validation examples:and confirm that this model-selection set contains63rows. - Compare depth 1, depth 3, and the unrestricted tree on both training and validation results.
- Read
validation_correct. Depth 3 gets55of63rows correct, while the unrestricted tree gets54of63. The validation-accuracy difference is only one example. - Find
depths = [1, 3, None]. Change only the middle3to4. - Before running, predict that training accuracy will rise because the tree has more flexibility.
- Click Run. Compare the new training score and validation correct count with the depth-3 run.
- Press Reset afterward.
Python used here: None as an optional value and for loops over model settings (P1 — values and loops).
Loading lab…
The question is not “Which tree fits training best?” It is “Which amount of flexibility is supported by evidence that transfers?”
Quick Check
Key Takeaways
- Decision trees compare candidate feature-and-threshold splits using a criterion such as impurity reduction.
- Decision trees learn recursive feature-and-threshold splits.
- Shallow trees can underfit; very deep trees can overfit.
- Training and validation scores must be read together.
- A small train/validation gap is not useful when both scores are poor.
- Single trees can be unstable because small data changes can alter their structure.
- Before blaming model complexity, rule out leakage, an unrepresentative split, noisy labels, a tiny validation set, and patterns that do not repeat across folds.
Next Lesson
Next, you will reduce single-tree instability by combining many diverse trees into a Random Forest.
References
- scikit-learn, Decision Trees.
- scikit-learn, learning_curve.
Completion is stored locally on this device.