Numpy Sampling with Replacement

To generate a sample with replacement with numpy from a given array, use random.choice which takes as arguments

  • $a$: array to sample from
  • $size$: the size of the samples array/output
  • $replace$: Sample with or without replacement
  • $p$: probabilities associated with each entry. If not provided, the uniform probability is assumed

In the example below, I generate a sample of 21 events from a list ['A', 'B', 'C', 'D', 'E'] with replacement.

import numpy as np

a = ['A', 'B', 'C', 'D', 'E']

# generate samples
sample = np.random.choice( a=a, size=21, replace=True )
sample
array(['E', 'E', 'B', 'C', 'B', 'D', 'E', 'B', 'E', 'A', 'E', 'B', 'C', 'D', 'E', 'E', 'A', 'E', 'B', 'E', 'B'], dtype='