Decision Trees and Ensembles
How recursive binary splitting builds a tree, why the Gini index beats accuracy as a splitting criterion, and how bagging and random forests turn a high-variance learner into a strong one, with the split arithmetic worked out.
Prerequisites: The Bias-Variance Tradeoff
Every model so far fits one global formula over the whole predictor space. Decision trees do the opposite: they partition the space into rectangular regions and predict a constant in each. This handles interactions and non-linearities without anyone specifying them, and produces a model you can read aloud - at the cost of being, on its own, distinctly unreliable.
That last weakness turns out to be fixable in a way that makes trees the backbone of some of the strongest general-purpose methods available.
A. How a tree makes predictions
A tree asks a sequence of yes/no questions about the predictors. Each internal node tests one predictor against one threshold; each leaf carries a prediction - the mean of the training responses in that region for regression, the majority class for classification.
Predicting means walking from the root to a leaf. Nothing is computed; you just follow the branches. This is why trees are held up as interpretable: the path is the explanation.
B. Growing the tree
Finding the optimal partition is computationally infeasible, so trees are grown with a greedy procedure called recursive binary splitting.
At each step, consider every predictor and every possible cutpoint , splitting the current region into and . Choose the pair that most improves the criterion, make the split, then repeat independently within each new region.
It is greedy because it takes the best split available now, without checking whether a worse split now would enable a much better one later. It is top-down because it starts at the root and never revisits a decision.
For regression the criterion is RSS, summed over the two child regions:
C. Splitting criteria for classification
The obvious criterion is the misclassification rate. It turns out to be a poor choice, and seeing why is worth the detour.
The two standard alternatives, for a region with class proportions :
Both measure node purity: they are near zero when one class dominates and maximal when classes are evenly mixed. Both are also sensitive to changes in the proportions everywhere, whereas the misclassification rate only notices when the majority class flips. A split that redistributes observations without flipping either region's majority leaves the misclassification rate of the majority-vote prediction unchanged, but Gini and cross-entropy both register the real improvement - so they produce better trees.
The figure uses twenty observations, ten per class, a different sample from the ten-point split worked below. Drag the Split threshold: misclassification stays at 0.2000 across the five middle thresholds while the Gini index moves.
Interactive: the criterion that cannot see a better split
Twenty observations, ten of each class.
- Misclassification
- 0.2000
- Gini
- 0.3200
- Left node
- 8 / 2
- Right node
- 2 / 8
Misclassification is 0.2000 here, and it is 0.2000 at the four neighbouring thresholds too - flat across the whole middle of the sweep, because each step moves one observation of each class across the line and the majority never changes hands. Gini is 0.3200 and is not flat: slide to either end of the flat stretch and watch it fall.
D. Working one split by hand
Ten observations, five in each class. A candidate split sends 6 observations left (4 of class 1, 2 of class 0) and 4 right (1 of class 1, 3 of class 0).
For two classes, where is the proportion of class 1.
The parent node. , so
the maximum possible for two classes - a perfectly mixed node.
Left child. :
Right child. :
Weighted by region size, since a split's value depends on how many observations it affects:
The improvement:
The tree-growing algorithm computes exactly this for every pair and takes the largest .
Runs in your browser. The first run downloads the Python runtime (~10 MB), then it is cached.
E. Why a single tree is unreliable
Grown far enough, a tree can isolate every training observation in its own leaf and achieve zero training error. That is pure overfitting. But even a well-pruned tree has a deeper problem: high variance.
Because splitting is greedy and hierarchical, a small change in the data can change the root split - and every split beneath it is then chosen on differently partitioned data. Two random halves of the same dataset can produce trees that look nothing alike. In the language of The Bias-Variance Tradeoff, trees sit at the low-bias, high-variance extreme.
F. Bagging: averaging the variance away
If a method has high variance, average many instances of it. For independent fits each with variance , the mean has variance .
We only have one dataset, so bagging (bootstrap aggregation) manufactures many: draw bootstrap samples - sampling observations with replacement - grow a deep, unpruned tree on each, and average the predictions (or take a majority vote).
Deep trees are exactly right here. Each has low bias and high variance, and averaging attacks the variance while leaving the low bias intact.
Bagging also comes with a free validation set. Each bootstrap sample omits about a third of the observations - the probability a given observation is missed is . Predicting each observation using only the trees that did not see it gives the out-of-bag error estimate, at no extra computational cost.
G. Random forests: decorrelating the trees
Bagging has a limit. If one predictor is strongly dominant, it will be the root split in nearly every bootstrap sample, so the trees are highly correlated - and averaging correlated quantities reduces variance far less than averaging independent ones. (The same fact explained why LOOCV is noisier than 10-fold in Cross-Validation and Resampling.)
Random forests add one deliberate handicap: at each split, only a random subset of predictors is even considered, typically for classification. Most splits cannot use the dominant predictor at all, so other predictors get their turn, the trees become genuinely different, and the average is far more effective.
It is a striking idea: each individual tree is made worse on purpose, and the ensemble is better for it.
H. What you trade away
| Single tree | Bagging | Random forest | |
|---|---|---|---|
| Bias | Low | Low | Low |
| Variance | High | Reduced | Lowest |
| Interpretable | Yes | No | No |
| Tuning burden | Depth/pruning | , |
The interpretability that motivated trees is gone the moment you average hundreds of them. Variable-importance measures - how much each predictor reduced Gini across the forest - recover a summary, but not the readable if-then path.
Importance scores are not causal. A predictor can rank highly because it is correlated with the true driver, and impurity-based importance is biased towards high-cardinality predictors. Treat them as a description of what the model used, not of what matters in the world.
Key takeaways
- Trees partition the predictor space and predict a constant per region, grown by greedy recursive binary splitting.
- Gini and cross-entropy beat misclassification rate as splitting criteria because they respond to purity changes that do not flip the majority.
- Our worked split: parent Gini , children and , weighted , gain .
- Single trees are low-bias, high-variance - unstable under small data changes.
- Bagging averages deep trees over bootstrap samples, with out-of-bag error free; random forests additionally restrict each split to predictors to decorrelate the trees.
- The ensemble buys accuracy with interpretability.
What's next
Trees carve the input space with axis-aligned cuts. A different family builds flexible functions by composing simple non-linear transformations, and learns them by following the gradient of the error. That is Backpropagation and Gradient Descent.
References & further reading
- Gareth James, Daniela Witten, Trevor Hastie, Robert Tibshirani, An Introduction to Statistical Learning, with Applications in R, Springer (Springer Texts in Statistics 103), 2013source ↗
Copyrighted works are cited for reference only and are not hosted here; please consult the publisher for access.