1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Efficiently draw random samples without replacement from an array in python

Discussão em 'Python' iniciado por Stack, Dezembro 19, 2024.

  1. Stack

    Stack Membro Participativo

    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?

    Continue reading...

Compartilhe esta Página