Random Forests: Bootstrap Sampling, Feature Subsets, and a 3-Tree Worked Example

See exactly where randomness enters a random forest by building three shallow trees, tracing their votes, and calculating a toy out-of-bag score.

KnowledgeGate Team

Exam prep & CS education

Updated 22 Nov 20266 min read
Random Forests: Bootstrap Sampling, Feature Subsets, and a 3-Tree Worked Example

One decision tree is easy to picture, but saying that a random forest contains "many random trees" does not tell you where the randomness enters or why the final answer is steadier. Three deliberately shallow trees from six rows illustrate both sampling operations, classification of one new row by vote, and an out-of-bag score. The recipe is simple: bootstrap the rows for each tree, draw candidate features afresh at every internal node, fit the trees independently, and aggregate their predictions.

Random forests use two different kinds of randomness

A random forest is an ensemble of decision trees. For each tree, the algorithm draws a bootstrap sample of the rows. At every split, it draws a random subset of the available features and chooses the best split only from that subset. Row and feature sampling are separate operations.

Method

Training rows

Features considered at a split

One decision tree

All rows

All available features

Bagged trees

A different bootstrap per tree

All available features

Random forest

A different bootstrap per tree

A random candidate subset per node

Restricting candidates reduces correlation between trees. Otherwise, one dominant feature may produce the same split repeatedly across different bootstraps. The ML & Data Science map provides wider context.

Classification uses majority vote. Regression uses the arithmetic mean of the tree predictions.

Bootstrap sampling builds a different row multiset for every tree

The binary-classification dataset is:

Row

Weekly revision hours, X1

Mock score, X2

Project completed, X3

Label

R1

1

40

No

Fail

R2

2

45

No

Fail

R3

3

52

No

Fail

R4

4

60

Yes

Pass

R5

5

68

Yes

Pass

R6

6

72

Yes

Pass

Draw six times with replacement for each tree:

  • B1 = [R1, R2, R2, R4, R5, R6], so OOB1 = {R3}

  • B2 = [R1, R3, R3, R4, R4, R6], so OOB2 = {R2, R5}

  • B3 = [R2, R3, R5, R5, R6, R6], so OOB3 = {R1, R4}

A bootstrap is a multiset, not a subset. A repeated row carries repeated weight, while an omitted row is out of bag, or OOB, for that tree.

On one draw, a specified row is missed with probability 1 - 1/6 = 5/6. Across six independent draws:

P(row omitted) = (5/6)^6 = 15,625/46,656 ≈ 0.3349.

The expected OOB count is 6 × 0.3349 ≈ 2.01 rows. This finite-n result differs from the familiar 36.8% large-sample limit, which is not a fixed quota for every bootstrap.

Random feature subsets make the three trees disagree usefully

Keep the forest at depth 1 so every split is visible, and set m_try = 1 at the root. Each stump has one internal node, so it makes one feature-subset draw. A deeper tree repeats the draw independently at every internal node, not once for the whole tree.

  • T1 on B1 draws {X1} and splits at X1 <= 3. The left leaf has R1, R2, R2, all Fail. The right leaf has R4, R5, R6, all Pass.

  • T2 on B2 draws {X2} and splits at X2 <= 56. The left leaf has R1, R3, R3, all Fail. The right leaf has R4, R4, R6, all Pass.

  • T3 on B3 draws {X3} and splits Project completed = No versus Yes. The No leaf has R2, R3, both Fail. The Yes leaf has R5, R5, R6, R6, all Pass.

Every leaf is pure, so its Gini impurity is 1 - 1² = 0. For T1 and T2, weighted child Gini is (3/6)0 + (3/6)0 = 0. For T3, it is (2/6)0 + (4/6)0 = 0. Real leaves are usually mixed.

The Coding & Skills catalogue offers broader programming practice. For the main ensemble contrast, gradient boosting regression builds trees sequentially to correct residuals; a random forest trains bootstrapped trees independently and aggregates them.

