learn-ensemble-methods-with-phoebe / Session 2 of 6
Learn Ensemble Methods with Phoebe · Session 2 of 6

Random forests

Session 1 ended on a wound: one deep tree at 75 percent on the customers it studied and 58 on the customers it never met. Tonight the first cure, and it is almost embarrassingly simple - train sixty different trees and let them vote. The craft is in the word different: bootstrap samples so each tree sees its own world, random feature subsets so the trees cannot all copy each other, and a free validation trick hiding inside the leftovers. All of it from Breiman's own two papers, quoted, then built on paper with your own hands.

🟢 Easy Builders - you hand-built a split in session 1 45 min
0-3 · Welcome 3-22 · The Breiman recipe 22-40 · Build-along: build the forest's logic by hand 40-45 · Q&A
Part 0

Where session 1 left you

You hand-built a split on the Mango Lane data, then watched a depth-6 tree score 75.0 on training and 58.0 on holdout - a 17-point gap made of memorized noise. Tonight's promise is specific and measured: the same trees, bagged and decorrelated, land at 68.0 holdout - averaging is worth ten points on this data. Not a slide claim; session 3's live engine re-measures it in your browser.

🎫 Clear all three quiz questions to stamp your passport Live = presented in the 45 minutes Self-study = the full depth, after class

★ What you walk out with tonight. Breiman's instability argument, quoted, so you know why bagging works and when it will not. The one idea that makes a forest more than bagged trees - random feature subsets - with its honest credit lineage. And out-of-bag error, the free lunch: a validation score you did not have to pay a test set for.

LiveThe recipe in one breath2 min▶

A random forest is four decisions, and you already understand three of them from session 1:

  • Bootstrap sample per tree - each tree trains on 200 customers drawn WITH replacement from the 200. Different tree, different world.
  • Random feature subset per split - at every node, a tree may only consider a random handful of the six features. This is the part that is actually new, and the part that earns the name.
  • Deep trees, on purpose - the instability that ruined the single tree is the fuel here. Do not prune the very thing you are about to average away.
  • Vote - sixty churn calls per customer, majority wins.

Everything in the next forty minutes is one of these four decisions examined properly.

Self-studySay it before you build it2 min▶

Before class, try saying the recipe out loud without the page. The order matters: bootstrap gives each tree its own sample, feature subsetting gives each tree its own perspective, depth keeps each tree opinionated, and the vote turns sixty opinionated witnesses into one calm verdict. If you can say why each step exists, session 3's boosting - which deletes the vote and replaces it with a chain - will land in minutes.

Part 1 · covers bagging, from the instability insight

Bagging: many worlds, one vote

7 min live

Bagging is 1996 Breiman, and his own sentence still explains it better than any textbook paraphrase: "The vital element is the instability of the prediction method. If perturbing the learning set can cause significant changes in the predictor constructed, then bagging can improve accuracy." Read that twice - it names both the disease and the entry requirement for the cure.

BOOTSTRAP: DRAW 200 CUSTOMERS WITH REPLACEMENT, ONCE PER TREE · EACH TREE SEES A DIFFERENT WORLD Training set 200 customers one fixed world Bootstrap world A cust 12 drawn twice, cust 41 never drawn Bootstrap world B cust 41 drawn 3 times, cust 12 never drawn Bootstrap world C its own duplicates, its own missing rows Deep tree A its own splits Deep tree B different splits Deep tree C different again Majority vote one verdict Perturb the sample, get a different tree - Breiman's instability, now working FOR you: private mistakes disagree and cancel. Duplicates and missing customers are the point, not a bug. Roughly one-third of rows miss each world - part 3 cashes that in.
🔍 Click to zoom - one training set, many bootstrap worlds, one vote
LiveThe mechanism: sampling with replacement, deep on purpose4 min▶

To bootstrap the Mango Lane training set, draw a customer at random from the 200, write it down, put it back, and repeat 200 times. Some customers appear twice or three times; others never appear at all. Do this sixty times and you have sixty overlapping-but-different training sets - sixty perturbed worlds.

Now recall session 1: a deep tree is spectacularly sensitive to its sample - move ten customers and the upper tree reshapes. Bagging turns that sensitivity into diversity. Sixty worlds produce sixty genuinely different trees, and that is why the recipe says deep trees, unpruned: instability is the fuel. Tame the trees and you starve the average.

