Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Choose list variable given probability of each variable

I've been trying to code a program that uses the softmax activation function in the middle.

Right now, I have a list of probabilities like this:

P[0.10,0.25,0.60,0.05] 

The sum of all the variables in P is always 1.

I wanted a way to pick the index of the list given the probability attached to it. Or, in other words, a function that returned

0 - 10% of the time 1 - 25% of the time 2 - 60% of the time 3 - 5% of the time 

I've absolutely no idea where to start on this. Any help would be appreciated. :)

like image 376
Roughmar Avatar asked Dec 14 '10 08:12

Roughmar


People also ask

How do you choose elements from a list with different probabilities?

Relative weights to choose elements from the list with different probability. First, define the probability for each element. If you specified the probability using the relative weight, the selections are made according to the relative weights. You can set relative weights using the weight parameter.

How do you randomly select from a list in Python?

Use the random. sample() function when you want to choose multiple random items from a list without repetition or duplicates. There is a difference between choice() and choices() . The choices() was added in Python 3.6 to choose n elements from the list randomly, but this function can repeat items.

How can I randomly select an item from a list?

randrange() Method to Randomly Select Elements From a List. This method is used to generate a random number in a range, for lists, we can specify the range to be 0 to its length, and get the index, and then the corresponding value.


1 Answers

You can easily achieve this with numpy. It has a choice function which accepts the parameter of probabilities.

np.random.choice(   ['pooh', 'rabbit', 'piglet', 'Christopher'],    5,   p=[0.5, 0.1, 0.1, 0.3] ) 
like image 185
Salvador Dali Avatar answered Sep 24 '22 15:09

Salvador Dali