lundi 25 février 2019

Sample from a 2d probability numpy array?

Say that I have an 2d array ar like this:

0.9, 0.1, 0.3
0.4, 0.5, 0.1
0.5, 0.8, 0.5

And I want to sample from [1, 0] according to this probability array.

rdchoice = lambda x: numpy.random.choice([1, 0], p=[x, 1-x])

I have tred two methods:

1) reshape it into a 1d array first and use numpy.random.choice and then reshape it back to 2d:

np.array(list(map(rdchoice, ar.reshape((-1,))))).reshape(ar.shape)

2) use the vectorize function.

func = numpy.vectorize(rdchoice)
func(ar)

But these two ways are all too slow, and I learned that the nature of the vectorize is a for-loop and in my experiments I found that map is no faster than vectorize.

I thought this can be done faster. If the 2d array is large it would be unbearably slow.




Aucun commentaire:

Enregistrer un commentaire