Real world

A team at a retailer once "improved" their forest by heavily pruning every tree to make each one individually respectable. The forest got worse. Sixty polite, stable, nearly identical trees have nothing to argue about - and the argument was the value.

LiveWhy the variance cancels - and why bagging logistic regression does nothing3 min▶

Each deep tree's error has two parts: the signal it caught, and private mistakes tied to its particular bootstrap sample - which duplicates it saw, which noise-labeled customers it happened to memorize. The signal part is shared across trees; the private part is independent-ish, pointing in different directions tree by tree. Average sixty of them and the shared signal survives while the disagreements cancel toward zero. That is variance reduction, in one sentence.

The same logic says when bagging is useless. Bag sixty logistic regressions and each fit lands on nearly the same coefficients - it is a stable method, exactly the case Breiman's sentence excludes. Sixty copies of the SAME mistake do not cancel; averaging identical errors changes nothing. The cure only works on the disease.

Self-studyThe measured payoff on the course data2 min▶
ModelTrainHoldoutRead
Single tree, depth 675.058.0memorized fragments, generalizes worst
Bagged forest, 60 trees d6, feature subsample88.568.0variance averaged away: +10 holdout

Notice the train number went UP too, 75 to 88.5 - the forest is not weaker on the training data, it is stronger everywhere, because sixty perspectives recover signal any single perturbed tree missed. And the train-holdout gap shrank from 17 points to about 20 on a much higher base - the noise-chasing got diluted. Session 3's engine fits this exact forest live so you can watch both numbers move.

Part 2 · covers the actual random forest contribution

The random in random forest is not the bootstrap

6 min live

Here is the part most blog posts flatten: bagging trees does NOT make a random forest. Breiman's 2001 definition: "Random forests are a combination of tree predictors such that each tree depends on the values of a random vector sampled independently..." - and the random vector he means governs a second randomization: at every split, the tree may only consider a random subset of the features.

SAME BOOTSTRAP WORLDS, TWO FORESTS · THE ONLY DIFFERENCE IS THE RANDOM FEATURE SUBSET PER SPLIT Bagging only: every feature, every split daysSinceOrder daysSinceOrder ...same, x60 The loudest feature wins every root. 60 near-copies of one tree. Correlated votes: the same mistake, repeated 60 times, does NOT cancel. Random forest: random subset per split daysSinceOrder tickets sessions30d Some roots never even SEE the loud feature. Different roots, different trees. Decorrelated votes: private mistakes disagree, and disagreements cancel. Breiman credits Ho 1998 (random subspaces) and Amit & Geman 1997 as precursors - bagging of trees alone is NOT a random forest. Randomness here is not sloppiness. It is engineered disagreement, and disagreement is what makes a vote worth counting.
🔍 Click to zoom - sixty clones versus sixty independent witnesses
LiveWhy subsetting decorrelates, on Mango Lane's own columns4 min▶

On this data, daysSinceOrder is the loudest feature - you found that yourself in session 1's hand-split. Left unconstrained, every one of the sixty trees grabs daysSinceOrder at the root, because it wins the split search in nearly every bootstrap world. Sixty trees, one skeleton: the forest degenerates into sixty near-copies, their votes redundant, and the averaging math from part 1 quietly dies - correlated errors do not cancel.

Force each split to choose from a random handful of features and some trees must open with tickets, or sessions30d, or discountShare. Those trees carve the customers along genuinely different boundaries, make genuinely different private mistakes - and the vote becomes what it pretends to be: independent witnesses, not one witness with sixty microphones.

Breiman measured what this buys: random feature selection "yields error rates that compare favorably to Adaboost... but are more robust with respect to noise." Hold that phrase - our lab data has 10 percent label noise by construction, and robustness to noise is exactly why the forest handles it with grace.

Self-studyCredit where it is due2 min▶

The lineage matters, and Breiman himself is careful about it: his 2001 paper credits Ho 1998 (the random subspace method) and Amit & Geman 1997 as precursors of the random-feature idea. So when someone tells you "random forest is just bagged trees", you now hold two corrections: it is bagging PLUS per-node feature subsetting, and the subsetting idea has its own family tree. Precision about credit is the same muscle as precision about scope - session 1 taught you the second, this is the first.

