Random Forest Voting, Explained

After reading this you will know why averaging many rough decision trees produces a smooth, accurate boundary, how the bootstrap and random splits build the diversity that makes the average work, and how to read the accuracy-versus-trees curve without fooling yourself.

What a random forest is

A single decision tree carves the plane into rectangles. Each split is a hard threshold like "feature 1 less than 0.42", so the boundary looks like a staircase: jagged, sensitive to a few points, prone to memorizing noise. A random forest trains many such trees, each on a slightly different view of the data, then lets them vote. The vote fraction at any location is the share of trees that assign class 1 there.

Here is the hook. Suppose you have 51 trees and a test point sits in a region where 33 trees say "class 1" and 18 say "class 0". The forest predicts class 1, and the vote fraction is 33/51 = 0.647. Move a few points and one tree flips its answer: the fraction shifts to 32/51 = 0.627, but the prediction does not change. That stability is the whole idea. Individual trees are twitchy; their average is calm.

You can grow one tree in the Decision Tree Builder. The forest tool takes that same tree, makes many noisy copies, and averages them.

When to reach for it, and when not

Random forests are a strong default for tabular data with a mix of numeric and categorical features, especially when you do not want to spend time tuning. They handle nonlinear boundaries, ignore irrelevant features fairly well, and rarely blow up if you add more trees. In this browser demo they turn a jagged two-class scatter into a clean boundary in a few seconds.

Reach for something else when you need a single interpretable rule set (use one shallow tree), when the true relationship is close to linear (try logistic regression, which needs far fewer parameters), or when you want a calibrated regression curve from staged residual fits (see gradient boosting). Forests also spend memory: 200 trees of depth 12 store a lot of thresholds, and on very high-dimensional sparse data a linear model often wins on both speed and accuracy.

The math: bagging and the variance drop

Two mechanisms create diversity between trees. The first is bagging (bootstrap aggregating). Each tree trains on a sample of size n drawn with replacement from the n training points. The chance that a given point is never chosen in n draws is:

\left(1 - \frac{1}{n}\right)^{n} \to e^{-1} \approx 0.3679

So each tree leaves out about 37% of the data and sees the rest with some points repeated. The second mechanism is randomized split selection: at each node the tree considers only a random subset of features and thresholds, which stops every tree from making the same first cut.

Why does averaging help? Model each tree's prediction at a point as a noisy number with variance \sigma^2, and suppose any two trees have correlation \rho. The variance of the average of B trees is:

\text{Var}(\bar{f}) = \rho\,\sigma^2 + \frac{1 - \rho}{B}\,\sigma^2

Read the two terms. The second term shrinks like 1/B: more trees kill the independent part of the noise. The first term does not shrink at all; it is set by the correlation \rho. That is exactly why randomizing splits matters. With \rho = 0.5, \sigma^2 = 1 and B = 100, the variance is 0.5 + 0.5/100 = 0.505. Push \rho down to 0.1 and the same 100 trees give 0.1 + 0.9/100 = 0.109, less than a quarter of the noise. Decorrelating the trees buys more than piling on trees.

Notice what the formula does not say: it never grows with B. Adding trees cannot raise variance, so a forest does not overfit by getting larger. It overfits (if at all) by letting each tree grow too deep, which raises \sigma^2.

A worked example you can reproduce

Use the demo defaults: two interleaved classes, a 70/30 train/test split, trees grown to moderate depth. The numbers below track a single test point sitting near the boundary as trees are added one at a time.

Watching one point's vote settle

Say the true class of the point is 1. Each tree votes independently-ish, and correct votes arrive slightly more often than wrong ones because each tree is a weak-but-better-than-chance classifier. Track the running vote fraction:

  1. Tree 1 votes 1. Fraction = 1/1 = 1.000. Prediction 1.
  2. Tree 2 votes 0. Fraction = 1/2 = 0.500. Prediction is a tie, resolved to 0.
  3. Tree 3 votes 1. Fraction = 2/3 = 0.667. Prediction 1.
  4. After 11 trees, 7 voted 1. Fraction = 7/11 = 0.636. Prediction 1.
  5. After 51 trees, 34 voted 1. Fraction = 34/51 = 0.667. Prediction 1, and now stable: one flip changes the fraction by only 1/51 = 0.0196.