Three decision stumps split bootstrap samples by X1, X2, or project completion, with Fail and Pass leaves.

Random forest voting turns three unstable answers into one prediction

Classify Z = (X1 = 3.5, X2 = 58, X3 = No).

  1. T1 sees 3.5 > 3, reaches its right leaf, and votes Pass.

  2. T2 sees 58 > 56, reaches its right leaf, and votes Pass.

  3. T3 sees No, reaches its No leaf, and votes Fail.

The tally is Pass 2, Fail 1, so the forest predicts Pass. Here, 2/3 means only that two of three toy trees voted Pass. It is not automatically a calibrated 66.7% probability. A library may average class proportions from leaves, and calibration is a separate question.

For a theoretical illustration, suppose B predictors each have error variance σ² and pairwise error correlation ρ:

Var(average) = ρσ² + (1 - ρ)σ²/B.

With B = 100 and σ² = 1, ρ = 0.8 gives 0.8 + 0.2/100 = 0.802. With ρ = 0.2, it gives 0.2 + 0.8/100 = 0.208. More trees shrink the second term. Random feature sampling targets the correlation term.

A test point follows three stumps: two vote Pass and one votes Fail, producing a Pass majority.

Out-of-bag prediction uses only trees that did not train on the row

For a row's OOB prediction, aggregate only trees whose bootstraps omitted that row. A tree must never vote on the OOB estimate of a row it trained on.

Row

Eligible OOB tree

Prediction

Result

R1, Fail

T3

Fail because X3 = No

Correct

R2, Fail

T2

Fail because X2 = 45 <= 56

Correct

R3, Fail

T1

Fail because X1 = 3 <= 3

Correct

R4, Pass

T3

Pass because X3 = Yes

Correct

R5, Pass

T2

Pass because X2 = 68 > 56

Correct

R6, Pass

None

No OOB prediction

Not evaluated

Five evaluable rows are correct, so toy OOB accuracy is 5/5 = 100%, not 6/6. R6 appears in all three bootstraps and has no eligible OOB vote.

That perfect score is an artefact of a tiny, clean example. A practical forest has enough trees for most rows to receive many OOB votes. OOB error helps during development, but final reporting still needs an untouched test set, especially after repeated hyperparameter tuning.

Random forest traps that break otherwise correct answers

Treating a bootstrap as sampling without replacement. The sample still has n positions, which causes the confusion. Removing repeats destroys the OOB logic. Write all n draws, keep duplicates, and take the set difference.

Giving each tree one permanent feature subset. That misses the within-tree de-correlation mechanism. Draw a fresh candidate subset at each internal node. These stumps draw once because each has one split.

Reading OOB accuracy as training or guaranteed test accuracy. Use only eligible OOB trees, state how many rows received a vote, and keep final evaluation separate.

If max_features is set to all p features while bootstrapping remains active, the method moves towards ordinary bagged trees and may increase tree correlation.

Random forest questions test the mechanism, not the slogan

Typical questions ask you to calculate the probability that a row is OOB, distinguish bagging from a random forest, or trace given bootstraps, feature choices, tree outputs, and a vote. A conceptual prompt may ask why adding trees cannot remove the ρσ² term, and why random feature sampling can help.

A strong answer names variance reduction, lower tree correlation, classification voting, regression averaging, and fresh feature sampling at each internal node. Approaches to AI supplies wider AI context. No particular exam pattern is required to test this mechanism.

Four-step random-forest summary

Remember four steps: draw n rows with replacement for each tree, sample candidate features afresh at every node, fit diverse trees, and combine classification votes or regression values. OOB rows provide a built-in error estimate because those trees did not see them.

Now reproduce B1 to B3, the three split rules, Z's 2-to-1 vote, and the 5/5 evaluable-row OOB calculation without looking. If you only need the concept, that is the next step. For structured follow-through, Artificial Intelligence (AI) offers a broader AI and machine-learning course.