Random Forests
A random forest (Breiman, 2001) averages many deep decision trees. Each tree grows on a bootstrap sample and sees a random subset of features at every split. The method extends bagging, short for bootstrap aggregating (Breiman, 1996). Bagging draws bootstrap samples of size with replacement from the training points. It grows one unpruned tree on each sample and combines them:
For regression the forest outputs this average. For classification it takes a majority vote of the trees, or it averages their class proportions and picks the largest.A random forest adds one rule. At each split, the tree may choose only among features drawn at random from the features. Setting recovers bagging. ESL (ch. 15) gives these defaults:
- Classification: , with trees grown until each leaf holds one point.
- Regression: , with a minimum leaf size of five.
Suppose each tree’s prediction at has variance and any two trees have correlation . The average of trees then has variance
More trees shrink only the second term, so the variance falls to as . Feature subsampling lowers and with it this floor.Each bootstrap sample misses a given training point with probability
The out-of-bag (OOB) error predicts each training point with only the trees that did not see it. It then averages the losses over all points. The forest gets this estimate of the test error without a separate validation set. A random forest is the standard strong baseline on tabular data, because it needs little tuning. 5.3. Gradient Boosted Trees usually beat it after tuning, and a single decision tree is easier to read.
Intuition
One deep tree fits its training sample closely, so it changes a lot when the sample changes. A forest averages many such trees. The average is more stable than any one tree. In the same way, the mean of many noisy measurements varies less than one measurement. The average cancels only the part of the errors that differs between trees. Trees grown on bootstrap samples of the same data are similar. They are most similar when one feature is much stronger than the rest. Every tree then splits on that feature first, and the trees become near copies of each other. A random forest hides most features from each split. Different trees then take different paths, their errors become less correlated, and the average becomes more stable. The name refers to this collection of trees, each made different by random draws of rows and features.
Properties
(More Trees Never Overfit) Adding trees changes only the term, so the test error falls toward a limit and does not rise again.
The limit as is the expected prediction over all bootstrap samples and feature draws. A finite forest is a Monte Carlo estimate of that limit, and more trees make the estimate more precise. The number of trees is therefore not a tuning parameter in the usual sense. Practitioners add trees until the OOB error curve flattens, often at a few hundred. The limit itself can still overfit noisy data, because it averages fully grown trees. Segal (2004) found small gains from limiting tree depth. ESL (ch. 15) reports that full-depth trees seldom cost much and save one tuning parameter.
Each bootstrap tree draws a different blocky boundary. The forest takes a vote, and its test error falls and levels off as grows.
\usepackage{tikz}
\usetikzlibrary{animations, arrows.meta}
\begin{document}
\definecolor{hot}{HTML}{D9483B}
\definecolor{cold}{HTML}{2F80ED}
\definecolor{mid}{HTML}{8A8A8A}
\newcommand{\pa}[1]{\filldraw[draw=black, fill=cold] (#1) circle (2.2pt);}
\newcommand{\pb}[1]{\filldraw[draw=black, fill=white] (#1) ++(-1.9pt,-1.9pt) rectangle ++(3.8pt,3.8pt);}
\begin{tikzpicture}[font=\small, >={Stealth[length=2.2mm]}]
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 2s = "true", 4.6s = "false", 21.6s = "false", repeats}}}]
\draw[mid, line width=1.6pt] (2.15,5.40) -- (2.15,4.29) -- (0.93,4.29) -- (0.93,2.22) -- (0.00,2.22);
\draw[mid, line width=1.6pt] (3.85,5.40) -- (3.85,1.27) -- (5.40,1.27);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 4.6s = "true", 16.2s = "false", 21.6s = "false", repeats}}}]
\draw[mid] (2.15,5.40) -- (2.15,4.29) -- (0.93,4.29) -- (0.93,2.22) -- (0.00,2.22);
\draw[mid] (3.85,5.40) -- (3.85,1.27) -- (5.40,1.27);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 4.6s = "true", 7.2s = "false", 21.6s = "false", repeats}}}]
\draw[mid, line width=1.6pt] (0.00,1.31) -- (1.06,1.31) -- (1.06,3.89) -- (3.27,3.89) -- (3.27,1.31) -- (5.40,1.31);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 7.2s = "true", 16.2s = "false", 21.6s = "false", repeats}}}]
\draw[mid] (0.00,1.31) -- (1.06,1.31) -- (1.06,3.89) -- (3.27,3.89) -- (3.27,1.31) -- (5.40,1.31);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 7.2s = "true", 8.7s = "false", 21.6s = "false", repeats}}}]
\draw[mid, line width=1.6pt] (0.00,1.81) -- (1.00,1.81) -- (1.00,4.23) -- (3.34,4.23) -- (3.34,1.27) -- (5.40,1.27);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 8.7s = "true", 16.2s = "false", 21.6s = "false", repeats}}}]
\draw[mid] (0.00,1.81) -- (1.00,1.81) -- (1.00,4.23) -- (3.34,4.23) -- (3.34,1.27) -- (5.40,1.27);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 8.7s = "true", 10.2s = "false", 21.6s = "false", repeats}}}]
\draw[mid, line width=1.6pt] (3.98,0.00) -- (3.98,2.81) -- (3.16,2.81) -- (3.16,3.80) -- (1.31,3.80) -- (1.31,3.06) -- (0.00,3.06);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 10.2s = "true", 16.2s = "false", 21.6s = "false", repeats}}}]
\draw[mid] (3.98,0.00) -- (3.98,2.81) -- (3.16,2.81) -- (3.16,3.80) -- (1.31,3.80) -- (1.31,3.06) -- (0.00,3.06);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 10.2s = "true", 11.7s = "false", 21.6s = "false", repeats}}}]
\draw[mid, line width=1.6pt] (2.15,5.40) -- (2.15,3.85) -- (1.28,3.85) -- (1.28,1.31) -- (0.00,1.31);
\draw[mid, line width=1.6pt] (2.33,5.40) -- (2.33,2.96) -- (2.15,2.96) -- (2.15,2.01) -- (5.40,2.01);
\draw[mid, line width=1.6pt] (2.57,5.40) -- (2.57,2.96) -- (5.40,2.96);
\draw[mid, line width=1.6pt] (3.34,5.40) -- (3.34,3.18) -- (5.40,3.18);
\draw[mid, line width=1.6pt] (5.40,1.31) -- (2.15,1.31) -- (2.15,1.51) -- (5.40,1.51);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 11.7s = "true", 16.2s = "false", 21.6s = "false", repeats}}}]
\draw[mid] (2.15,5.40) -- (2.15,3.85) -- (1.28,3.85) -- (1.28,1.31) -- (0.00,1.31);
\draw[mid] (2.33,5.40) -- (2.33,2.96) -- (2.15,2.96) -- (2.15,2.01) -- (5.40,2.01);
\draw[mid] (2.57,5.40) -- (2.57,2.96) -- (5.40,2.96);
\draw[mid] (3.34,5.40) -- (3.34,3.18) -- (5.40,3.18);
\draw[mid] (5.40,1.31) -- (2.15,1.31) -- (2.15,1.51) -- (5.40,1.51);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 11.7s = "true", 13.2s = "false", 21.6s = "false", repeats}}}]
\draw[mid, line width=1.6pt] (0.00,1.27) -- (1.84,1.27) -- (1.84,4.10) -- (2.41,4.10) -- (2.41,1.27) -- (5.40,1.27);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 13.2s = "true", 16.2s = "false", 21.6s = "false", repeats}}}]
\draw[mid] (0.00,1.27) -- (1.84,1.27) -- (1.84,4.10) -- (2.41,4.10) -- (2.41,1.27) -- (5.40,1.27);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 13.2s = "true", 14.7s = "false", 21.6s = "false", repeats}}}]
\draw[mid, line width=1.6pt] (2.66,5.40) -- (2.66,3.57) -- (1.17,3.57) -- (1.17,3.38) -- (0.00,3.38);
\draw[mid, line width=1.6pt] (3.16,5.40) -- (3.16,1.99) -- (5.40,1.99);
\draw[mid, line width=1.6pt] (0.00,1.31) -- (5.40,1.31);
\draw[mid, line width=1.6pt] (0.00,1.51) -- (5.40,1.51);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 14.7s = "true", 16.2s = "false", 21.6s = "false", repeats}}}]
\draw[mid] (2.66,5.40) -- (2.66,3.57) -- (1.17,3.57) -- (1.17,3.38) -- (0.00,3.38);
\draw[mid] (3.16,5.40) -- (3.16,1.99) -- (5.40,1.99);
\draw[mid] (0.00,1.31) -- (5.40,1.31);
\draw[mid] (0.00,1.51) -- (5.40,1.51);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 14.7s = "true", 16.2s = "false", 21.6s = "false", repeats}}}]
\draw[mid, line width=1.6pt] (0.00,3.23) -- (3.27,3.23) -- (3.27,1.75) -- (5.40,1.75);
\draw[mid, line width=1.6pt] (0.00,3.47) -- (3.27,3.47) -- (3.27,3.57) -- (0.00,3.57);
\draw[mid, line width=1.6pt] (0.00,3.65) -- (5.40,3.65);
\draw[mid, line width=1.6pt] (0.00,3.97) -- (5.40,3.97);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "true", 2s = "false", 21.6s = "false", repeats}}}]
\node[anchor=west] at (-0.6,-1.25) {Sixty training points. The forest will grow one tree at a time.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 2s = "true", 4.6s = "false", 21.6s = "false", repeats}}}]
\draw[hot, line width=2pt] (2.15,5.40) -- (2.15,4.29) -- (0.93,4.29) -- (0.93,2.22) -- (0.00,2.22);
\draw[hot, line width=2pt] (3.85,5.40) -- (3.85,1.27) -- (5.40,1.27);
\filldraw[draw=black, fill=hot] (7.40,3.00) circle (2.4pt);
\node[anchor=west] at (7.60,4.90) {$B = 1$: \ test error $0.172$};
\node[anchor=west] at (-0.6,-1.25) {Tree $1$ is fit to a bootstrap sample. With one tree, the forest is that tree.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 4.6s = "true", 7.2s = "false", 21.6s = "false", repeats}}}]
\draw[hot, line width=2pt] (2.15,5.40) -- (2.15,4.29) -- (0.93,4.29) -- (0.93,2.22) -- (0.00,2.22);
\draw[hot, line width=2pt] (3.85,5.40) -- (3.85,1.31) -- (5.40,1.31);
\draw[hot, very thick] (7.40,3.00) -- (8.48,2.98);
\filldraw[draw=black, fill=hot] (8.48,2.98) circle (2.4pt);
\node[anchor=west] at (7.60,4.90) {$B = 2$: \ test error $0.170$};
\node[anchor=west] at (-0.6,-1.25) {Tree $2$ sees another bootstrap sample and makes different cuts. Red: the vote of all trees so far.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 7.2s = "true", 8.7s = "false", 21.6s = "false", repeats}}}]
\draw[hot, line width=2pt] (0.00,1.81) -- (1.00,1.81) -- (1.00,4.23) -- (3.34,4.23) -- (3.34,1.31) -- (3.85,1.31) -- (3.85,1.27) -- (5.40,1.27);
\draw[hot, very thick] (7.40,3.00) -- (8.48,2.98) -- (9.12,2.25);
\filldraw[draw=black, fill=hot] (9.12,2.25) circle (2.4pt);
\node[anchor=west] at (7.60,4.90) {$B = 3$: \ test error $0.118$};
\node[anchor=west] at (-0.6,-1.25) {Tree $3$ added. The trees disagree in many places. The vote keeps what most trees share.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 8.7s = "true", 10.2s = "false", 21.6s = "false", repeats}}}]
\draw[hot, line width=2pt] (0.00,2.22) -- (0.93,2.22) -- (0.93,3.06) -- (1.00,3.06) -- (1.00,4.23) -- (3.34,4.23) -- (3.34,2.81) -- (3.85,2.81) -- (3.85,1.31) -- (3.98,1.31) -- (3.98,1.27) -- (5.40,1.27);
\draw[hot, very thick] (7.40,3.00) -- (8.48,2.98) -- (9.12,2.25) -- (9.57,1.78);
\filldraw[draw=black, fill=hot] (9.57,1.78) circle (2.4pt);
\node[anchor=west] at (7.60,4.90) {$B = 4$: \ test error $0.085$};
\node[anchor=west] at (-0.6,-1.25) {Tree $4$ added. The trees disagree in many places. The vote keeps what most trees share.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 10.2s = "true", 11.7s = "false", 21.6s = "false", repeats}}}]
\draw[hot, line width=2pt] (0.00,1.81) -- (1.00,1.81) -- (1.00,3.06) -- (1.06,3.06) -- (1.06,3.89) -- (2.15,3.89) -- (2.15,4.23) -- (2.33,4.23) -- (2.33,3.89) -- (2.57,3.89) -- (2.57,4.23) -- (3.34,4.23) -- (3.34,2.96) -- (3.27,2.96) -- (3.27,2.81) -- (3.34,2.81) -- (3.34,2.01) -- (3.85,2.01) -- (3.85,1.51) -- (3.34,1.51) -- (3.34,1.31) -- (3.98,1.31) -- (3.98,1.27) -- (5.40,1.27);
\draw[hot, very thick] (7.40,3.00) -- (8.48,2.98) -- (9.12,2.25) -- (9.57,1.78) -- (9.92,2.12);
\filldraw[draw=black, fill=hot] (9.92,2.12) circle (2.4pt);
\node[anchor=west] at (7.60,4.90) {$B = 5$: \ test error $0.108$};
\node[anchor=west] at (-0.6,-1.25) {Tree $5$ added. The trees disagree in many places. The vote keeps what most trees share.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 11.7s = "true", 13.2s = "false", 21.6s = "false", repeats}}}]
\draw[hot, line width=2pt] (0.00,1.81) -- (1.00,1.81) -- (1.00,3.06) -- (1.06,3.06) -- (1.06,3.89) -- (1.84,3.89) -- (1.84,4.10) -- (2.15,4.10) -- (2.15,4.23) -- (2.33,4.23) -- (2.33,4.10) -- (2.41,4.10) -- (2.41,3.89) -- (2.57,3.89) -- (2.57,4.23) -- (3.34,4.23) -- (3.34,2.96) -- (3.27,2.96) -- (3.27,2.81) -- (3.34,2.81) -- (3.34,2.01) -- (3.85,2.01) -- (3.85,1.51) -- (3.34,1.51) -- (3.34,1.31) -- (3.98,1.31) -- (3.98,1.27) -- (5.40,1.27);
\draw[hot, very thick] (7.40,3.00) -- (8.48,2.98) -- (9.12,2.25) -- (9.57,1.78) -- (9.92,2.12) -- (10.20,2.09);
\filldraw[draw=black, fill=hot] (10.20,2.09) circle (2.4pt);
\node[anchor=west] at (7.60,4.90) {$B = 6$: \ test error $0.106$};
\node[anchor=west] at (-0.6,-1.25) {Tree $6$ added. The trees disagree in many places. The vote keeps what most trees share.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 13.2s = "true", 14.7s = "false", 21.6s = "false", repeats}}}]
\draw[hot, line width=2pt] (0.00,1.31) -- (1.06,1.31) -- (1.06,1.51) -- (0.00,1.51);
\draw[hot, line width=2pt] (0.00,1.81) -- (1.00,1.81) -- (1.00,3.06) -- (1.06,3.06) -- (1.06,3.38) -- (1.17,3.38) -- (1.17,3.57) -- (1.28,3.57) -- (1.28,3.85) -- (2.15,3.89) -- (2.15,4.10) -- (2.33,4.10) -- (2.33,3.89) -- (2.41,3.89) -- (2.41,3.80) -- (2.57,3.80) -- (2.57,3.89) -- (2.66,3.89) -- (2.66,4.23) -- (3.16,4.23) -- (3.16,3.89) -- (3.27,3.89) -- (3.27,2.96) -- (3.16,2.96) -- (3.16,2.81) -- (3.27,2.81) -- (3.27,2.01) -- (3.85,1.99) -- (3.85,1.51) -- (3.27,1.51) -- (3.27,1.31) -- (3.98,1.31) -- (3.98,1.27) -- (5.40,1.27);
\draw[hot, very thick] (7.40,3.00) -- (8.48,2.98) -- (9.12,2.25) -- (9.57,1.78) -- (9.92,2.12) -- (10.20,2.09) -- (10.44,2.22);
\filldraw[draw=black, fill=hot] (10.44,2.22) circle (2.4pt);
\node[anchor=west] at (7.60,4.90) {$B = 7$: \ test error $0.116$};
\node[anchor=west] at (-0.6,-1.25) {Tree $7$ added. The trees disagree in many places. The vote keeps what most trees share.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 14.7s = "true", 16.2s = "false", 21.6s = "false", repeats}}}]
\draw[hot, line width=2pt] (0.00,2.22) -- (0.93,2.22) -- (0.93,3.06) -- (1.00,3.06) -- (1.00,3.23) -- (1.06,3.23) -- (1.06,3.38) -- (1.17,3.38) -- (1.17,3.47) -- (1.06,3.47) -- (1.06,3.57) -- (1.28,3.57) -- (1.28,3.65) -- (1.06,3.65) -- (1.06,3.89) -- (1.84,3.89) -- (1.84,3.97) -- (2.15,3.97) -- (2.15,4.10) -- (2.33,4.10) -- (2.33,3.97) -- (2.41,3.97) -- (2.41,3.89) -- (2.57,3.89) -- (2.57,3.97) -- (2.66,3.97) -- (2.66,4.23) -- (3.16,4.23) -- (3.16,3.97) -- (3.34,3.97) -- (3.34,3.65) -- (3.27,3.65) -- (3.27,2.01) -- (3.85,1.99) -- (3.85,1.75) -- (3.98,1.75) -- (3.98,1.51) -- (3.34,1.51) -- (3.34,1.31) -- (5.40,1.31);
\draw[hot, very thick] (7.40,3.00) -- (8.48,2.98) -- (9.12,2.25) -- (9.57,1.78) -- (9.92,2.12) -- (10.20,2.09) -- (10.44,2.22) -- (10.65,1.91);
\filldraw[draw=black, fill=hot] (10.65,1.91) circle (2.4pt);
\node[anchor=west] at (7.60,4.90) {$B = 8$: \ test error $0.093$};
\node[anchor=west] at (-0.6,-1.25) {Tree $8$ added. The trees disagree in many places. The vote keeps what most trees share.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 16.2s = "true", 16.7s = "false", 21.6s = "false", repeats}}}]
\draw[hot, line width=2pt] (0.00,2.85) -- (0.93,2.85) -- (0.93,3.07) -- (1.06,3.12) -- (1.07,3.38) -- (1.17,3.38) -- (1.17,3.47) -- (1.07,3.47) -- (1.07,3.57) -- (1.28,3.57) -- (1.28,3.65) -- (1.07,3.65) -- (1.07,3.89) -- (1.84,3.89) -- (1.84,3.97) -- (2.15,3.97) -- (2.15,4.10) -- (2.33,4.10) -- (2.33,3.97) -- (2.41,3.97) -- (2.41,3.89) -- (2.57,3.89) -- (2.57,3.97) -- (2.66,3.97) -- (2.66,4.17) -- (3.16,4.17) -- (3.16,3.89) -- (3.27,3.89) -- (3.27,3.78) -- (3.34,3.78) -- (3.34,3.65) -- (3.27,3.65) -- (3.27,3.33) -- (3.34,3.33) -- (3.34,2.96) -- (3.27,2.96) -- (3.27,2.81) -- (3.34,2.81) -- (3.34,2.01) -- (3.85,2.01) -- (3.85,1.75) -- (3.98,1.75) -- (3.98,1.51) -- (3.85,1.51) -- (3.85,1.31) -- (5.40,1.31);
\draw[hot, very thick] (7.40,3.00) -- (8.48,2.98) -- (9.12,2.25) -- (9.57,1.78) -- (9.92,2.12) -- (10.20,2.09) -- (10.44,2.22) -- (10.65,1.91) -- (10.84,1.87) -- (11.00,1.85) -- (11.15,1.81) -- (11.29,1.84);
\filldraw[draw=black, fill=hot] (11.29,1.84) circle (2.4pt);
\node[anchor=west] at (7.60,4.90) {$B = 12$: \ test error $0.088$};
\node[anchor=west] at (-0.6,-1.25) {More trees: the vote settles and the test error levels off.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 16.7s = "true", 17.2s = "false", 21.6s = "false", repeats}}}]
\draw[hot, line width=2pt] (0.00,2.85) -- (0.93,2.85) -- (0.93,3.03) -- (1.06,3.07) -- (1.06,3.12) -- (1.16,3.13) -- (1.16,3.78) -- (1.18,3.89) -- (1.29,3.89) -- (1.29,3.97) -- (1.68,3.97) -- (1.68,3.89) -- (1.84,3.89) -- (1.84,4.10) -- (2.33,4.10) -- (2.33,3.97) -- (2.41,3.97) -- (2.41,3.89) -- (2.54,3.89) -- (2.54,4.17) -- (2.57,4.17) -- (2.57,4.23) -- (3.16,4.23) -- (3.16,3.89) -- (3.34,3.89) -- (3.34,3.18) -- (3.39,3.18) -- (3.39,2.96) -- (3.34,2.96) -- (3.34,2.81) -- (3.41,2.81) -- (3.39,2.01) -- (3.85,1.99) -- (3.85,1.77) -- (3.98,1.75) -- (3.98,1.51) -- (3.85,1.51) -- (3.85,1.31) -- (5.40,1.31);
\draw[hot, very thick] (7.40,3.00) -- (8.48,2.98) -- (9.12,2.25) -- (9.57,1.78) -- (9.92,2.12) -- (10.20,2.09) -- (10.44,2.22) -- (10.65,1.91) -- (10.84,1.87) -- (11.00,1.85) -- (11.15,1.81) -- (11.29,1.84) -- (11.41,1.91) -- (11.53,1.83) -- (11.63,1.83) -- (11.73,1.83) -- (11.83,1.85) -- (11.92,1.85) -- (12.00,1.83) -- (12.08,1.82);
\filldraw[draw=black, fill=hot] (12.08,1.82) circle (2.4pt);
\node[anchor=west] at (7.60,4.90) {$B = 20$: \ test error $0.087$};
\node[anchor=west] at (-0.6,-1.25) {More trees: the vote settles and the test error levels off.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 17.2s = "true", 17.7s = "false", 21.6s = "false", repeats}}}]
\draw[hot, line width=2pt] (0.00,3.04) -- (1.18,3.07) -- (1.19,3.22) -- (1.28,3.23) -- (1.28,3.47) -- (1.19,3.47) -- (1.19,3.57) -- (1.28,3.57) -- (1.28,3.65) -- (1.16,3.68) -- (1.16,3.78) -- (1.19,3.78) -- (1.19,3.89) -- (1.29,3.89) -- (1.29,3.97) -- (1.38,3.97) -- (1.38,4.04) -- (1.68,4.04) -- (1.68,3.89) -- (1.84,3.89) -- (1.84,4.01) -- (2.24,4.04) -- (2.24,4.01) -- (2.33,4.01) -- (2.33,3.97) -- (2.41,3.97) -- (2.41,3.89) -- (2.52,3.89) -- (2.54,4.11) -- (2.59,4.15) -- (3.16,4.15) -- (3.16,3.89) -- (3.21,3.89) -- (3.16,3.87) -- (3.21,3.87) -- (3.21,3.78) -- (3.34,3.78) -- (3.34,3.38) -- (3.27,3.38) -- (3.27,3.33) -- (3.34,3.33) -- (3.34,3.13) -- (3.39,3.13) -- (3.41,2.81) -- (3.82,2.81) -- (3.82,2.37) -- (3.74,2.37) -- (3.74,2.01) -- (3.85,1.99) -- (3.85,1.31) -- (3.92,1.31) -- (3.92,1.27) -- (5.40,1.27);
\draw[hot, very thick] (7.40,3.00) -- (8.48,2.98) -- (9.12,2.25) -- (9.57,1.78) -- (9.92,2.12) -- (10.20,2.09) -- (10.44,2.22) -- (10.65,1.91) -- (10.84,1.87) -- (11.00,1.85) -- (11.15,1.81) -- (11.29,1.84) -- (11.41,1.91) -- (11.53,1.83) -- (11.63,1.83) -- (11.73,1.83) -- (11.83,1.85) -- (11.92,1.85) -- (12.00,1.83) -- (12.08,1.82) -- (12.16,1.81) -- (12.23,1.88) -- (12.30,1.81) -- (12.37,1.88) -- (12.43,1.77) -- (12.49,1.77) -- (12.55,1.81) -- (12.61,1.79) -- (12.66,1.80) -- (12.72,1.72) -- (12.77,1.77) -- (12.82,1.72) -- (12.87,1.76) -- (12.91,1.75) -- (12.96,1.77);
\filldraw[draw=black, fill=hot] (12.96,1.77) circle (2.4pt);
\node[anchor=west] at (7.60,4.90) {$B = 35$: \ test error $0.083$};
\node[anchor=west] at (-0.6,-1.25) {More trees: the vote settles and the test error levels off.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 17.7s = "true", 18.2s = "false", 21.6s = "false", repeats}}}]
\draw[hot, line width=2pt] (0.00,3.03) -- (0.87,3.03) -- (0.87,3.06) -- (1.09,3.07) -- (1.09,3.12) -- (1.16,3.13) -- (1.16,3.22) -- (1.19,3.22) -- (1.16,3.89) -- (2.15,3.89) -- (2.15,3.97) -- (2.24,3.97) -- (2.24,3.89) -- (2.32,3.89) -- (2.32,3.97) -- (2.33,3.89) -- (2.54,3.89) -- (2.54,3.97) -- (2.66,3.97) -- (2.66,4.11) -- (2.74,4.11) -- (2.74,3.97) -- (3.16,3.97) -- (3.16,3.89) -- (3.27,3.89) -- (3.21,3.87) -- (3.27,3.87) -- (3.27,3.78) -- (3.34,3.78) -- (3.34,3.47) -- (3.27,3.47) -- (3.27,3.38) -- (3.21,3.38) -- (3.21,3.33) -- (3.27,3.33) -- (3.27,3.29) -- (3.21,3.29) -- (3.21,3.23) -- (3.27,3.23) -- (3.27,3.07) -- (3.39,3.04) -- (3.38,2.86) -- (3.51,2.85) -- (3.51,2.81) -- (3.82,2.81) -- (3.82,2.37) -- (3.74,2.37) -- (3.74,2.01) -- (3.85,1.99) -- (3.85,1.66) -- (3.91,1.66) -- (3.91,1.51) -- (3.85,1.51) -- (3.85,1.31) -- (5.40,1.31);
\draw[hot, very thick] (7.40,3.00) -- (8.48,2.98) -- (9.12,2.25) -- (9.57,1.78) -- (9.92,2.12) -- (10.20,2.09) -- (10.44,2.22) -- (10.65,1.91) -- (10.84,1.87) -- (11.00,1.85) -- (11.15,1.81) -- (11.29,1.84) -- (11.41,1.91) -- (11.53,1.83) -- (11.63,1.83) -- (11.73,1.83) -- (11.83,1.85) -- (11.92,1.85) -- (12.00,1.83) -- (12.08,1.82) -- (12.16,1.81) -- (12.23,1.88) -- (12.30,1.81) -- (12.37,1.88) -- (12.43,1.77) -- (12.49,1.77) -- (12.55,1.81) -- (12.61,1.79) -- (12.66,1.80) -- (12.72,1.72) -- (12.77,1.77) -- (12.82,1.72) -- (12.87,1.76) -- (12.91,1.75) -- (12.96,1.77) -- (13.00,1.74) -- (13.05,1.77) -- (13.09,1.75) -- (13.13,1.77) -- (13.17,1.74) -- (13.21,1.74) -- (13.24,1.74) -- (13.28,1.76) -- (13.32,1.75) -- (13.35,1.76) -- (13.39,1.76) -- (13.42,1.75) -- (13.45,1.76) -- (13.48,1.76) -- (13.52,1.76) -- (13.55,1.75) -- (13.58,1.75) -- (13.61,1.75) -- (13.64,1.75) -- (13.67,1.74) -- (13.69,1.75) -- (13.72,1.74) -- (13.75,1.74) -- (13.78,1.73) -- (13.80,1.73);
\filldraw[draw=black, fill=hot] (13.80,1.73) circle (2.4pt);
\node[anchor=west] at (7.60,4.90) {$B = 60$: \ test error $0.081$};
\node[anchor=west] at (-0.6,-1.25) {More trees: the vote settles and the test error levels off.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 18.2s = "true", 21.6s = "false", 21.6s = "false", repeats}}}]
\draw[hot, line width=2pt] (0.00,2.22) -- (0.17,2.22) -- (0.17,2.37) -- (0.00,2.37);
\draw[hot, line width=2pt] (0.00,2.43) -- (0.14,2.43) -- (0.14,2.52) -- (0.00,2.52);
\draw[hot, line width=2pt] (0.00,2.81) -- (0.14,2.81) -- (0.14,2.85) -- (0.30,2.85) -- (0.30,2.81) -- (0.87,2.85) -- (0.87,3.04) -- (1.06,3.07) -- (1.07,3.13) -- (1.15,3.13) -- (1.16,3.65) -- (1.07,3.68) -- (1.07,3.78) -- (1.16,3.78) -- (1.15,3.89) -- (1.29,3.89) -- (1.29,3.97) -- (1.68,3.97) -- (1.68,3.89) -- (1.84,3.89) -- (1.84,3.97) -- (2.41,3.97) -- (2.41,3.89) -- (2.52,3.89) -- (2.52,3.97) -- (3.16,3.97) -- (3.16,3.89) -- (3.39,3.89) -- (3.34,3.87) -- (3.34,3.78) -- (3.47,3.76) -- (3.47,3.68) -- (3.41,3.68) -- (3.39,3.47) -- (3.34,3.47) -- (3.34,3.07) -- (3.41,3.07) -- (3.41,2.85) -- (3.82,2.81) -- (3.82,2.60) -- (3.74,2.60) -- (3.74,2.43) -- (3.82,2.43) -- (3.82,2.37) -- (3.74,2.37) -- (3.74,2.01) -- (3.85,1.99) -- (3.85,1.31) -- (3.98,1.31) -- (3.98,1.27) -- (4.52,1.27) -- (4.52,1.31) -- (4.62,1.31) -- (4.62,1.27) -- (5.02,1.27) -- (5.02,1.31) -- (5.07,1.31) -- (5.07,1.27) -- (5.40,1.27);
\draw[hot, line width=2pt] (0.22,2.22) -- (0.22,2.37) -- (0.36,2.37) -- (0.36,2.43) -- (0.30,2.43) -- (0.30,2.52) -- (0.51,2.52) -- (0.51,2.43) -- (0.45,2.43) -- (0.45,2.37) -- (0.51,2.37) -- (0.51,2.22) -- (0.22,2.22);
\draw[hot, line width=2pt] (0.60,2.22) -- (0.60,2.37) -- (0.87,2.37) -- (0.87,2.22) -- (0.60,2.22);
\draw[hot, very thick] (7.40,3.00) -- (8.48,2.98) -- (9.12,2.25) -- (9.57,1.78) -- (9.92,2.12) -- (10.20,2.09) -- (10.44,2.22) -- (10.65,1.91) -- (10.84,1.87) -- (11.00,1.85) -- (11.15,1.81) -- (11.29,1.84) -- (11.41,1.91) -- (11.53,1.83) -- (11.63,1.83) -- (11.73,1.83) -- (11.83,1.85) -- (11.92,1.85) -- (12.00,1.83) -- (12.08,1.82) -- (12.16,1.81) -- (12.23,1.88) -- (12.30,1.81) -- (12.37,1.88) -- (12.43,1.77) -- (12.49,1.77) -- (12.55,1.81) -- (12.61,1.79) -- (12.66,1.80) -- (12.72,1.72) -- (12.77,1.77) -- (12.82,1.72) -- (12.87,1.76) -- (12.91,1.75) -- (12.96,1.77) -- (13.00,1.74) -- (13.05,1.77) -- (13.09,1.75) -- (13.13,1.77) -- (13.17,1.74) -- (13.21,1.74) -- (13.24,1.74) -- (13.28,1.76) -- (13.32,1.75) -- (13.35,1.76) -- (13.39,1.76) -- (13.42,1.75) -- (13.45,1.76) -- (13.48,1.76) -- (13.52,1.76) -- (13.55,1.75) -- (13.58,1.75) -- (13.61,1.75) -- (13.64,1.75) -- (13.67,1.74) -- (13.69,1.75) -- (13.72,1.74) -- (13.75,1.74) -- (13.78,1.73) -- (13.80,1.73) -- (13.83,1.73) -- (13.85,1.74) -- (13.88,1.75) -- (13.90,1.74) -- (13.93,1.74) -- (13.95,1.72) -- (13.97,1.73) -- (14.00,1.74) -- (14.02,1.74) -- (14.04,1.73) -- (14.06,1.72) -- (14.09,1.72) -- (14.11,1.73) -- (14.13,1.73) -- (14.15,1.71) -- (14.17,1.69) -- (14.19,1.71) -- (14.21,1.73) -- (14.23,1.73) -- (14.25,1.71) -- (14.27,1.69) -- (14.29,1.68) -- (14.31,1.70) -- (14.33,1.71) -- (14.35,1.72) -- (14.36,1.71) -- (14.38,1.69) -- (14.40,1.69) -- (14.42,1.68) -- (14.44,1.70) -- (14.45,1.66) -- (14.47,1.69) -- (14.49,1.70) -- (14.50,1.71) -- (14.52,1.70) -- (14.54,1.71) -- (14.55,1.71) -- (14.57,1.71) -- (14.58,1.70) -- (14.60,1.71);
\filldraw[draw=black, fill=hot] (14.60,1.71) circle (2.4pt);
\node[anchor=west] at (7.60,4.90) {$B = 100$: \ test error $0.079$};
\node[anchor=west] at (-0.6,-1.25) {With $100$ trees the error is $0.079$. One tree fit to all the data has error $0.152$.};
\end{scope}
\draw (0,0) rectangle (5.4,5.4);
\node at (2.70,-0.35) {$x_1$};
\node at (-0.35,2.70) {$x_2$};
\draw[->] (7.4,0.6) -- (14.90,0.6);
\draw[->] (7.4,0.6) -- (7.4,5.10);
\draw (7.40,0.6) -- (7.40,0.52) node[below] {\footnotesize $1$};
\draw (8.48,0.6) -- (8.48,0.52) node[below] {\footnotesize $2$};
\draw (9.92,0.6) -- (9.92,0.52) node[below] {\footnotesize $5$};
\draw (11.00,0.6) -- (11.00,0.52) node[below] {\footnotesize $10$};
\draw (12.08,0.6) -- (12.08,0.52) node[below] {\footnotesize $20$};
\draw (13.52,0.6) -- (13.52,0.52) node[below] {\footnotesize $50$};
\draw (14.60,0.6) -- (14.60,0.52) node[below] {\footnotesize $100$};
\draw (7.4,2.00) -- (7.32,2.00) node[left] {\footnotesize $0.1$};
\draw (7.4,3.40) -- (7.32,3.40) node[left] {\footnotesize $0.2$};
\draw (7.4,4.80) -- (7.32,4.80) node[left] {\footnotesize $0.3$};
\node at (11.00,-0.15) {number of trees $B$ (log scale)};
\node[rotate=90] at (6.55,2.70) {test error};
\draw[dashed, thick] (7.4,2.73) -- (14.60,2.73);
\node[anchor=south east, inner sep=2pt] at (14.60,2.73) {\footnotesize one tree on all data: $0.152$};
\pa{2.27,5.00}\pb{1.48,0.32}\pb{1.68,3.88}\pa{4.22,2.91}\pa{1.68,4.95}\pa{5.01,2.36}\pb{2.21,3.31}\pa{3.85,3.35}\pb{2.34,2.39}\pa{3.49,4.96}\pb{0.26,0.48}\pa{4.11,2.48}\pa{4.20,2.91}\pa{2.49,5.15}\pb{1.99,3.49}\pb{2.93,0.60}\pa{0.91,4.41}\pb{2.82,3.67}\pa{2.45,4.59}\pa{4.60,2.32}\pa{0.70,4.49}\pb{2.69,3.01}\pb{2.74,0.20}\pb{2.77,0.52}\pa{0.34,4.47}\pa{0.77,3.90}\pb{2.37,2.71}\pb{2.74,0.90}\pa{0.34,3.44}\pb{0.55,0.35}\pa{0.25,3.26}\pa{5.11,2.92}\pa{0.08,3.13}\pb{2.58,3.71}\pa{0.64,3.64}\pb{5.03,1.18}\pa{5.13,3.26}\pb{4.56,1.12}\pa{2.09,4.71}\pa{4.49,3.18}\pa{3.99,3.08}\pb{2.41,1.66}\pb{2.69,2.79}\pb{2.61,0.06}\pa{5.29,5.35}\pb{3.84,0.99}\pa{4.72,1.35}\pb{0.20,1.00}\pa{4.26,4.70}\pa{5.20,4.34}\pb{1.65,2.99}\pb{3.78,0.30}\pa{0.46,3.86}\pb{1.85,0.50}\pb{2.71,0.49}\pb{3.63,0.45}\pa{5.27,4.44}\pb{1.10,0.83}\pb{3.84,1.27}\pa{4.68,4.06}
\end{tikzpicture}
\end{document}(Bias Equals One Tree’s) The trees are identically distributed, so the forest has the same bias as one of its trees.
Every tree comes from the same random procedure, so each has the same expected prediction . The average of trees has that expectation too. A forest therefore improves on one tree only through lower variance in the bias-variance decomposition. Forests use deep, unpruned trees for this reason, since those have low bias and high variance. A forest of stumps would keep the high bias of a stump. Feature subsampling makes each tree slightly more biased, since a split cannot always use the best feature. A small lowers the test error when the variance it removes exceeds the squared bias it adds.
(Why Feature Subsampling Helps) When one feature dominates, bagged trees all split on it first, and subsampling hides it from most splits.
Suppose one of features predicts house prices far better than the rest. With bagging, nearly every tree uses it at the root. The trees then share their top structure, and stays high. With , the strong feature is missing from the candidates at a given split with probability . About two thirds of the trees therefore start from a different feature. Subsampling can also hurt. Suppose only of features matter and . A split then misses both with probability , and most splits use noise features (ESL 15.3.4).
(OOB Error Is Nearly Free) The OOB estimate needs no refitting, and it is reliable only when the rows are independent.
The OOB estimate behaves like leave-one-out cross-validation, and no model is refit. Each OOB prediction averages only about trees, so it is noisier than the full forest’s prediction. The OOB error therefore slightly overstates the full forest’s test error. The estimate also assumes independent rows. If one patient contributes many rows, a tree almost surely trains on some of them. The patient’s other rows are out of bag for that tree but not new to it. The OOB error is then too optimistic, and validation must split by patient, as in 2.3. Generalization.
(Feature Importance) Impurity importance favors features with many possible split points, and permutation importance avoids that bias.
Impurity importance, also called mean decrease in impurity, sums the impurity decreases from all splits on feature . It then averages this sum over the trees. A continuous feature, or a categorical one with many levels, offers many thresholds. One of them often fits noise, so even a useless customer ID can score high (Strobl et al., 2007). Permutation importance shuffles feature in the OOB data and records the rise in OOB error. A useless feature then scores near zero. Both measures share credit between correlated features. Two near copies of a strong feature can each look much weaker than the feature alone.
Derivation
Let be the prediction of tree at a fixed point. Over the random training sample and the random draws, each has variance , and each pair has covariance . The variance of the average expands into variance terms and covariance terms:
With this is the familiar for independent draws. With every tree is identical, and averaging changes nothing. Trees grown on the same data have , because any two bootstrap samples share many points.
Examples
(Easy) Variance of a Forest
A forest predicts house prices in units of dollars. At a given house, each tree’s prediction has variance . Any two trees have correlation .
What is the variance of the forest's prediction with trees, with , and in the limit? With , the variance is . With , it is . As , it falls to . Going from to trees removes of the gap to the limit. No number of trees gets below , so further gains require a lower .
(Medium) Out-of-Bag Coverage
A bank trains a forest on loan applications.
For one application, how often does a tree leave it out? How many OOB trees does it get with and with ? A bootstrap sample misses the application with probability , close to . Each sample therefore holds about distinct applications. With , the application is out of bag for about trees. The chance that all trees saw it is . With , it is out of bag for only trees on average. All five trees see it with probability , so about of the applications get no OOB prediction. A small forest gives a noisy and incomplete OOB estimate.
(Hard) Bagging Versus a Random Forest
A regression problem has features and noise variance . Bagged trees () have squared bias , variance and correlation . Random-forest trees with have squared bias , variance and .
Compare the expected test MSE of one tree and of trees for each method. How many trees bring the forest's variance within of its limit? The expected test MSE is noise plus squared bias plus variance, as in 2.3.1. Bias-Variance Decomposition. One bagged tree has . One random-forest tree has , so it is worse alone. With trees, bagging has variance and MSE . The forest has variance and MSE . Each forest tree is worse alone, yet the forest wins because its trees are less correlated. The forest's limit is . The extra term falls to , which is of that limit, at .
See Also
- 5.1. Decision Trees, the base learner and the source of its high variance.
- 5.3. Gradient Boosted Trees, which adds shallow trees in sequence to reduce bias.
- 5. Tree-Based Methods, which compares forests with single trees and boosting.
- 4.4.1. Dropout, which trains an implicit average of many thinned networks.