Decision Trees
Recursive splitting on features to build interpretable if-then models, and how depth and leaf size control overfitting.
Beginner · about 5 hours · difficulty 2/5
Learn first: Framing ML Problems and the scikit-learn API
A decision tree asks a sequence of yes/no questions about features. Trees handle mixed feature types, need no scaling, capture interactions and are easy to visualize. On their own they overfit easily, but they are the building block of the strongest tabular models.
How trees learn
- At each node, choose the feature and threshold that best reduce impurity: Gini or entropy for classification, variance (MSE) for regression.
- Split greedily until a stopping rule is hit.
- Leaves predict the majority class or mean target of their samples.
Controlling complexity
max_depth,min_samples_leaf,min_samples_split,max_leaf_nodes.- Cost-complexity pruning (
ccp_alpha) trims branches after growing. - A fully grown tree memorizes training data: near-100% training accuracy, poor test accuracy.
Interpretation
Plot the tree with plot_tree or export rules. Impurity-based feature importances are convenient but biased toward high-cardinality features; prefer permutation importance for conclusions.
Good enough to move on when you can show the overfitting curve of a tree as depth grows and pick a depth by cross-validation.
Key concepts
- Gini, entropy and variance splits
- Depth and leaf-size limits
- Cost-complexity pruning
- Visualizing trees
- Feature importance caveats
Mini project
Train decision trees of depth 1 to 20 on the UCI Heart Disease dataset, plot train versus validation accuracy, and visualize the best tree's top three levels.
Check yourself
Why does a decision tree not need feature scaling, and why does it still overfit easily?
Free resources
- scikit-learn: Decision trees (scikit-learn)
- An Introduction to Statistical Learning (chapter 8) (ISL)