CV Courseversity

Decision Trees and Ensemble Learning

Covers how decision trees recursively split data using impurity measures, how bagging and random forests combine many trees to reduce variance, and how boosting and stacking combine diverse models to reduce error further.

“A single decision tree trained to predict loan default draws one confident, easy-to-read set of if-then rules — and then a slightly different random sample of the same applicants produces a completely different tree, with a different question at the very first split. Do you trust that one tree's story, or do you grow five hundred slightly different trees on slightly different data and let them vote?”

Decision Trees: Recursive Splitting and Impurity · 15 min

A decision tree classifies an example by asking a sequence of simple questions about its features, one at a time, following a path down the tree until it reaches a leaf that assigns a label. The classic procedure for growing such a tree from data, described in Tan, Steinbach, and Kumar's widely used textbook Introduction to Data Mining as Hunt's algorithm, is recursive and greedy: at any node t holding a set of training records Dt, if every record in Dt already belongs to the same class, the algorithm stops and turns t into a leaf labeled with that class; otherwise, since Dt "contains records that belong to more than one class," the algorithm selects a single attribute test condition, such as whether income is above some threshold, to split Dt into smaller child subsets, one per outcome of the test, and then applies the exact same procedure recursively to each child. Because this process is greedy, it commits to whichever split looks best at the current node without ever reconsidering that choice in light of splits made later in the tree, and because it is recursive, an entire tree, potentially with many layers of nested questions, emerges from repeatedly applying one simple splitting rule at each level. The central design question this leaves open is how, exactly, the algorithm should decide which attribute test condition, among all the ones available at a given node, is best.

Tan, Steinbach, and Kumar's text answers this by defining node impurity, a measure of how mixed together the classes are within a node's records, and choosing whichever candidate split reduces impurity the most. Three impurity measures are commonly used. Entropy is defined as Entropy(t) = −Σᵢ p(i|t) log₂ p(i|t), summing over classes i, where p(i|t) is the fraction of node t's records belonging to class i; it is zero when a node is perfectly pure and largest when the classes are evenly mixed. The Gini index is defined as Gini(t) = 1 − Σᵢ [p(i|t)]², and classification error is defined as 1 − maxᵢ [p(i|t)], the error rate if the node simply predicted its own majority class; all three share the same basic shape, minimized at purity and maximized at an even class split, but weight intermediate mixtures somewhat differently. Given an impurity measure I, the quality of a candidate split into k child nodes v₁, ..., vₖ is scored by the gain criterion, Δ = I(parent) − Σⱼ [N(vⱼ)/N] I(vⱼ), which is the parent node's impurity minus the weighted average impurity of the resulting children, weighted by how many of the parent's records end up in each child; the algorithm greedily selects, at every node, whichever available attribute test maximizes this gain, meaning it produces the largest possible drop in weighted impurity from parent to children.

Bagging and Random Forests · 14 min

A single decision tree grown all the way down tends to have low bias, since it can carve out an arbitrarily complex decision boundary, but high variance, since a small change in the training data, even removing or adding just a few records, can change which attribute looks best at the very first split and cascade into a completely different tree. Leo Breiman's 1996 paper "Bagging Predictors" proposes a direct way to reduce this variance without touching bias: bootstrap aggregating, or bagging. Bagging generates many alternate training sets, each drawn by sampling from the original training set at random "with replacement," so that some original records appear multiple times in a given bootstrap sample and others do not appear at all, then fits an identical learning procedure, such as a full decision tree, separately on each bootstrap sample. To make a final prediction, Breiman's paper specifies that the predictions of these many trees are aggregated by different rules depending on the type of output: for a numerical target, an obvious procedure is to replace a single tree's prediction by the average of the predictions across all the bootstrap-trained trees, while for a class-label target, where each tree predicts a discrete label, one method of aggregating the individual predictions "is by voting," assigning the class that receives the most votes across all the trees.

