jax.random.choice

Contents

jax.random.choice#

jax.random.choice(key, a, shape=(), replace=True, p=None, axis=0)[source]#

Generates a random sample from a given array.

Warning

If p has fewer non-zero elements than the requested number of samples, as specified in shape, and replace=False, the output of this function is ill-defined. Please make sure to use appropriate inputs.

Parameters:
  • key (KeyArrayLike) – a PRNG key used as the random key.

  • a (int | ArrayLike) – array or int. If an ndarray, a random sample is generated from its elements. If an int, the random sample is generated as if a were arange(a).

  • shape (Shape) – tuple of ints, optional. Output shape. If the given shape is, e.g., (m, n), then m * n samples are drawn. Default is (), in which case a single value is returned.

  • replace (bool) – boolean. Whether the sample is with or without replacement. default is True.

  • p (RealArray | None) – 1-D array-like, The probabilities associated with each entry in a. If not given the sample assumes a uniform distribution over all entries in a.

  • axis (int) – int, optional. The axis along which the selection is performed. The default, 0, selects by row.

Return type:

Array

Returns:

An array of shape shape containing samples from a.