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?
choicemethod to draw a fixed set of choices or would it also be ok to draw a larger set in one chunk?