Part 3 · covers out-of-bag error

Out-of-bag error: the free lunch

5 min live

Look back at the bootstrap picture: each world is missing customers. Breiman noticed the leftovers are not waste - "about one-third of the instances are left out" of each bootstrap sample, which means every customer has roughly twenty trees (of sixty) that never saw it. Score each customer using only those trees and you get a validation estimate without spending a single row on a test set.

CUSTOMER 17'S OOB SCORE: ONLY TREES WHOSE BOOTSTRAP NEVER DREW CUSTOMER 17 MAY VOTE Customer 17 62 days inactive did she churn? Tree A · in-bag drew cust 17 twice Tree B · out-of-bag never saw cust 17 Tree C · in-bag drew cust 17 once vote ignored - it studied her, so its answer is memory vote ignored - same reason OOB vote for cust 17 only tree B counts: a real test on an unseen customer Across 60 trees, roughly 20 are out-of-bag for any customer. Average these honest votes over all 200 rows: the OOB error.
🔍 Click to zoom - the leftovers of every bootstrap become a built-in test set
LiveWhat Breiman claimed for it, and the caveat that keeps it honest3 min▶

Breiman's claim is bold and verbatim: "using the out-of-bag error estimate removes the need for a set aside test set", and he reports the OOB estimates are "unbiased". For every customer, some trees never saw her - their votes are honest tests, not recall. Aggregate those honest votes across all 200 training customers and you have a generalization estimate that cost you nothing.

The caveat you must carry: OOB overestimates error early in training. With only a handful of trees grown, each customer's OOB committee is tiny - two or three trees instead of twenty - and a tiny committee votes badly. The unbiasedness arrives once enough trees are grown, not at tree five. Watch a forest's OOB curve and you will see it start pessimistic and settle as the committee fills out.

One-third, from first principles. Each draw misses a given customer with probability 199/200. After 200 draws with replacement, the chance she was never drawn is (199/200)^200, about 0.37 - Breiman's "about one-third of the instances are left out" is that number, not a design choice.

Self-studyWhen you still want a true holdout anyway3 min▶

OOB is forest-internal - it validates a random forest against resamples of its own training data. Two situations where that is not enough:

  • Time-based splits. Churn is a future-prediction problem: in production you train on last quarter and predict next quarter. Bootstrap resampling shuffles time away, so OOB cannot tell you whether the model survives the drift between quarters. A holdout cut by date can.
  • Comparing across model families. Next session's booster has no bootstrap and therefore no OOB. To put a forest, a booster and a logistic regression on one honest leaderboard, they must all face the SAME unseen customers - which is exactly why this course keeps its 100-customer holdout even though tonight's model comes with free validation built in.

So the working rule: OOB to monitor and tune the forest, the true holdout to judge it against everything else.

Build-along

Build the forest's logic by hand

★ 18 min · everyone builds

Sixty trees is a job for code, but every mechanism in the recipe - bootstrap, forced feature subsets, the vote, out-of-bag - fits on paper at ten customers and two trees. Get your session 1 10-customer excerpt and a die.

