Part 05 / Topic 4 of 7
Decision trees
A decision tree asks yes/no questions ("tenure < 12?") and routes each row down to a leaf with a prediction. Trees are readable and need no scaling — but grown without limits they memorise the training set:
deep = DecisionTreeClassifier(random_state=0) # no depth limit
shallow = DecisionTreeClassifier(max_depth=4, random_state=0)
# on digits: deep train 1.0 test 0.878 <- memorised, then stumbles
# depth 4 train 0.576 test 0.569 <- too simple for 10 classes
Perfect training accuracy with a test drop is the overfitting signature (more in Part 6). A single tree is rarely the final model — but it is the building block of the next two.
🧪 Quick quiz
- A tree scores 1.0 on training data and 0.878 on test data. What happened?
Show the answer
It overfitted — with no depth limit it memorised the training set. Limit depth, prune, or use an ensemble.
Done here? The practice set for Part 5 — 4 exercises with solutions — is on the part page.