v9.1.1
Menu

Bootstrap Sample

easyFREEEnsemble MethodsMachine Learning 1

Generate a bootstrap sample (sampling with replacement) using a given list of random indices.

Given a dataset of nn items, a bootstrap sample draws nn items with replacement. To make this deterministic, you are given a list of indices to draw.

Return the selected items as a list.

Example

Input
data = ['a', 'b', 'c', 'd']
indices = [0, 2, 2, 1]
Output
['a', 'c', 'c', 'b']
Reasoning
  • data is the original dataset: n=4n = 4 items, 'a', 'b', 'c', 'd', sitting at positions 00 to 33. indices is a pre-drawn list of n=4n = 4 positions (i1,i2,i3,i4)=(0,2,2,1)(i_1, i_2, i_3, i_4) = (0, 2, 2, 1), standing in for the random draws so the result is reproducible.
  • Draw kk simply returns the item at position iki_k, i.e. outputk=dik_k = d_{i_k}: position 00 gives 'a', position 22 gives 'c', position 22 again gives 'c', and position 11 gives 'b'.
  • Keeping the draws in order gives ['a', 'c', 'c', 'b'] — the same length nn as the original, holding the items themselves (strings here), not their positions.
  • Notice that 'c' appears twice while 'd' never appears: that is what sampling with replacement means. A drawn item stays available for later draws, so some items repeat and others are left out.
  • Here 33 of the 44 distinct items made it in; for large nn a bootstrap sample contains about 1−(1−1/n)n≈63.2%1 - (1 - 1/n)^n \approx 63.2\% of them, and the missing ones are called out-of-bag.

Constraints

  • data: list of items
  • indices: list of integer indices (0 to len(data)-1, may repeat)
  • Return the items at the given indices

Learn this in

More from Machine Learning 1

solution.py

Test results

Sign in to run your code against the test cases.