Sub topic 4
Bootstrap sampling
This chapter assumes you know what a training set is, can read basic NumPy, and understand sampling probabilities. Bootstrap sampling is the first step toward making the trees in a forest differ from one another.
What a bootstrap sample is
A bootstrap sample is a random sample drawn with replacement from a dataset, with the same size as the original. If the training set has n rows, you pick a row at random n times. After each pick you put the row back, so it can be picked again.
The result is a new dataset of n rows that has these properties:
- Some original rows appear two or more times.
- Some original rows do not appear at all.
- Every row has the same chance of being chosen on each draw.
Each tree in a random forest gets its own bootstrap sample. Because the samples differ, the trees see slightly different data and learn slightly different splits.
How much data each sample contains
The chance that a given row is missing from one bootstrap sample is (1 - 1/n)^n. As n grows, this value approaches 1/e, which is about 0.368. So a bootstrap sample contains about 63.2% of the distinct original rows. The other 36.8% are left out.
You can check this by simulation:
import numpy as np
rng = np.random.default_rng(0)
n = 1000
fractions = []
for _ in range(200):
idx = rng.integers(0, n, size=n) # draw n row indices with replacement
fractions.append(len(np.unique(idx)) / n)
print("Mean fraction of distinct rows:", round(float(np.mean(fractions)), 3))
print("Theory, 1 - (1 - 1/n)^n: ", round(1 - (1 - 1 / n) ** n, 3))Both numbers should be close to 0.632.
Why resampling helps
A fully grown tree is sensitive to small changes in its training data. Removing a few rows or repeating others can change the top split, and that changes the whole tree below it. This sensitivity is high variance.
Bootstrap sampling uses that sensitivity on purpose. Each tree is perturbed in a different way, so each makes somewhat different errors. Averaging many such trees cancels part of the error that comes from the random quirks of one sample. The next chapters on bagging and aggregation explain this averaging.
Bootstrap samples also have a second use. The rows a tree never saw form a free validation set for that tree. The out-of-bag chapter builds on this.
Common errors
- Sampling without replacement. If you draw n rows without replacement, you get the same dataset in a different order, so every tree would see identical data. Use replacement. In NumPy, that means
rng.integers(0, n, size=n)orrng.choice(n, size=n, replace=True). - Resampling features instead of rows. The bootstrap acts on rows. Choosing features is a separate step that happens at each split.
- Expecting independent trees. Bootstrap samples overlap by about 63%, so the trees are correlated. Feature subsampling, covered later, reduces that correlation further.
- Ignoring duplicates. A row drawn twice counts twice in the impurity calculation. Scikit-learn handles this internally with sample weights, so you do not need to deduplicate.
Exercise
A training set has 10 rows. What is the probability that a particular row is absent from one bootstrap sample of size 10? Is this higher or lower than the large-n limit of 0.368?
Show answer
The probability is (1 - 1/10)^10 = 0.9^10, which is about 0.349. This is slightly lower than 0.368. The value rises toward 1/e as n grows.