permutation

Contents

permutation#

class brainstate.random.permutation(x, axis=0, independent=False, key=None)#

Randomly permute a sequence, or return a permuted range.

If x is a multi-dimensional array, it is only shuffled along its first index.

Parameters:
  • x (int or array_like) – If x is an integer, randomly permute np.arange(x). If x is an array, make a copy and shuffle the elements randomly.

  • axis (int) – The axis which x is shuffled along. Default is 0.

  • independent (bool) – Whether to use independent random permutations for each batch. If False (default), the same random permutation is used for all batches. If True, each batch is shuffled independently. Ignored if x is an integer.

  • key (int | Array | ndarray | None) – The key for the random number generator. If not given, the default random number generator is used.

Returns:

out – Permuted sequence or array range.

Return type:

ndarray

Examples

Permute integers from 0 to 9:

>>> import brainstate
>>> result = brainstate.random.permutation(10)
>>> print(result.shape)  # (10,)
>>> print(sorted(result))  # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Permute a given array:

>>> arr = [1, 4, 9, 12, 15]
>>> result = brainstate.random.permutation(arr)
>>> print(result.shape)  # (5,)
>>> print(sorted(result))  # [1, 4, 9, 12, 15]

Permute rows of a 2D array:

>>> import numpy as np
>>> arr = np.arange(9).reshape((3, 3))
>>> result = brainstate.random.permutation(arr)
>>> print(result.shape)  # (3, 3)
>>> print(result.flatten().sort() == np.arange(9).sort())  # True