Skip to content Skip to sidebar Skip to footer

Vectorizing Numpy Choices

Say I have the 2-dimensional numpy arrays A, B and P and they all have the same shape. A and B represent two choices and P contains probabilities in the interval [0,1]. Now I want

Solution 1:

I found a workaround. Just get a random array r = np.random.rand(*A.shape) and do

C = (r <= p) * A + (r > p) * B

and you are done. Stuff like this must be part of the standard library somewhere, though.

Post a Comment for "Vectorizing Numpy Choices"