flopscope.

flopscope.numpy.random.RandomState.choice

fnp.random.RandomState.choice(self, a, size=None, replace=True, p=None)

Generates a random sample from a given 1-D array

Adapted from NumPy docs np.random.RandomState.choice

Arearandom
Typecounted
Cost
\text{numel}(\text{output})$ if replace, else $n \cdot \lceil \log_2 n \rceil
Flopscope Context

Legacy choice sampler; numel(output) if replace else sort_cost(n).

Note.

New code should use the choice method of a Generator instance instead; please see the random-quick-start.

Warning.

This function uses the C-long dtype, which is 32bit on windows and otherwise 64bit on 64bit platforms (and 32bit on 32bit ones). Since NumPy 2.0, NumPy's default integer is 32bit on 32bit platforms and 64bit on 64bit platforms.

Parameters

a:1-D array-like or int

If an ndarray, a random sample is generated from its elements. If an int, the random sample is generated as if it were flops.arange(a)

size:int or tuple of ints, optional

Output shape. If the given shape is, e.g., (m, n, k), then m * n * k samples are drawn. Default is None, in which case a single value is returned.

replace:boolean, optional

Whether the sample is with or without replacement. Default is True, meaning that a value of a can be selected multiple times.

p:1-D array-like, optional

The probabilities associated with each entry in a. If not given, the sample assumes a uniform distribution over all entries in a.

Returns

samples:single item or ndarray

The generated random samples

Raises

:ValueError

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

See also

Notes

Setting user-specified probabilities through p uses a more general but less efficient sampler than the default. The general sampler produces a different sample than the optimized sampler even if each element of p is 1 / len(a).

Sampling random rows from a 2-D array is not possible with this function, but is possible with Generator.choice through its axis keyword.

Examples

Generate a uniform random sample from flops.arange(5) of size 3:

>>> flops.random.choice(5, 3)
array([0, 3, 4]) # random
>>> #This is equivalent to flops.random.randint(0,5,3)

Generate a non-uniform random sample from flops.arange(5) of size 3:

>>> flops.random.choice(5, 3, p=[0.1, 0, 0.3, 0.6, 0])
array([3, 3, 0]) # random

Generate a uniform random sample from flops.arange(5) of size 3 without replacement:

>>> flops.random.choice(5, 3, replace=False)
array([3,1,0]) # random
>>> #This is equivalent to flops.random.permutation(flops.arange(5))[:3]

Generate a non-uniform random sample from flops.arange(5) of size 3 without replacement:

>>> flops.random.choice(5, 3, replace=False, p=[0.1, 0, 0.3, 0.6, 0])
array([2, 3, 0]) # random

Any of the above can be repeated with an arbitrary array-like instead of just integers. For instance:

>>> aa_milne_arr = ['pooh', 'rabbit', 'piglet', 'Christopher']
>>> flops.random.choice(aa_milne_arr, 5, p=[0.5, 0.1, 0.1, 0.3])
array(['pooh', 'pooh', 'pooh', 'Christopher', 'piglet'], # random
      dtype='<U11')