Tree Models · Lesson 39

Random Forests

Random Forests is an ensemble method based on many decorrelated decision trees.

ConceptWorked examplePracticeKnowledge check
Textbook walkthrough

What Random Forests actually means

Random Forests is an ensemble method based on many decorrelated decision trees. Bootstrap sampling and random feature subsets create diversity; averaging/voting reduces variance compared with a single deep tree.

Random Forests matters because tree models learn nonlinear threshold rules and interactions with little preprocessing, but unconstrained trees can have high variance. Ensembles and pruning trade interpretability, variance and computational cost in different ways.

Deeper walkthrough

Read Random Forests as a mechanism, not a recipe

Treat this as a sequence of observable decisions rather than one opaque command. Stage 1: Draw bootstrap samples of training rows for different trees. Stage 2: At each split, consider only a random subset of features. Stage 3: Grow many trees (often deep) independently. Final checkpoint: Use out-of-bag samples as an internal estimate for some diagnostics while still keeping a separate final evaluation design.

Mechanism

Follow the transformation

Draw bootstrap samples of training rows for different trees.

At each split, consider only a random subset of features.

Grow many trees (often deep) independently.

Evidence

Know what would convince you

  • Fit a tiny or baseline case first and confirm prediction shape/range and a few outputs.
  • Evaluate with the same held-out folds/metric as competing models and inspect variability, not just the mean.
Useful distinctionBagging: Averages models trained on resampled data.
Visual demonstration of Random Forests
Visual demonstration: use the diagram to trace the main objects and state changes involved in Random Forests.
Click a stage to inspect what happens, what changes, and what should be checked before moving on.
Stage 1

Draw bootstrap samples of training rows…

Draw bootstrap samples of training rows for different trees. At this stage of Random Forests, keep the incoming data or object separate from the learned parameter, transformed object, or statistic so the change can be reproduced and independently checked.

Transformation focus: keep the input and produced parameters/result separate so the change is observable and reproducible.
How it works

Trace the mechanism step by step

  1. Draw bootstrap samples of training rows for different trees.
  2. At each split, consider only a random subset of features.
  3. Grow many trees (often deep) independently.
  4. Average regression predictions or class probabilities/votes.
  5. Use out-of-bag samples as an internal estimate for some diagnostics while still keeping a separate final evaluation design.
Worked demonstration

Make the concept concrete

Demonstration

Python / scikit-learn example

# Step 1 — Import only the named objects needed by the following steps, keeping dependencies explicit.
from sklearn.ensemble import RandomForestClassifier
# Step 2 — Compute the right-hand expression and store its result in `X` for the next step.
X=[[0],[1],[2],[3],[4],[5]]; y=[0,0,0,1,1,1]
# Step 3 — Fit the model or transformer, learning its parameters from the supplied training data.
m=RandomForestClassifier(n_estimators=50, random_state=0, oob_score=True, bootstrap=True).fit(X,y)
# Step 4 — Display the current value explicitly so the result/state can be inspected during execution.
print(round(m.oob_score_,3))
Expected / illustrative result
An out-of-bag estimate is produced from samples not used to fit each tree; it is useful diagnostic evidence, not a replacement for a final independent evaluation.
Interpret the result.

For Random Forests, connect the reported result to the exact training/validation/prediction step that produced it and check one prediction, fold or metric component independently.

Distinctions & related ideas

Know what this is — and what it is not

BaggingAverages models trained on resampled data.
Random forestBagging + random feature subsets at splits.
Single treeHighly interpretable path but less stable.
BoostingBuilds learners sequentially to correct previous errors rather than independently averaging them.
Use deliberately

When it is appropriate

Use Random Forests when its inductive assumptions fit the feature/target structure and it can be compared fairly with a simpler baseline on unseen data.

Boundary conditions

When to stop or reconsider

Prefer a simpler or different model when the sample size, representation, computational budget, interpretability requirement or data geometry conflicts with this method.

Common mistakes

Failure modes to recognise

  • Judging the model only by training fit instead of generalisation on held-out data.
  • Comparing models with inconsistent preprocessing, folds or evaluation metrics.
  • Tuning complexity without checking a simple baseline, error patterns and variance across splits.
Verification

How to check the result

  • Fit a tiny or baseline case first and confirm prediction shape/range and a few outputs.
  • Evaluate with the same held-out folds/metric as competing models and inspect variability, not just the mean.
  • Inspect errors/residuals or decision boundaries and vary one key hyperparameter to verify expected behaviour.
Hands-on practice

Demonstrate understanding

Try this:

Build a tiny, inspectable example of Random Forests. First draw bootstrap samples of training rows for different trees. Then at each split, consider only a random subset of features. Write the expected result before running it, and explain one condition that would make the result misleading or invalid.

Start with a small baseline and a fixed validation split/fold assignment. Predict what increasing or decreasing one complexity control should do before testing it.
Knowledge check

Check reasoning, not memorisation

Before trusting a result from Random Forests, which check provides the strongest evidence that you understand and applied it correctly?

Quick reference

Keep the important distinctions visible

Step 1Draw bootstrap samples of training rows for different trees.
Step 2At each split, consider only a random subset of features.
Step 3Grow many trees (often deep) independently.
Step 4Average regression predictions or class probabilities/votes.
Lesson summary

What to remember

  • Random Forests is an ensemble method based on many decorrelated decision trees. Bootstrap sampling and random feature subsets create diversity; averaging/voting reduces variance compared with a single deep tree.
  • Draw bootstrap samples of training rows for different trees.
  • Judging the model only by training fit instead of generalisation on held-out data.
  • Fit a tiny or baseline case first and confirm prediction shape/range and a few outputs.