larray.random.choice¶

larray.random.
choice
(choices=None, axes=None, replace=True, p=None, meta=None)[source]¶ Generates a random sample from given choices
 Parameters
 choices1D arraylike 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 1D LArray, 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)
, thenm * n * k
samples are drawn. replaceboolean, optional
Whether the sample is with or without replacement.
 parraylike, optional
The probabilities associated with each entry in choices. If p is a 1D LArray, choices are taken from its axis labels. If p is an ND LArray, 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 OrderedDict 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
 LArray 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 1dimensional, if choices is an arraylike 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
Examples
Generate one random value out of given choices (each choice has the same probability of occurring):
>>> la.random.choice(['hello', 'world', '!']) # doctest: +SKIP hello
With given probabilities:
>>> la.random.choice(['hello', 'world', '!'], p=[0.1, 0.8, 0.1]) # doctest: +SKIP 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') # doctest: +SKIP 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 LArray
>>> proba = LArray([0.3, 0.5, 0.2], Axis([5, 10, 15], 'outcome')) # doctest: +SKIP >>> proba # doctest: +SKIP outcome 5 10 15 0.3 0.5 0.2 >>> choice(p=proba, axes='a=a0,a1;b=b0..b2') # doctest: +SKIP 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) # doctest: +SKIP {0}* 0 1 2 3 2 0 >>> # This is equivalent to la.random.randint(0, 5, 3)
Generate a nonuniform 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]) # doctest: +SKIP {0}* 0 1 2 world ! hello
Using an Ndimensional array as probabilities:
>>> proba = LArray([[0.15, 0.25, 0.10], ... [0.20, 0.10, 0.20]], 'a=a0,a1;b=b0..b2') # doctest: +SKIP >>> proba # doctest: +SKIP 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') # doctest: +SKIP draw\axis a b d0 a1 b2 d1 a1 b1 d2 a0 b1 d3 a0 b0 d4 a1 b2 d5 a0 b1