Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply numpy random.choice to a matrix of probability values (Vectorized solution)

Tags:

python

numpy

The problem I have is as follows

I have a 1-D list of integers (or np.array) with 3 values

l = [0,1,2]

I have a 2-D list of probabilities (for simplicity, we'll use two rows)

P = 
[[0.8, 0.1, 0.1],
 [0.3, 0.3, 0.4]]

What I want is numpy.random.choice(a=l, p=P), where each row in P (probability distribution) is applied to l. So, I want a random sample to be drawn from [0,1,2] with prob. dist. [0.8, 0.1, 0.1] first, then with prob. dist. [0.3, 0.3, 0.4] next, to give me two outputs.

===== Update ======

I can use for loops or list comprehension, but I am looking for a fast/vectorized solution.

like image 615
max_max_mir Avatar asked Nov 07 '16 20:11

max_max_mir


People also ask

How do you create a NumPy matrix with random values?

To create a matrix of random integers in Python, randint() function of the numpy module is used. This function is used for random sampling i.e. all the numbers generated will be at random and cannot be predicted at hand. Parameters : low : [int] Lowest (signed) integer to be drawn from the distribution.

How do I randomly select a value from a NumPy array?

choice() function is used to get random elements from a NumPy array. It is a built-in function in the NumPy package of python. Parameters: a: a one-dimensional array/list (random sample will be generated from its elements) or an integer (random samples will be generated in the range of this integer)

How do you select an element of a NumPy Matrix?

With the help of Numpy matrix. choose() method, we can select the elements from a matrix by passing a parameter as an array which contain the index of row number to be selected. Output array having the same size as passed in the parameter.

How does NumPy random choice work?

Working of the NumPy random choice() function When we pass the list of elements to the NumPy random choice() function it randomly selects the single element and returns as a one-dimensional array, but if we specify some size to the size parameter, then it returns the one-dimensional array of that specified size.


1 Answers

Here's one way.

Here's the array of probabilities:

In [161]: p
Out[161]: 
array([[ 0.8 ,  0.1 ,  0.1 ],
       [ 0.3 ,  0.3 ,  0.4 ],
       [ 0.25,  0.5 ,  0.25]])

c holds the cumulative distributions:

In [162]: c = p.cumsum(axis=1)

Generate a set of uniformly distributed samples...

In [163]: u = np.random.rand(len(c), 1)

...and then see where they "fit" in c:

In [164]: choices = (u < c).argmax(axis=1)

In [165]: choices
Out[165]: array([1, 2, 2])
like image 125
Warren Weckesser Avatar answered Oct 17 '22 22:10

Warren Weckesser