Bootstrap Unique and Out-of-Bag Fractions

Quick Overview

Write vectorized NumPy functions that build a bootstrap in-bag mask, compute the unique and out-of-bag fraction of each draw, and compare a seeded simulation with the exact expectation. It tests fancy indexing, bootstrap probability and the sampling behind random forest OOB estimates.

Bootstrap Unique and Out-of-Bag Fractions

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement four vectorized NumPy functions that measure how many distinct rows a bootstrap draw contains. `indices` is a 2-D integer array of shape `(n_draws, draw_size)` with values in `[0, n_samples)`. - `in_bag_mask(indices, n_samples)`: bool array of shape `(n_draws, n_samples)`, `True` where a row appears in that draw. `draw_size` may differ from `n_samples`. - `bootstrap_fractions(indices, n_samples)`: returns `(unique, oob)`, float arrays with one value per draw: the distinct count divided by `n_samples`, and `1 - unique`. - `expected_unique_fraction(n_samples)`: the exact value `1 - (1 - 1/n)**n` as a float. - `simulate_bootstrap(n_samples, n_draws, seed)`: draw indices with `np.random.default_rng(seed).integers(0, n_samples, size=(n_draws, n_samples))`, so the same seed gives the same result. Return a dict of floats with the keys `unique_fraction` and `oob_fraction` (means over draws), `expected_unique_fraction`, and `limit_unique_fraction` (${1 - e^{-1}}$). Example: ```python idx = [[0, 0, 2, 3], [1, 1, 1, 1], [3, 2, 1, 0]] in_bag_mask(idx, 4) # [[T, F, T, T], [F, T, F, F], [T, T, T, T]] bootstrap_fractions(idx, 4) # unique = [0.75, 0.25, 1.0], oob = [0.25, 0.75, 0.0] expected_unique_fraction(2) # 0.75 ``` The first draw picks row 0 twice and never picks row 1, so 3 of its 4 rows are in the bag. For two rows, the four equally likely draws (0, 0), (0, 1), (1, 0) and (1, 1) contain 1, 2, 2 and 1 distinct rows, an average of 1.5 out of 2. ```hint Let duplicates take care of themselves Writing `True` into the same cell twice leaves it `True`. Look for a single fancy-indexing assignment that pairs every draw number with that draw's indices. ``` ### Constraints & Assumptions - `indices` is a 2-D integer array of shape `(n_draws, draw_size)` with values in `[0, n_samples)`, and `draw_size` may differ from `n_samples`. - `n_samples` is at least 1. - The functions are vectorized: no Python loops over draws or rows. ### What a Strong Answer Covers - A loop-free in-bag mask that handles repeated indices and a draw size different from `n_samples`. - Per-draw unique and OOB fractions computed as row reductions of the mask and returned as floats. - The exact expectation explained from the chance that one row is missed by every pick, and its limit as `n` grows. - A seeded simulation that returns the four keys as floats, with averages close to the exact value. - The time and memory cost of the mask approach. ### Follow-up Questions - Why does the unique fraction approach ${1 - e^{-1}}$, and how quickly does the exact value converge to it? - How does the OOB fraction change when each draw takes `m` rows instead of `n_samples` rows? - How does a random forest use the out-of-bag rows, and how many trees score each row on average?

Overview: Write vectorized NumPy functions that build a bootstrap in-bag mask, compute the unique and out-of-bag fraction of each draw, and compare a seeded simulation with the exact expectation. It tests fancy indexing, bootstrap probability and the sampling behind random forest OOB estimates.

General logo
General
Nov 24, 2026
easyMachine Learning EngineerOtherMachine Learning
0
0

Implement four vectorized NumPy functions that measure how many distinct rows a bootstrap draw contains. indices is a 2-D integer array of shape (n_draws, draw_size) with values in [0, n_samples).

  • in_bag_mask(indices, n_samples) : bool array of shape (n_draws, n_samples) , True where a row appears in that draw. draw_size may differ from n_samples .
  • bootstrap_fractions(indices, n_samples) : returns (unique, oob) , float arrays with one value per draw: the distinct count divided by n_samples , and 1 - unique .
  • expected_unique_fraction(n_samples) : the exact value 1 - (1 - 1/n)**n as a float.
  • simulate_bootstrap(n_samples, n_draws, seed) : draw indices with np.random.default_rng(seed).integers(0, n_samples, size=(n_draws, n_samples)) , so the same seed gives the same result. Return a dict of floats with the keys unique_fraction and oob_fraction (means over draws), expected_unique_fraction , and limit_unique_fraction ( 1−e−1{1 - e^{-1}} ).

Example:

idx = [[0, 0, 2, 3], [1, 1, 1, 1], [3, 2, 1, 0]]
in_bag_mask(idx, 4)
# [[T, F, T, T], [F, T, F, F], [T, T, T, T]]
bootstrap_fractions(idx, 4)
# unique = [0.75, 0.25, 1.0], oob = [0.25, 0.75, 0.0]
expected_unique_fraction(2)  # 0.75

The first draw picks row 0 twice and never picks row 1, so 3 of its 4 rows are in the bag. For two rows, the four equally likely draws (0, 0), (0, 1), (1, 0) and (1, 1) contain 1, 2, 2 and 1 distinct rows, an average of 1.5 out of 2.

Constraints & Assumptions

  • indices is a 2-D integer array of shape (n_draws, draw_size) with values in [0, n_samples) , and draw_size may differ from n_samples .
  • n_samples is at least 1.
  • The functions are vectorized: no Python loops over draws or rows.

What a Strong Answer Covers Guidance

  • A loop-free in-bag mask that handles repeated indices and a draw size different from n_samples .
  • Per-draw unique and OOB fractions computed as row reductions of the mask and returned as floats.
  • The exact expectation explained from the chance that one row is missed by every pick, and its limit as n grows.
  • A seeded simulation that returns the four keys as floats, with averages close to the exact value.
  • The time and memory cost of the mask approach.

Follow-up Questions Guidance

  • Why does the unique fraction approach 1−e−1{1 - e^{-1}} , and how quickly does the exact value converge to it?
  • How does the OOB fraction change when each draw takes m rows instead of n_samples rows?
  • How does a random forest use the out-of-bag rows, and how many trees score each row on average?