Leo Breiman's 2001 paper "Random Forests" extends bagging with one further source of randomness. A random forest is formally defined, in Breiman's own words, as "a classifier consisting of a collection of tree-structured classifiers {h(x,Θk), k=1, ...} where the {Θk} are independent identically distributed random vectors and each tree casts a unit vote for the most popular class at input x"; concretely, each tree is grown on its own bootstrap sample exactly as in bagging, but at every single split within every tree, only a small random subset of the available features is even considered as a candidate for that split, rather than all of them, which forces different trees to rely on different features and decorrelates the resulting ensemble more than bootstrap resampling alone would. Breiman shows mathematically, via a strong-law-of-large-numbers argument over the sequence of random vectors Θ₁, Θ₂, ..., that the generalization error of this voting ensemble converges as more trees are added rather than continuing to change unpredictably, a result he uses to explain why "random forests do not overfit as more trees are added, but produce a limiting value of the generalization error." Because each individual tree in the forest is left unpruned and deep, and therefore has low bias on its own bootstrap sample, while the combination of bootstrap resampling and random feature selection sharply reduces the variance that a single such tree would otherwise have, random forests are able to achieve both properties, low bias and low variance, simultaneously in a way a lone decision tree of comparable depth cannot.

Boosting and Stacking · 14 min

Where bagging and random forests train many independent models in parallel and only combine them at the very end, boosting builds its ensemble sequentially, with each new model specifically targeting the mistakes of the ones trained before it. Yoav Freund and Robert Schapire's 1997 paper "A Decision-Theoretic Generalization of On-Line Learning and an Application to Boosting" introduced AdaBoost, the foundational algorithm in this family. AdaBoost maintains a distribution of weights over the training examples, initially uniform, and on each round trains a new weak hypothesis, a classifier required only to do slightly better than random guessing, using the current weights; after each round, the paper describes an update rule under which the weight distribution "reduces the probability assigned to those examples on which the hypothesis makes a good prediction and increases the probability of the examples on which the prediction is poor," so that every subsequent weak learner is forced to pay more attention to the examples the ensemble so far still gets wrong. After a fixed number of rounds, the final combined classifier is not a simple average of the weak hypotheses but a weighted vote, in which more accurate weak hypotheses are given proportionally larger votes and less accurate ones are given smaller ones; the paper describes this final hypothesis as one that "combines the outputs of the T weak hypotheses using a weighted majority vote," with weights derived directly from each hypothesis's error rate on the weighted training distribution it saw.

AdaBoost's weighted majority vote and a random forest's per-tree vote both combine model outputs by a fixed, predetermined rule, whether uniform or error-weighted, decided in advance of seeing which specific examples any given base model happens to be good or bad at. Stacked generalization, introduced by David Wolpert in 1992 and analyzed further in later work, takes a different approach by learning the combination rule itself. As one paper analyzing the technique summarizes it, stacking first collects the predictions of several already-trained base models into a new dataset, one row per original training example, where "this data set represents every model's prediction of that instance's class, along with its true classification"; in a second stage, "the new data is treated as the data for another learning problem," and a separate meta-model is trained on it, learning, in effect, how much to trust each base model, and under which circumstances, rather than weighting or voting them by a rule fixed in advance. This makes stacking strictly more flexible than either a simple majority vote, of the kind Breiman's random forests use when each tree "casts a unit vote for the most popular class," or AdaBoost's fixed error-derived weights, at the cost of needing enough additional data and computation to train that second-level model without simply overfitting it to the base models' behavior on the training set.

Practice

From One Tree to a Forest

Single tree Low bias, high variance bag + rand. feats Random forest Vote (majority class) Low bias AND low variance

Bagging many trees on bootstrap samples and letting them vote reduces the variance of any single deep tree; random forests add one more twist — restricting each split to a random subset of features — to decorrelate the trees further.

  • Hunt's algorithm grows a decision tree greedily: at every node it picks whichever attribute test maximizes the gain Δ = I(parent) − Σ[N(vⱼ)/N]I(vⱼ), the drop in weighted impurity (entropy, Gini index, or classification error) from parent to children, and recurses until every leaf is pure.
  • Bagging trains identical models on many bootstrap-resampled training sets and aggregates by averaging (regression) or voting (classification); random forests add a second trick — considering only a random subset of features at every split — which decorrelates the trees enough that Breiman proved the ensemble's generalization error converges, rather than overfitting, as more trees are added.
  • Boosting (AdaBoost) and stacking both improve on simple voting, but differently: AdaBoost reweights training examples round by round so each new weak learner focuses on what previous ones got wrong, then combines them by an error-derived weighted vote; stacking instead trains a separate meta-model on the base models' own predictions, learning the combination rule itself rather than fixing it in advance.

