Machine learningLoading progress…

Partitions and leaves

Choose a split by impurity reduction and distinguish bagging, random forests and boosting.

A tree is a list of yes/no questions

Imagine sorting 60 emails into spam and not spam using only questions of the form "is this number above that line?" Each question sends an example down one of two doors. Ask a few of them in sequence and you have a decision tree. Every example ends up in exactly one leaf, and the leaf holds the answer.

Geometrically each question is an axis-aligned cut: "x ≤ 4.75" draws a vertical line through the feature space, "y ≤ 3.65" a horizontal one. A tree therefore chops the plane into rectangles (a partition), and the tree and the rectangles are two views of one object. A leaf predicts from the training examples that landed in it: the most common class for classification, the mean target for regression. A tree with no cuts at all is a single leaf, which predicts one constant for every input.

feature xfeature y
Click cuts along:
blue30 | 30
1 leavesdepth 0train accuracy 50%test accuracy 62.5% (400 held-out points)

Each leaf box shows its predicted class and the training counts as blue | orange. The tree is the picture on the left written as questions.

The 60 training points (blue and orange, 30 each). Click inside the plot to cut the region you click in. Watch the tree on the right and the accuracy numbers change.

Where the predictions come from

With one leaf the best constant guess is the majority class, and since the classes are 30 and 30 that is a coin flip: 50% training accuracy. One cut on x already gets 73.3%. A second round of cuts (4 leaves) reaches 90% on the training points and 83% on 400 fresh points the tree never saw. Those fresh points are the honest number.

Predict first. You keep cutting until every leaf holds only one class. What happens to the training accuracy, and to the accuracy on fresh points?

The common mistake: reading a pure leaf as proof

A leaf that holds one class looks like certainty, but a leaf with a single example is just that example repeated. Training accuracy only measures how well the tree remembers; it cannot tell remembering from understanding. Depth is the knob that controls how fine the rectangles get, so a tree's depth (or minimum leaf size) is something you choose by looking at held-out data.

Chapter context

A decision tree recursively asks questions that partition data. An ensemble combines many models to improve behavior, but the way those models are trained determines what kind of errors the combination can reduce.

Chapter reference: math, worked example & code

This section belongs to the whole chapter, so it is the same on every lesson in it and may cover a different lesson's topic. The code for the experiment you just ran is inside the experiment itself.

Math and notation

G(p)=1−∑kpk2,ΔG=Gparent−nLnGL−nRnGR.G(p)=1-\sum_kp_k^2,\quad \Delta G=G_{parent}-\frac{n_L}{n}G_L-\frac{n_R}{n}G_R.

A boosted additive model has the form Ft+1(x)=Ft(x)+ηht(x)F_{t+1}(x)=F_t(x)+\eta h_t(x). The stage target depends on the loss and boosting method.

Worked example

A parent contains three red examples and one blue example. Its Gini is 1−0.75²−0.25²=0.375. Split into a left child with two red examples and a right child with one red and one blue. Weighted child Gini is 2/4×0 + 2/4×0.5 = 0.25, giving gain 0.125.

Reference code

The following example uses the Python standard library and does not call a remote service.

from math import isclose
gini = lambda counts: 1-sum((n/sum(counts))**2 for n in counts)
parent = gini([3,1])
children = .5*gini([2,0]) + .5*gini([1,1])
assert isclose(parent, .375)
assert isclose(children, .25)
assert isclose(parent-children, .125)

Python examples are provided for study and local execution; this page does not execute Python.

Common mistakes

Feature importance is not causal influence. A pure training leaf need not generalize. Bagging and boosting are different training strategies, not interchangeable names for averaging.