The early predictions swing between 0 and 1. By 51 trees a single disagreement no longer moves the decision. That is the vote settling, and it is why the heatmap softens from blocky patches into smooth bands as trees accumulate.

The vote fraction swings wildly for the first dozen trees, then tightens around 0.66. The vertical marker at 51 trees is where a single flip moves the fraction by less than 0.02.

Reading the accuracy curve and the heatmap

The tool shows two things at once. The heatmap is the vote fraction across the whole plane; the curve below is test accuracy as a function of ensemble size. Read them together.

The accuracy curve almost always has the same shape: a fast climb, then a plateau. With the demo data you might see test accuracy jump from 0.78 at one tree to 0.88 at ten trees, then crawl to 0.90 at fifty and sit there. The gain from tree 50 to tree 200 is often under one percentage point. That flat tail is the 1/B term running out of room to shrink.

Typical test accuracy versus number of trees on the demo data
TreesTest accuracyGain over previous row
10.78-
50.850.07
100.880.03
250.8950.015
500.900.005
1000.9020.002

In the heatmap, sharp corners mean few trees or shallow diversity. Smooth gradients mean many decorrelated trees. Regions near 0.5 vote fraction are where the forest is genuinely unsure; regions near 0 or 1 are confident. Do not read the vote fraction as a true probability. It is a share of votes, and it tends to be too confident near the edges and too timid in the middle unless you calibrate it.

With one tree the boundary is a jagged staircase and test accuracy is about 0.78. Raising the tree count to 10 smooths the staircase and lifts accuracy to about 0.88; by 50 trees the boundary is smooth and accuracy plateaus near 0.90. Higher tree correlation flattens the improvement.

Common mistakes

The mistakes below cost accuracy or, worse, produce numbers you trust when you should not.

Confusing vote fraction with probability
A vote of 0.9 means 90% of trees agree, not that the point is class 1 with probability 0.9. Forests are usually overconfident at the extremes.
Growing every tree to full depth on tiny data
Deep trees raise \sigma^2 and memorize noise. Averaging tames variance but cannot remove a shared bias if every tree is deep and correlated.
Reading the training accuracy
A forest of deep trees can hit near 1.0 training accuracy while test accuracy sits at 0.90. Only the 30% held-out split tells you anything useful here.
Expecting more trees to fix underfitting
If accuracy plateaus at 0.85, adding trees from 100 to 500 will not help. Change tree depth or the feature-sampling instead.

This demo trains on a few dozen 2D points. Do not read production behavior from it. Real forests run on thousands of rows and dozens of features, where the variance reduction and the risk of correlated trees look different. The demo shows the mechanism, not the scale.

How it relates to other methods here

A forest is an ensemble of the trees you can grow in the Decision Tree Builder, so start there to see one jagged partition before you average many. If your boundary is nearly linear, the SVM Playground finds a max-margin line with far fewer moving parts, and the Perceptron Learning Animation shows the simplest linear update. For a different ensemble philosophy (correcting residuals in sequence rather than averaging in parallel), compare gradient boosting. And to see the overfitting the forest resists, crank up model complexity in the Overfitting Sandbox and watch train and test error split apart.

Frequently asked questions

Does adding more trees cause overfitting?

No. The variance formula never grows with the number of trees B; the 1/B term only shrinks. Overfitting in a forest comes from letting individual trees grow too deep, not from the count. The accuracy curve climbs and then flattens.

How many trees should I use?

Use enough to reach the plateau. On the demo data that is around 50: going from 50 to 100 trees added only 0.002 accuracy. Add trees until the gain per batch drops below what you care about, then stop.

Why bootstrap instead of using all the data for every tree?

If every tree saw the same data it would learn nearly the same splits, so \rho would be near 1 and averaging would not help. The bootstrap leaves out about 37% of points per tree, which decorrelates them so the (1-\rho)/B term has room to shrink.

Is the vote fraction a real probability?

Treat it as a confidence share, not a calibrated probability. A fraction of 0.647 means 33 of 51 trees agreed. Forests tend to be overconfident near 0 and 1. If you need genuine probabilities, calibrate them against held-out data.

What is the difference between this and gradient boosting?

A forest builds trees in parallel and averages independent votes to cut variance. Gradient boosting builds trees in sequence, each fitting the previous stage's errors, to cut bias. Boosting can overfit if you add too many stages; a forest does not overfit by adding trees.