larray.random.choice

larray.random.choice(choices=None, axes=None, replace=True, p=None, meta=None) Array[source]

Generate a random sample from given choices.

Parameters
choices1-D array-like or int, optional

Values to choose from. If an array, a random sample is generated from its elements. If an int n, the random sample is generated as if choices was la.sequence(n) If p is a 1-D Array, choices are taken from its axis.

axesint, tuple of int, str, Axis or tuple/list/AxisCollection of Axis, optional

Axes (or shape) of the resulting array. If axes is None (the default), a single value is returned. Otherwise, if the resulting axes have a shape of, e.g., (m, n, k), then m * n * k samples are drawn.

replaceboolean, optional

Whether the sample is with or without replacement.

parray-like, optional

The probabilities associated with each entry in choices. If p is a 1-D Array, choices are taken from its axis labels. If p is an N-D Array, each cell represents the probability that the combination of labels will occur. If not given the sample assumes a uniform distribution over all entries in choices.

metalist of pairs or dict or Metadata, optional

Metadata (title, description, author, creation_date, …) associated with the array. Keys must be strings. Values must be of type string, int, float, date, time or datetime.

Returns
Array or scalar

The generated random samples with given axes (or shape).

Raises
ValueError

If choices is an int and less than zero, if choices or p are not 1-dimensional, if choices is an array-like of size 0, if p is not a vector of probabilities, if choices and p have different lengths, or if replace=False and the sample size is greater than the population size.

See also

randint, permutation

Examples

Generate one random value out of given choices (each choice has the same probability of occurring):

>>> la.random.choice(['hello', 'world', '!'])                                       
hello

With given probabilities:

>>> la.random.choice(['hello', 'world', '!'], p=[0.1, 0.8, 0.1])                    
world

Generate a 2 x 3 array with given axes and values drawn from the given choices using given probabilities:

>>> la.random.choice([5, 10, 15], p=[0.3, 0.5, 0.2], axes='a=a0,a1;b=b0..b2')       
a\b  b0  b1  b2
 a0  15  10  10
 a1  10   5  10

Same as above with labels and probabilities given as a one dimensional Array

>>> proba = Array([0.3, 0.5, 0.2], Axis([5, 10, 15], 'outcome'))                   
>>> proba                                                                           
outcome    5   10   15
         0.3  0.5  0.2
>>> choice(p=proba, axes='a=a0,a1;b=b0..b2')                                        
a\b  b0  b1  b2
 a0  10  15   5
 a1  10   5  10

Generate a uniform random sample of size 3 from la.sequence(5):

>>> la.random.choice(5, 3)                                                          
{0}*  0  1  2
      3  2  0
>>> # This is equivalent to la.random.randint(0, 5, 3)

Generate a non-uniform random sample of size 3 from the given choices without replacement:

>>> la.random.choice(['hello', 'world', '!'], 3, replace=False, p=[0.1, 0.6, 0.3])  
{0}*      0  1      2
      world  !  hello

Using an N-dimensional array as probabilities:

>>> proba = Array([[0.15, 0.25, 0.10],
...                 [0.20, 0.10, 0.20]], 'a=a0,a1;b=b0..b2')                        
>>> proba                                                                           
a\b    b0    b1   b2
 a0  0.15  0.25  0.1
 a1   0.2   0.1  0.2
>>> choice(p=proba, axes='draw=d0..d5')                                             
draw\axis   a   b
       d0  a1  b2
       d1  a1  b1
       d2  a0  b1
       d3  a0  b0
       d4  a1  b2
       d5  a0  b1