Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Decision trees and random forests

ProblemClassify by a sequence of yes/no questions about single features, learned from data; average many randomised trees for accuracy
Devised byJames Morgan and John Sonquist (1963), AID; Leo Breiman, Jerome Friedman, Richard Olshen and Charles Stone (1984), CART; Ross Quinlan (1986, 1993), ID3 and C4.5; Tin Kam Ho (1995), random subspaces; Breiman (1996), bagging, and (2001), random forests
AccuracySingle trees are high-variance; forests are among the most accurate off-the-shelf classifiers for tabular data
CostGrowing a tree (a sort per feature per node); a forest of trees times that on features per split
Read firstFoundations: models, data, preprocessing and metrics, The bootstrap
APIDecisionTree (new(max_depth), with_min_samples_split, predict_proba, depth), RandomForest (new(trees, max_depth, seed), oob_accuracy, feature_importances)

Overview

A decision tree partitions the feature space into boxes by repeated splits on one feature at a time (“is ?”) and predicts the majority class in each box. Trees are easy to read, need no feature scaling, and capture interactions, but a deep tree memorises its training data and a shallow one is crude: small changes in the data change the tree completely. A random forest grows hundreds of deep trees on bootstrap resamples of the data, each choosing among a random subset of features at every split, and averages them. The averaging removes most of the variance while keeping the flexibility.

Where it is used

Decision trees give predictions that people can read.

  • Credit, insurance and medicine. Rules such as “if income is below X and the account is under two years old” are easy to audit and explain.
  • Engineering diagnosis. Classifying a machine’s condition from vibration and temperature features.
  • Random forests. Averaging many randomised trees gives a strong all-round classifier and regressor for tabular data, with a built-in estimate of which features matter (used in remote sensing and bioinformatics).

History

James Morgan and John Sonquist (1963) introduced tree-structured “automatic interaction detection” for survey data. Leo Breiman, Jerome Friedman, Richard Olshen and Charles Stone’s book Classification and Regression Trees (1984) set out CART: binary splits chosen by impurity, then pruning. Ross Quinlan’s ID3 (1986) and C4.5 (1993) did the same with information gain in machine learning. Breiman’s bagging (1996) averaged trees grown on bootstrap samples; Tin Kam Ho (1995) and Yali Amit and Donald Geman (1997) randomised the features; Breiman (2001) combined the two as random forests, with out-of-bag error estimates and variable importance. Forests and gradient-boosted trees remain the strongest general methods for tabular data.

The idea

Growing a tree by Gini impurity

A decision tree classifies a point by asking a sequence of yes-or-no questions of the form “is feature below the threshold ?”. Each answer leads down a branch to another question, and finally to a leaf, which holds the prediction. Geometrically, each question cuts a region of feature space in two with a cut parallel to an axis, so the tree divides the space into boxes.

Measuring purity. A good question separates the classes. To compare candidate questions we need a measure of how mixed a set of points is. For a node whose points have class proportions , the Gini impurity is

It is the probability that two points drawn at random from the node have different labels. A pure node, with all its points in one class, has . A node split evenly between two classes has , the largest possible for two classes.

Choosing a split. At a node, every feature and every threshold is a candidate. For each feature, sort the node’s points by that feature; only thresholds between consecutive distinct values need be considered, since any other threshold divides the points in the same way as one of those. Each candidate sends some points to a left child and the rest to a right child. Its quality is the average impurity of the two children, weighted by their sizes, and the split chosen is the one that reduces the impurity most.

Growing. The procedure is applied recursively to each child, until a node is pure, has too few points to split, or has reached the depth limit. It then becomes a leaf, which stores the class proportions of its training points; these serve as predicted probabilities.

The choice at each node is greedy: it is the best single split, without looking ahead to what later splits could achieve. Finding the truly optimal tree is computationally intractable, and the greedy tree is good enough in practice.

A tree of depth 2 draws three axis-parallel cuts; a fully grown tree carves out a box for every stray point, including thin slivers; a forest of 200 trees averages them into a boundary that follows the moons.

Bagging and random features

A fully grown tree fits its training data perfectly and is very sensitive to it: a slightly different training set can produce a quite different tree. In the language of the bias-variance decomposition it has low bias and high variance. Averaging is the classical cure for variance, and a random forest is an average of many trees.

How much does averaging help? If predictors each have variance and every pair has correlation , their average has variance

