mercredi 16 janvier 2019

Given a numpy array of probabilities, randomly pick an index as weighted by the probabilities

I have a 1 dimensional numpy array. Let's say:

Y = [0, 14, 36, 12, 0, 1, 902, 77]

I would like to select an index at random, weighted by the values of each index.
So, each index would have a probability out of 14+36+12+1+902+77=1042

Index 0 would have a probability of 0 out of 1042 to be chosen, whereas index 6 would have a probability of 902 out of 1042 to be chosen.

What is a good way to go about doing this? Thank you.




Aucun commentaire:

Enregistrer un commentaire