I have a 2d array of probabilities, p that sum to 1.0:
p = np.array([[0.05, 0.05, 0.1 , 0. ],
[0.4 , 0.1 , 0.1 , 0.2 ]])
I want to have a function that samples an index from the 2d array p based on the probabilities. For example, I should return [1,0] with a probability of 0.4 or [0,2] with a probability of 0.1.
If I call the function sample(p) which takes the 2d probability array p.
Then with the 2d p array as I've defined above I could expect indices anywhere from [0,0] to [1,3] but not including [0,3] as that has a probability of 0 of appearing:
sample(p)
>> [1,0]
sample(p)
>> [0,2]
sample(p)
>> [1,0]
sample(p)
>> [1,3]
Normally I would use numpy.random.choice however it only accepts 1D array probabilities. Is there a way that I can extend this to work for 2d probability arrays of any size .