The second term shrinks as trees are added, but the first does not. If the trees were independent () the variance would fall like ; if they were identical () averaging would achieve nothing. So the trees must be made to differ, while each remains a reasonable classifier. Two sources of randomness do this.

  • Bagging (bootstrap aggregating): each tree is trained on a bootstrap sample, points drawn with replacement from the training set, so each tree sees a somewhat different data set.
  • Random features: at each split only a random subset of features (by default ) may be considered. Without this, a single strong feature would be chosen at the top of every tree and the trees would all resemble one another. With it, other features get their turn, and the trees are less correlated.

Out-of-bag error and feature importance

Bagging provides a test set for free. Each bootstrap sample omits about 37% of the training points (see the bootstrap), so for every training point there are trees that never saw it. Predicting each point using only those trees, and comparing with its true label, gives the out-of-bag accuracy: an honest estimate of the accuracy on new data, with no data held back and no extra training.

Figure 11.8. Accuracy against the number of trees.

The forest beats the single deep tree by eight points of accuracy with a few dozen trees, and more trees never hurt. The out-of-bag estimate (available once every point has been left out at least once) runs a little below the test accuracy here; with 300 training points its standard error is about 0.02.

The library’s feature importance is the share of all splits that use each feature. On four informative and six pure-noise features:

Table 11.6. Feature importances of a random forest.

feature0123456789
share of splits0.1600.1430.1240.1060.0820.0760.0740.0730.0880.072

Features 0–3 carry the class information; 4–9 are pure noise.

The informative features rank first, but the noise features still receive nearly half the splits: deep trees keep splitting on noise once the signal is exhausted. Impurity-weighted or permutation importances separate them more clearly.

Algorithm

Growing a tree on a set of training points. grow(node):

  1. Stop? If all the node’s points have the same class, or there are too few to split, or the depth limit has been reached, make the node a leaf holding its class proportions.
  2. Search for the best split. For each candidate feature:
    • sort the node’s points by that feature;
    • scan through them in order, moving one point at a time from the right side to the left and updating the class counts of the two sides;
    • wherever two consecutive values differ, the midpoint between them is a candidate threshold: compute the weighted Gini impurity of the two sides from the counts, and remember the best.
  3. Split. If no split reduces the impurity, make a leaf. Otherwise divide the points at the best feature and threshold, and call grow on each side.

Keeping running counts makes the scan of one feature after its sort, so a node with points and candidate features costs .

Growing a forest of trees. For each tree:

  1. draw a bootstrap sample of points with replacement;
  2. grow a tree on it as above, except that step 2 considers only features, chosen at random afresh at every node;
  3. for each training point that is not in the bootstrap sample, add the tree’s prediction to that point’s out-of-bag votes.

Predicting with a forest: pass the point down every tree to a leaf, average the class proportions of those leaves, and return the class with the largest average.

Implementation

        let mut best: Option<(f64, usize, f64)> = None; // (impurity decrease, feature, threshold)
        for &f in &candidates {
            let mut order: Vec<usize> = idx.to_vec();
            order.sort_by(|&a, &b| x[[a, f]].total_cmp(&x[[b, f]]));
            let mut left = vec![0.0; self.classes];
            for k in 0..order.len() - 1 {
                left[labels[order[k]]] += 1.0;
                let (a, b) = (x[[order[k], f]], x[[order[k + 1], f]]);
                if a == b {
                    continue;
                }
                let nl = (k + 1) as f64;
                let right: Vec<f64> = counts.iter().zip(&left).map(|(c, l)| c - l).collect();
                let weighted = (nl * gini(&left, nl) + (n - nl) * gini(&right, n - nl)) / n;
                let gain = parent - weighted;
                if best.is_none_or(|(g, _, _)| gain > g + 1e-15) {
                    best = Some((gain, f, 0.5 * (a + b)));
                }
            }
        }
        match best {
            // Zero-gain splits are allowed: structure such as XOR has no single split that improves
            // purity, yet splitting twice separates it. Depth and node-size limits ensure termination.
            Some((gain, feature, threshold)) if gain > -1e-12 => {
                let (l, r): (Vec<usize>, Vec<usize>) = idx.iter().partition(|&&i| x[[i, feature]] <= threshold);
                Node::Split { feature, threshold, left: Box::new(self.grow(x, labels, &l, depth + 1, rng)), right: Box::new(self.grow(x, labels, &r, depth + 1, rng)) }
            }
            _ => leaf(&counts),
        }
        for t in 0..self.trees {
            let sample: Vec<usize> = (0..n).map(|_| rng.below(n)).collect();
            let mut in_bag = vec![false; n];
            sample.iter().for_each(|&i| in_bag[i] = true);
            let mut tree = DecisionTree::new(self.max_depth).with_max_features(m, self.seed.wrapping_add(1 + t as u64));
            tree.fit_subset(x, labels, &sample, classes);
            for i in (0..n).filter(|&i| !in_bag[i]) {
                let d = tree.distribution(&x.row(i).to_vec());
                oob_votes[i].iter_mut().zip(d).for_each(|(v, p)| *v += p);
                oob_seen[i] = true;
            }
            self.forest.push(tree);
        }
  • Splits with zero impurity decrease are allowed, so that problems such as XOR, where no single split helps, can still be separated; the depth and node-size limits stop the growth.
  • There is no pruning: limit the depth or use a forest.

