Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficiently draw random samples without replacement from an array in python

I need to draw random samples without replacement from a 1D NumPy array. However, performance is critical since this operation will be repeated many times.

Here’s the code I’m currently using:

import numpy as np

# Example array
array = np.array([10, 20, 30, 40, 50])

# Number of samples to draw
num_samples = 3

# Draw samples without replacement
samples = np.random.choice(array, size=num_samples, replace=False)

print("Samples:", samples)

While this works for one sample, it requires a loop to generate multiple samples, and I believe there could be a way to optimize or vectorize this operation to improve performance when sampling multiple times.

  • Is there a way to vectorize or otherwise optimize this operation?
  • Would another library (e.g., TensorFlow, PyTorch) provide better performance for this task?
  • Are there specific techniques for bulk sampling that avoid looping in Python?
like image 992
Mark Avatar asked Feb 24 '26 07:02

Mark


1 Answers

First you should use the choice method of a Generator instance, see here. This will increase the performance substantially, according to this post (if this is still up to date):

rng = np.random.default_rng()

samples = rng.choice(array, size=num_samples, replace=False)
like image 88
JE_Muc Avatar answered Feb 25 '26 22:02

JE_Muc