I have a 2d array, say, a = [ [1, 2, 3, 4], [5, 6,7, 8], [9, 10, 11, 12], ...[21, 22, 23, 24] ], and I would like to pick N elements from each row randomly, based on a probability distribution p which can be different for each row.
So basically, I'd like to do something like [ np.random.choice(a[i], N, p=p_arr[i]) for i in range(a.shape[0]) ] without using a loop, where p_arr is a 2d array the same shape as a. p_arr stores the probability distribution for each row in a.
The reason I want to avoid using a for loop is because running a line profiler shows that the loop is slowing down my code by a lot (I have large arrays to work with).
Is there a more python-ic way of doing this?
I checked out these links (here and here) but they don't answer my question.
Thank you!
An example of what I'd like to do without the loop:
a = np.ones([500, 500])
>>> p_arr = np.identity(a.shape[0])
>>> for i in range(a.shape[0]):
... a[i] = a[i]*np.arange(a.shape[0])
...
>>> [print(np.random.choice(a[i], p =p_arr[i])) for i in range(a.shape[0])]
p_arris 2D? Can you share a minimal example?a[i]andi in range(a.shape[0]), instead of just doingnp.random.choice(x) for x in a)?