Bootstrap your excerpt. Number your 10 customers 0-9. Roll a die (or use a phone's random digit) 10 times, writing down the customer each roll picks - with replacement, so repeats stay. Look at your list: some customers appear twice, some never. You are holding bootstrap world A, and the missing customers are its out-of-bag set. Keep the list.

Grow paper tree A under a feature handcuff. Rule: this tree may only split on {tickets, aov} - a forced random subset. Find your best split by counting, exactly like session 1. Notice what just happened: your best feature, daysSinceOrder, was not on the menu, so this tree carved the customers along a boundary your session 1 tree never considered. That difference IS the decorrelation from part 2.

Grow paper tree B on a second bootstrap with the subset {daysSinceOrder, sessions30d}. Roll a fresh bootstrap first. This tree probably roots on daysSinceOrder like your session 1 tree - but on a different sample, so its threshold lands somewhere new.

Hold the vote. Pick three customers from the excerpt and run each through paper tree A, paper tree B, and your session 1 tree: three churn calls per customer, majority wins. Find a customer where the vote overrules one wrong tree - that single overrule, times sixty trees and two hundred customers, is the entire ten-point payoff.

Compute a tiny OOB set. For each paper tree, list the customers its bootstrap never drew. Those customers can be scored by that tree as honest unseen tests - you just computed an out-of-bag set by hand. If a customer is out-of-bag for both trees, both may vote on her; that committee-of-honest-votes is Breiman's free lunch at miniature scale.

Bring this intuition to session 3. Voting averages away mistakes, but it cannot add signal no tree ever found - if all sixty trees miss a pattern, the vote misses it too. Session 3 replaces the vote with a chain: each new tree is trained on exactly what the previous ones got wrong. Averaging fixes variance; chaining goes after what averaging cannot touch.

Homework

Before session 3

30 min
  1. Finish the paper forest: add a third bootstrap and a third feature handcuff ({discountShare, orders90d}), grow paper tree C, and re-run the three-customer vote with four voters. Did any verdict flip?
  2. Say the recipe from memory - bootstrap per tree, random subset per split, deep trees, vote - and for each step, one sentence on what breaks without it. The subsetting sentence is the one most working data scientists cannot produce.
  3. Explain to a colleague why bagging their logistic regression will not help, using Breiman's instability sentence. If they ask "so instability is good now?", you have the conversation this session prepared you for.
  4. Read Breiman 2001, section 1 only (stat.berkeley.edu/~breiman/randomforest2001.pdf) - watch him credit Ho and Amit & Geman by name, and find the definition sentence this session quoted.
  5. Optional: work out (199/200)^200 on any calculator and confirm the one-third for yourself. Small proofs you own beat big claims you rent.
Check yourself

Three questions

Clear all three and session 2 gets stamped in your passport.

1 · Bagging sixty deep trees buys ten holdout points on the course data, but bagging sixty logistic regressions barely moves the needle. Why?

Breiman 1996, verbatim: "The vital element is the instability of the prediction method. If perturbing the learning set can cause significant changes in the predictor constructed, then bagging can improve accuracy." Instability is the entry requirement - bagging helps unstable learners like deep trees and does little for stable ones. The flaw that ruined session 1's single tree is the fuel here.

2 · Your teammate says "a random forest is just bagged decision trees". What is missing?

RF = bagging PLUS per-node random feature subsetting - "each tree depends on the values of a random vector sampled independently", in Breiman's definition. Without it, all sixty Mango Lane trees root on daysSinceOrder and become near-copies whose correlated errors do not cancel. And credit travels with the idea: Breiman himself cites Ho 1998 and Amit & Geman 1997 as precursors of the random-feature trick.

3 · How does out-of-bag error work, and when should you distrust the number?

Each bootstrap leaves out "about one-third of the instances", so every customer has trees that never saw her - their votes are honest tests. Breiman: "using the out-of-bag error estimate removes the need for a set aside test set", and the estimates are "unbiased" - but only once enough trees exist; early on, tiny committees make OOB pessimistic. And OOB stays forest-internal: for time-based splits or cross-family comparisons you still want the true holdout.

Sources covered

What this session teaches, and where it came from

✓Breiman, Random Forests, Machine Learning 2001The definition, the random-feature contribution with its Ho / Amit & Geman lineage, and OOB error with the early-training caveat
✓Breiman, Bagging Predictors, Machine Learning 1996The instability sentence in full, and its converse: bagging does little for stable learners

Every fact on this page, and its verification tier, is recorded in the course's source map.

Session 2 cheat sheet · pin this

The recipe in one breathBootstrap sample per tree, random feature subset per split, deep trees on purpose, majority vote.
Instability is the fuelBagging improves a predictor only if perturbing the sample changes it - Breiman 1996. Deep trees qualify; logistic regression does not.
RF is not just bagged treesThe contribution is per-node feature subsetting - with lineage: Breiman credits Ho 1998 and Amit & Geman 1997.
Subsetting = engineered disagreementWithout it every tree roots on the loudest feature and 60 correlated votes cancel nothing.
OOB, with its caveatEach bootstrap misses about a third of rows; score customers by trees that never saw them. Unbiased once enough trees are grown - pessimistic early.
The measured payoffSingle tree 75.0 / 58.0 - forest 88.5 / 68.0. Averaging is worth ten holdout points on this data, and session 3 re-measures it live.