← Back to the track

Learn machine learning · deep dive D2 · optional

Cost-complexity pruning

Put a price on a leaf and a tree prunes itself. Work out the price at which each subtree stops paying for itself, then read the staircase that falls out of it.

Deep dive D2DerivationAbout 40 minutesThe gate is a behaviour you produce

Optional, and it blocks nothing. It opens because 2.4 Pruning and instability is done. The gate is three prices within 0.0005 plus the correct minimum marked in all four columns.

01 · brief

Session 2.3 limited a tree by depth and 2.4 watched what a small change in the rows did to its shape. Depth is a blunt control: it caps every branch at the same length whether or not that branch was earning anything.

The alternative charges for size instead. Score a tree as R-alpha(T) = R(T) + alpha x leaves(T), where R is the training error rate and alpha is a price per leaf. At alpha zero the largest tree wins. Raise the price and at some point a subtree's extra leaves cost more than the error they save, so it collapses.

The question you cannot answer yet: how many different trees does that price actually produce as it rises from zero, and who picks which one.

02 · commit

Before anything runs

Before any arithmetic: as the price per leaf rises from zero to a large number, how many distinct trees does this sequence of four produce?

Before any arithmetic: as the price per leaf rises from zero to a large number, how many distinct trees does this sequence of four produce?

Choose something first.

03 · work

For two nested subtrees the price is the extra training error divided by the leaves given up: alpha = (R of the smaller - R of the larger) / (leaves of the larger - leaves of the smaller). Collapsing all the way to a single leaf is the textbook case, where the denominator is leaves - 1.

To four decimals or better, within 0.0005.

Attempts: 0

To four decimals or better, within 0.0005.

Attempts: 0

To four decimals or better, within 0.0005.

Attempts: 0

0 of 3 prices accepted.

The four subtrees

illustrativeauthored fixture
Leaves, training error and validation accuracy for the four nested subtrees.
subtreeleavestraining errorvalidation accuracy
T080.1500.771
T150.1660.796
T230.1920.788
T310.3000.702

These four are a fixture, chosen so every number on this page can be checked with a pencil. They are not a fit on the loan rows.

Mark the smallest R-alpha in each column

R-alpha(T) = R(T) + alpha x leaves(T). One radio group per column: choose the row with the smallest value.

R-alpha for each subtree at four prices per leaf, with a radio button per cell for marking the minimum.
subtree alpha 0.004 alpha 0.010 alpha 0.020 alpha 0.060
T0
T1
T2
T3

Mark one row in every column first.

Four columns of R-alpha, one per price per leaf, and four rows, one per subtree. Each column holds the four trees scored at that price; one of the four is the smallest.

Show the grid as a table
Values shown in the figure above
subtreeleaveserroralpha 0.004alpha 0.01alpha 0.02alpha 0.06
T080.1500.18200.23000.31000.6300
T150.1660.18600.21600.26600.4660
T230.1920.20400.22200.25200.3720
T310.3000.30400.31000.32000.3600

The alpha path prints here once the three prices above are accepted. They are the three numbers it is made of, so it stays shut until you have worked them out.

Which tree a given price keeps

Ask for a hint

Rungs open as you run the session, not as time passes. You have run it 0 times.

1. A question: available after 3 evaluated runs.

2. The answer: available after 5 evaluated runs.

04 · verdict

Work the three prices above and the verdict states the path against what you committed to.

05 · gate

Your check: an outcome you produce

not yet

The three prices within 0.0005, and the smallest R-alpha marked correctly in all four columns of the grid.

Not met yet. Keep working the session.

The validation block scores highest on T1 at 0.796, and the training error alone would have kept the largest tree. Which prices keep T1 is the arithmetic above.

carry

Your takeaway

Edit it into your own words, or keep the line as it is.

back to the track

2.4 Pruning and instability

A deep dive blocks nothing. Open the notebook