Bootstrap sampling
Quick answer
Bootstrap sampling draws training rows with replacement. A row can occur multiple times in one resample, and another row may be absent. Bagging uses these different training sets to create diverse predictors. Examples run locally with Python 3.13.5, scikit-learn 1.8.0, NumPy 2.3.5 and SciPy 1.17.0 when imported. ByteMind library execution is unverified.
Trace a fixed draw
ids = ["A", "B", "C", "D"]draw = [0, 0, 3, 1]print([ids[i] for i in draw])print([v for i,v in enumerate(ids) if i not in draw])Expected output:
['A', 'A', 'D', 'B']['C']A appears twice; C is out of this resample.
How it works
Suppose four independent training rows have IDs A, B, C, D. A possible size-four bootstrap draw is A, A, D, B. The repeated A is intentional; C is absent. Draw indices rather than sampling feature and label arrays separately, so each feature vector stays paired with its label. For n rows and m independent uniform draws, the probability that one particular row is absent is (1−1/n)^m. When m=n and n grows, this approaches exp(−1), about 0.368. The expected number of distinct rows is n[1−(1−1/n)^m]. This is an expectation, not a required number of distinct rows. Sampling without replacement instead gives exactly m distinct indices (if m≤n). A local random generator makes the example repeatable in the same environment; it does not ensure identical behavior across versions. A row bootstrap assumes that resampling individual observations is a reasonable approximation to the sampling process. Repeated measurements from the same device or time-dependent observations may need group or block resampling instead. Never include validation or test rows in a training bootstrap.
Compute absence probability
from fractions import Fractionprint(Fraction(3,4)**4)print(4*(1-Fraction(3,4)**4))Expected output:
81/256175/64The expected distinct count can be fractional even though actual counts are integers.
Common mistakes
Do not remove repeated rows from a bootstrap, shuffle labels separately, or treat an OOB row as an independent external test observation. A seed is not evidence of independent data.
Practice and next steps
For three rows and three draws, enumerate the 27 equally likely index sequences and count those that omit row zero. Complete the knowledge check and coding exercise. Next: Bagging and variance.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Bootstrap sampling — knowledge check
Apply bootstrap sampling to new examples.
Which draw is valid from three rows with replacement?
What is the chance one row is absent in two draws from two rows?
What is the expected distinct count for n=1 and m=4?
How do features and labels stay aligned?
Which setting challenges IID row resampling?