Random Forest

Random Forest

Let many small trees vote for a steadier answer.

Drag the new seed, or tap anywhere on the graph. Every tree below votes, and the forest answers with the most votes.

  • sprouts
  • stays dry
  • new seed: drag it, or tap the graph
  • Thow many trees vote
123456789

y^=the most common answer of h1(q),…,hT(q)\hat{y} = \text{the most common answer of } h_1(q), \dots, h_{\htmlClass{sym-line}{T}}(q)

T = 9 trees: 4 dry, 5 sprout → sprouts (56% of the votes)

Tree 1 drew 30 seeds at random, with repeats: it saw 21 different seeds and never saw 9. It says sprout.

Right on unseen seeds 19 of 30
63% right

The 9 trees: what each one learned from, and its vote for the new seed.
12193sprout
218124sprout
319114dry
415154dry
519112dry
618124sprout
719113sprout
819114dry
919112sprout

Try it in Python

The same 30 seeds, trees and new seed as the Lab, in Python. Plain Python uses the Lab's own random numbers, so it grows the Lab's exact trees; scikit-learn makes its own random choices.

What you are looking at

Each shape is a seed in a tray. Across the graph is how warm its soil was; up the graph is how much water it got each day. Circles sprouted and triangles stayed dry. Seeds sprout when warmth and water together are enough, so the border between them runs on a slant.

The diamond is a new seed. Its colour is the forest's answer, and the shading shows the answer everywhere else: stronger where more trees agree. Below the graph, every tree is drawn as a small map. The numbers are made up, and Refresh Data shows another tray of 30 seeds.

What each symbol means

q
The new seed, the one we want an answer for.
hₜ(q)
The answer of tree number t.
T
How many trees vote. It is always odd, so the vote never ties.
n
How many seeds there are to learn from.
ŷ
The forest's answer: sprouts or stays dry.

How a forest is grown

A random forest is many small decision trees that vote. If every tree learned from the same seeds in the same way, they would all be the same tree, and the vote would add nothing. So each tree gets two kinds of chance:

  1. Its own sample. Each tree draws n seeds at random, putting each one back after drawing it. Some seeds come twice or more, and some not at all. Pick a tree to see its sample on the big graph.
    samplet=n draws from the n seeds, with repeats\text{sample}_t = n \text{ draws from the } n \text{ seeds, with repeats}
    P(a seed is never drawn)=(1−1n)n≈37%P(\text{a seed is never drawn}) = \left(1 - \tfrac{1}{n}\right)^{n} \approx 37\%
  2. Its own questions. At each question, the tree may only look at one feature, picked at random: temperature or water. It then finds the best threshold on that feature.
  3. A vote. For a new seed, each tree gives its answer, and the most common answer wins.
    y^=the most common answer of h1(q),…,hT(q)\hat{y} = \text{the most common answer of } h_1(q), \dots, h_{\htmlClass{sym-line}{T}}(q)

Why many trees beat one

One tree can only cut the chart with straight lines across and up, so a slanted border becomes a rough staircase. Each tree's staircase is different, and its mistakes are in different places. When they vote, the mistakes tend to cancel out, and the forest's border follows the slant more closely than any single tree.

Odd seeds near the border fool some trees but not others: a tree that never drew the odd seed does not bend around it. That is why a forest is steadier than one deep tree.

Choosing T, and an honest score

More trees make the vote steadier, and they never make the forest worse at learning; they only cost time. After a few dozen trees the answer hardly changes.

Every tree leaves out about a third of the seeds, which gives a free test. The score Right on unseen seeds asks, for each seed, only the trees that did not draw it. This is called the out-of-bag score, and it tells you how the forest does on seeds it has not learned from.

The model in short

  1. Sample: each tree draws its own seeds, with repeats.
  2. Grow: each question looks at one feature picked at random.
  3. Vote: the forest answers with the most common answer.
  4. Check: test each seed on the trees that never saw it.