Skip to content
ml-recall

Topic notes

Decision Trees & Ensembles

Decision trees split greedily on impurity; bagging and random forests average many trees to cut variance, while boosting (AdaBoost, gradient boosting) adds trees in sequence to cut bias.

4-min read AI-checked

Practise 72 questions Take a test

Key ideas

Decision trees. A tree predicts by routing an example from the root to a leaf, answering one test per node; the leaf returns its majority class (classification), the class shares (predict_proba) or the mean target (regression)1. Most tests are axis-aligned (one feature against a threshold); oblique tests that combine several features also exist.

Bagging and random forests. Bagging trains each model on a bootstrap sample (nn draws with replacement; a point is missed with probability (1−1/n)n≈1/e(1 - 1/n)^n \approx 1/e, so about 63%63\% of the points are distinct) and averages the predictions, which mainly reduces variance; it suits strong, unstable learners such as deep trees2,4. A random forest adds a second source of randomness: each split considers only a random subset of features (max_features, often d\sqrt d for classification)2. Trees are independent, so they train in parallel2. More trees cost time but cannot make a random forest overfit; beyond some number the error just stops improving5. Extra-trees also draw the thresholds at random, which lowers variance a little more at a small cost in bias2.

Boosting. Weak learners are added one after another, each focusing on what the current ensemble gets wrong; it suits weak, high-bias learners such as stumps or shallow trees and mainly reduces bias2,4.

Combining models. Hard voting takes the majority label; soft voting averages predicted probabilities, and the two can disagree2. Stacking trains a meta-model on the base models' out-of-fold predictions2.

Feature importance. Impurity-based (MDI) importance comes from training-time splits and favours high-cardinality features, even pure noise; permutation importance on held-out data avoids this. When features are strongly correlated, permuting one barely hurts the model, so each gets a low permutation importance6.

Formulas

Worked example

A node holds 5 positives and 3 negatives; a split sends [4+,0−][4+, 0-] left and [1+,3−][1+, 3-] right.

In code

import numpy as np
from sklearn.tree import DecisionTreeClassifier

x = np.arange(1, 9).reshape(-1, 1)
y = np.array([1, 1, 1, 1, 0, 1, 0, 0])
stump = DecisionTreeClassifier(criterion="entropy", max_depth=1).fit(x, y)
t = stump.tree_
print(t.threshold[0])               # 4.5
print(t.impurity.round(3))          # [0.954 0.    0.811]  (root, left, right)
print(stump.predict_proba([[6]]))   # [[0.75 0.25]]  class shares in the leaf

Common pitfalls

References

  1. scikit-learn User Guide: Decision Trees — Sec. 1.10 (advantages and disadvantages); Sec. 1.10.5 Tips on practical use; Sec. 1.10.6 Tree algorithms; Sec. 1.10.7 Mathematical formulation; Sec. 1.10.9 Minimal Cost-Complexity Pruning
  2. scikit-learn User Guide: Ensembles — Sec. 1.11.1.2 GradientBoostingClassifier and GradientBoostingRegressor (1.11.1.2.3 Mathematical formulation, 1.11.1.2.5 Shrinkage via learning rate); Sec. 1.11.2 Random forests and other randomized tree ensembles (1.11.2.1–1.11.2.3); Sec. 1.11.3 Bagging meta-estimator; Sec. 1.11.4 Voting Classifier; Sec. 1.11.6 Stacked generalization
  3. Wikipedia: AdaBoost — Example algorithm (Discrete AdaBoost); Boosting as gradient descent
  4. Wikipedia: Bias–variance tradeoff — Approaches
  5. Google Machine Learning courses: Decision Forests, Clustering, GANs, Problem Framing — Decision Forests: Random forests — The clarity of noise (adding more trees)
  6. scikit-learn User Guide: Permutation feature importance — Sec. 5.2.2 Relation to impurity-based importance in trees; Sec. 5.2.3 Misleading values on strongly correlated features