Machine Learning

Random Forests — Bagging for Trees

Build a random forest and see why it works: many decision trees, each trained on a bootstrap sample with random feature choices, vote together. Compare the smooth forest boundary with the jagged boundary of a single tree, and read out-of-bag accuracy and feature importance.
Trained live in your browserInteractive

Forest of 50 trees

Data

300
0.4

Forest

50
8
1

Each tree trains on a random sample drawn with replacement.

Forest test accuracy

96.0%

Forest train accuracy

99.6%

Average single tree (test)

89.0%

Out-of-bag accuracy

91.1%

Each point scored only by trees that never saw it.

Feature importance (x1 / x2)

52% / 48%

How to use this tool

  1. 1Pick a dataset, the number of points and the noise.
  2. 2Set the number of trees, depth, leaf size, bootstrap and features per split.
  3. 3Compare the forest’s decision regions with any single tree.
  4. 4Read test accuracy, out-of-bag accuracy and feature importance.

Frequently asked questions

Each deep tree overfits its own bootstrap sample in a different way. Averaging many such trees cancels much of that noise, so the forest generalises better.

About a third of the training points are left out of each tree’s bootstrap sample. Scoring each point only with the trees that never saw it gives a free estimate of test accuracy.

With the CART algorithm and Gini impurity, splitting at midpoints between values — the same method as scikit-learn’s DecisionTreeClassifier.

No. Everything is calculated in your browser; nothing you enter is sent to MuhammadLab.