Recall Practice

Greedy splittingClick to reveal
How does Hunt's algorithm decide which attribute to split on at a given node?
It computes the gain Δ = I(parent) − Σ[N(vⱼ)/N]I(vⱼ) for every candidate attribute test — the parent node's impurity (entropy, Gini index, or classification error) minus the weighted average impurity of the resulting child nodes — and greedily picks whichever candidate split maximizes that gain.
Bagging's aggregation ruleClick to reveal
In Breiman's bagging, how are predictions from the many bootstrap-trained models combined, and does it differ between regression and classification?
Yes — for a numerical (regression) target, bagging averages the predictions across all the bootstrap-trained models; for a class-label (classification) target, the models vote, and the class receiving the most votes is the ensemble's prediction.
Random forest's extra trickClick to reveal
What does a random forest add on top of ordinary bagging, and why does it help?
At every single split in every tree, only a random subset of the available features is considered as a candidate, rather than all of them. This forces different trees to rely on different features, which decorrelates the ensemble more than bootstrap resampling alone — Breiman showed this lets the generalization error converge rather than overfit as more trees are added.
AdaBoost vs. stackingClick to reveal
How does AdaBoost's way of combining weak learners differ from stacking's way of combining base models?
AdaBoost combines weak learners with a weighted majority vote where the weights are derived directly from each learner's error rate on a reweighted training distribution — a fixed rule decided by the algorithm. Stacking instead trains a separate second-level (meta) model on the base models' own predictions, learning how much to trust each one rather than fixing the combination rule in advance.

Glossary

Impurity (node impurity)
A measure of how mixed the class labels are among the records at a decision-tree node, such as entropy, the Gini index, or classification error; zero at a pure node and largest when classes are evenly mixed.
Gini index
An impurity measure defined as Gini(t) = 1 − Σᵢ[p(i|t)]², where p(i|t) is the fraction of a node's records belonging to class i.
Bagging (bootstrap aggregating)
An ensemble method that trains identical models on many bootstrap-resampled (sampled with replacement) versions of the training set and aggregates their predictions by averaging (regression) or voting (classification).
Random forest
An ensemble of decision trees, each grown on a bootstrap sample with only a random subset of features considered at every split, whose predictions are combined by a majority vote.
Boosting (AdaBoost)
A sequential ensemble method that trains weak learners one after another, reweighting training examples after each round to focus subsequent learners on previously misclassified examples, then combines all weak learners via a weighted majority vote.
Stacking (stacked generalization)
An ensemble method that trains a second-level meta-model on the predictions of several already-trained base models, learning how to combine them rather than fixing a voting or weighting rule in advance.
Practical Activity

Trace a Split, Then Vote Like an Ensemble

A fully paper-based exercise, no software. Part A: learners are given a tiny node of 10 labeled training records (6 of class A, 4 of class B) and asked to hand-compute that node's Gini index (1 − (0.6² + 0.4²) = 0.48) and entropy (−0.6·log₂0.6 − 0.4·log₂0.4), then repeat the calculation for two candidate post-split child-node distributions provided in the worksheet, and use the gain formula Δ = I(parent) − Σ[N(vⱼ)/N]·I(vⱼ) to determine by hand which of the two candidate splits Hunt's algorithm would greedily choose. Part B: learners are given five 'toy weak classifiers' worth of predictions (as a table of hit/miss on 8 labeled points) and asked to simulate one round of AdaBoost's example-reweighting by hand (redistributing weight away from correctly classified points toward misclassified ones, without needing to solve the exact multiplicative update), then separately compute what a plain unweighted majority vote across the same five classifiers would predict on each point, and write one paragraph on where AdaBoost's reweighted vote and the plain majority vote would disagree and why.

Ready to test yourself?

5 questions on this module.

Start Quiz