Professor Turing

Study guides

What is random forest?

Last updated: 2026-11-23

A random forest is many decision trees that each see a slightly different version of the data, and then vote. Each tree is trained on a bootstrap sample of the rows and, at each split, may choose only among a random subset of the features. For a yes-or-no label the forest predicts the class that most trees pick, and for a number it averages the trees' predictions.

Start learning No account needed

1. Random forest in plain words

A decision tree splits the data with yes-or-no questions, such as "hours per week at or below 3.5?", until each leaf is mostly one label. One deep tree fits its training rows closely, and a few changed rows can change the whole tree. That sensitivity is variance, which the bias-variance tradeoff explains.

A random forest keeps the trees and makes them differ on purpose, in two ways. First, each tree is trained on a bootstrap sample: draw n rows from the n rows you have, one at a time and with replacement, so a row can be drawn more than once and some rows are not drawn at all. Second, at each split a tree can only consider some of the features. Then the trees vote. Because the trees were trained on different rows and asked different questions, they tend to make different mistakes, and a vote across them is steadier than any one tree.

To choose a split, a tree compares how mixed the two sides are. The usual measure is Gini impurity, 2 × p × (1 - p), where p is the share of passes on one side. It is 0 when a side is all one label, and the tree picks the split with the lowest weighted total.

2. A worked example: 10 students, 3 trees, one new student

The numbers below are made up for this page. Each student has hours studied per week, practice quizzes taken and hours of sleep, and an outcome of pass (1) or fail (0).

Row12345678910
Hours2345678946
Quizzes1213243545
Sleep6876586758
Passed0000110111

To keep the arithmetic short, each tree makes a single split, which is called a stump. Real forests grow deeper trees and use many more of them, with the same voting. Each tree draws 10 rows with replacement and is offered two of the three features. The draws and the feature pairs below were fixed once from a random number generator, so you can repeat them.

TreeRows drawnDifferent rowsLeft outFeatures offeredSplit chosen
110, 2, 10, 9, 1, 6, 8, 9, 2, 86 of the 103, 4, 5, 7hours, sleephours at or below 3.5
210, 5, 7, 7, 8, 3, 1, 2, 7, 27 of the 104, 6, 9quizzes, sleepquizzes at or below 4
39, 6, 1, 6, 8, 10, 6, 10, 1, 95 of the 102, 3, 4, 5, 7hours, sleephours at or below 3

Here is how tree 1 chose. Among its draws, hours at or below 3.5 holds three draws that all failed (rows 1 and 2), and the other seven draws all passed, so both sides are pure and the weighted Gini impurity is 0. Tree 3 found the same kind of clean split at 3 hours. Tree 2 was not offered hours, and its sample has row 7 (8 hours, 3 quizzes, failed) three times. With quizzes at or below 4, the low side holds 8 draws with one pass, so its Gini impurity is 2 × (1/8) × (7/8) = 0.21875, and the weighted value is 8/10 × 0.21875 = 0.175. The high side is two draws, both passes.

Now a new student with 5 hours, 3 quizzes and 7 hours of sleep. Tree 1 sees hours above 3.5 and votes pass. Tree 2 sees quizzes at or below 4, where 7 of the 8 draws failed, and votes fail. Tree 3 sees hours above 3 and votes pass. The vote is two votes to one, so the forest predicts pass. Say this student did pass. Then the forest is right and tree 2 is wrong, and tree 2 leaned on three copies of one failing row and was not offered the feature the other two trees used.

Output from the script, run on 2026-11-23: votes 1, 0, 1 -> forest says 1, tree 2 left side gini 0.21875, weighted 0.175 and tree 2 draws row 7 3 times. These come from a short script, random-forest-example.mjs, that builds the three stumps and the vote.

3. A common misconception: a bootstrap sample is a smaller subset of the rows

It is tempting to picture each tree getting a random handful of the rows, such as 7 of the 10. A bootstrap sample is a different thing. It has as many draws as there are rows, 10 here, but the draws are made with replacement, so some rows repeat and the others are left out. In our example the three samples contain 6, 7 and 5 different rows, and row 7 appears three times in tree 2.

The share left out has a size you can compute. For 10 rows, the chance that a given row is missed by one sample is 0.910 = 0.349, so a given row is in a sample with chance 0.651, and about 3.5 of the 10 rows are left out of a typical sample. Ours left out 4, 3 and 5. With many rows, the chance that a row is in a sample approaches 63 percent. The left-out rows are useful, since each tree can be checked on rows it did not see.

4. Test yourself

Close this page and explain, to someone else or to an empty room, why tree 2 voted fail on the new student while the forest still said pass, and what two things the forest changes between trees. Then reopen the page and compare. The first place your explanation drifted is the part to study again.

For another way to predict a yes-or-no label, what is logistic regression fits one curve instead of many trees. For the general method, see how to test yourself.

What the sources found, and their limits

An Introduction to Statistical Learning, sections 8.1 and 8.2. Gareth James, Daniela Witten, Trevor Hastie and Robert Tibshirani, first edition, seventh printing, free from the authors. It supports building trees on bootstrapped training samples, choosing a random sample of m predictors as candidates at each split (about the square root of the number of predictors), a majority vote for a class and an average for a number, a single tree changing a lot with small changes in the data, the Gini index as a measure of node purity, each bagged tree using about two-thirds of the observations with the rest out of bag, and the reason for limiting the predictors, which is that one very strong predictor would otherwise lead most trees to split on it first. Source: Chapter 8, Tree-Based Methods, read on 2026-11-23. Limits: the book defines the Gini index as a sum over classes, which for two labels equals 2 × p × (1 - p) as used here, but the way we weight the two sides of a split by how many draws each holds is the usual convention and we did not find it spelled out in the pages we read. The 10 students, the three stumps and the new student are ours, made up to show the arithmetic. Real forests use deeper trees, so the votes here are a small model of the idea and not a result about any real data.

How Professor Turing helps

Professor Turing is a tutor for adults learning any subject, in college or on their own. It asks you questions instead of giving the final answer, does not grade a bare guess, and remembers what you got wrong. No account is needed to start.

Bring a topic you are studying, such as how a random forest votes. Its first reply asks what you already know before it teaches anything. If you answer with a bare guess, such as a term, it asks how you got there instead of saying right or wrong, and when you stay stuck it changes its approach and makes the next step smaller instead of giving the final answer. Later in a session it can offer a short test without help, scored apart from the tutoring, so you can tell what you can do alone from what the tutor carried you through.

It does not read or mark a written answer, and it does not write your explanation for you, so the explanation has to be yours. You must be 18 or older, and conversations are deleted after 7 days.

FAQ

Why does each tree get only some of the features? If one feature is much stronger than the others, each tree would split on it first and the trees would look alike, so their votes would add little. In our example, tree 2 was offered quizzes and sleep but not hours, so it asked a different question from trees 1 and 3 and ended up voting fail on the new student.

What are the left-out rows for? A tree does not see the rows its bootstrap sample left out, so those rows give an honest check on that tree. In our example, tree 1 gets 10 of its 10 draws right, since its split separates them cleanly, and only 1 of the 4 rows it left out (rows 3, 4, 5 and 7). The second number is the one that says how the tree does on rows it has not seen.

Start with what you're working on

Tell the tutor which topic you are studying and let it ask you the questions.

Start learning No account needed

See also: all study guides