In practice

//! A single tree against a random forest, with out-of-bag accuracy.

use lib_jsl_machine_learning::common::Classifier;
use lib_jsl_machine_learning::datasets::make_classification;
use lib_jsl_machine_learning::metrics::accuracy;
use lib_jsl_machine_learning::supervised::classification::{DecisionTree, RandomForest};

fn main() {
    let (x, labels) = make_classification(100, 1);
    let (x_test, test_labels) = make_classification(1000, 2);
    for depth in [1, 2, 4, 8, 50] {
        let mut tree = DecisionTree::new(depth);
        tree.fit(x.view(), &labels).unwrap();
        println!(
            "tree, depth limit {depth:>2} (grown to {}): training {:.3}, test {:.3}",
            tree.depth(),
            accuracy(&labels, &tree.predict(x.view()).unwrap()).unwrap(),
            accuracy(&test_labels, &tree.predict(x_test.view()).unwrap()).unwrap()
        );
    }
    let mut forest = RandomForest::new(200, 50, 1);
    forest.fit(x.view(), &labels).unwrap();
    println!(
        "forest of 200 trees: test {:.3}, out-of-bag estimate {:.3}",
        accuracy(&test_labels, &forest.predict(x_test.view()).unwrap()).unwrap(),
        forest.oob_accuracy().unwrap()
    );
    println!("share of splits per feature: {:.3?}", forest.feature_importances());
}
tree, depth limit  1 (grown to 1): training 0.607, test 0.614
tree, depth limit  2 (grown to 2): training 0.857, test 0.822
tree, depth limit  4 (grown to 4): training 0.913, test 0.846
tree, depth limit  8 (grown to 8): training 0.997, test 0.827
tree, depth limit 50 (grown to 10): training 1.000, test 0.827
forest of 200 trees: test 0.893, out-of-bag estimate 0.900
share of splits per feature: [0.299, 0.251, 0.246, 0.204]

Strengths

  • Trees: interpretable, no scaling, handle interactions and mixed feature scales.
  • Forests: accurate with little tuning, robust to irrelevant features, with a free out-of-bag estimate.

Weaknesses

  • Single trees are unstable and overfit; boundaries are axis-parallel staircases.
  • Forests lose interpretability and are slower to train and predict; split-count importances are biased.
  • Classification only; no regression trees in this module (see gradient boosting in the ensembles module).

Exercises

  1. Show that the Gini impurity is the expected misclassification rate of a rule that labels randomly with the node’s proportions.
  2. Compute the variance of the average of equicorrelated predictors, and its limit as .
  3. Show that the probability that a given point is left out of a bootstrap sample of size tends to .
  4. Construct the XOR problem and show that no single axis-parallel split reduces the Gini impurity, but two levels separate it.
  5. Implement permutation importance (the drop in out-of-bag accuracy when a feature’s values are shuffled) and compare with split counts.
Hints and answers
  1. A rule that labels at random with the node’s proportions is wrong with probability , which is the Gini impurity.
  2. For predictors of variance and pairwise correlation , . As it tends to : averaging removes only the part of the variance that is not shared, which is why forests decorrelate the trees.
  3. Each of the draws misses a given point with probability , so the probability that it is never drawn is .
  4. Take points of one class and of the other. Any split on one coordinate puts one point of each class on each side, leaving the impurity at . After splitting on , each child can be split perfectly on .
  5. Compare the accuracy on out-of-bag samples before and after shuffling one feature’s column; a large drop means the forest relies on that feature. Unlike split counts, it is not biased toward features with many possible split points.

Further reading

  • L. Breiman, J. H. Friedman, R. A. Olshen and C. J. Stone, Classification and Regression Trees, Wadsworth, 1984.
  • L. Breiman, “Random forests”, Machine Learning 45 (2001) 5–32.
  • L. Breiman, “Bagging predictors”, Machine Learning 24 (1996) 123–140.
  • G. Louppe, Understanding Random Forests: From Theory to Practice, PhD thesis, University of Liège, 2014.