jeudi 25 janvier 2018

How to sample independently N times without replacement?

Numpy allows me to easily sample s numbers from a range of 0,...,M-1 without replacement, for example this way:

numpy.random.choice(5, 3, replace=False) # sampling for M=5 and s=3

However, how do I do this independently N times?

If I were to write a loop by hand in Python, it would look like this:

N = 10
res = []
for i in range (0, N):
    res.append(numpy.random.choice(5, 3, replace=False)

However, it is best to avoid writing any loops in Python in favor of vectorizing computations using numpy facilities, since this offers much better performance.

Therefore, how to independently sample s numbers without replacement N times from a range of 0,...,M-1 in numpy?




Aucun commentaire:

Enregistrer un commentaire