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.
- Growing: top-down and greedy. At each node, try every candidate split (for a numeric feature, thresholds halfway between sorted neighbouring values) and keep the one with the lowest weighted child impurity, i.e. the highest information gain1. The search scores every candidate, so it returns the best candidate, not a local maximum. Finding the optimal tree is NP-complete, so greedy algorithms such as ID3 and CART give no optimality guarantee1.
- Splits depend only on the order of feature values, so standardising or negating a feature changes the thresholds but not the partition: trees need no feature scaling1.
- Trees are piecewise constant: they cannot extrapolate beyond the target range seen in training1.
- Complexity control: a deep tree overfits (it can memorise the training set); a too-shallow tree underfits (poor on training and test data). Regularise with
max_depth,min_samples_leaf, a minimum impurity decrease, or post-pruning on a validation set or by cost-complexity pruning with chosen by cross-validation1. Pruning exists to fight overfitting. - Strengths: interpretable if–then rules; weaknesses: high variance, since small data changes can give a very different tree1.
Bagging and random forests. Bagging trains each model on a bootstrap sample ( draws with replacement; a point is missed with probability , so about 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 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.
- AdaBoost reweights the training points: misclassified points are multiplied by the same factor , correct ones by 3. The final classifier is a weighted vote, so its boundary is far richer than one stump's. AdaBoost minimises the exponential loss3.
- Gradient boosting fits each new tree to the negative gradient of the loss (for squared loss: the residuals ) and adds it scaled by the learning rate 2. A smaller needs more trees for the same training error2; as a rule of thumb, halving roughly doubles the number of trees, since each step is half as long. Gradient boosting uses shallow trees and can overfit as trees are added, so the number of trees is chosen by early stopping on validation data2. Trees are built sequentially, so libraries parallelise the split search inside each tree.
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
- Node class shares . Entropy (0 for a pure node, 1 bit for a 50/50 binary node); Gini 1.
- Information gain 1.
- Cost-complexity pruning: , with the number of leaves1.
- AdaBoost round : , , , then renormalise3. Since , . After renormalising, the misclassified points hold exactly half the weight, so has error and cannot be picked in the very next round.
- Gradient boosting: , starting from a constant (the mean for squared loss)2.
- Averaging predictors whose errors have mean 0, variance and pairwise covariance : expected squared error (expand the variance of a mean). Correlated members ( near ) gain little, which is why random forests decorrelate their trees.
- Majority vote of independent classifiers with error : the vote is wrong when more than half are wrong, a binomial tail. Three members with give .
Worked example
A node holds 5 positives and 3 negatives; a split sends left and right.
- Parent: bits; Gini .
- Children: left is pure (); right , Gini .
- Weighted child entropy , so the information gain is bits. Weighted Gini .
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
- High entropy means an impure node; trees pick the split with the largest information gain (smallest weighted child impurity).
- Bad accuracy on both training and test data means the tree is too shallow (underfitting), not overfitting.
- More trees never make a random forest overfit, but they can make gradient boosting overfit; the learning rate belongs to boosting, not to random forests2,5.
- A boosted classifier is a weighted vote of weak learners, not a weak learner with new parameters; AdaBoost's votes are never negative.
- In AdaBoost, the weighted error of each newly chosen weak learner tends to increase over rounds, because the weights concentrate on hard points.
- Averaging probabilities is not guaranteed to beat the best member's accuracy; it helps most when members are accurate and their errors are weakly correlated.
- Standardising features is pointless for trees, unlike for -NN or SVMs.
References
- 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
- 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
- Wikipedia: AdaBoost — Example algorithm (Discrete AdaBoost); Boosting as gradient descent
- Wikipedia: Bias–variance tradeoff — Approaches
- Google Machine Learning courses: Decision Forests, Clustering, GANs, Problem Framing — Decision Forests: Random forests — The clarity of noise (adding more trees)
- 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