bundles / numpy 2.4.4 / numpy / random / _generator / Generator / permutation
cython_function_or_method
numpy.random._generator:Generator.permutation
Signature
def permutation(x, axis = 0)Summary
Randomly permute a sequence, or return a permuted range.
Parameters
x: int or array_likeIf
xis an integer, randomly permutenp.arange(x). Ifxis an array, make a copy and shuffle the elements randomly.axis: int, optionalThe axis which
xis shuffled along. Default is 0.
Returns
out: ndarrayPermuted sequence or array range.
Examples
rng = np.random.default_rng()
✓rng.permutation(10)
✗array([1, 5, 6, 7, 8, 9, 3, 0, 2, 4])
rng.permutation([1, 4, 9, 12, 15])
✗array([ 1, 9, 15, 4, 12])
arr = np.arange(9).reshape((3, 3))
✓rng.permutation(arr)
✗array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
rng.permutation("abc")
✓Traceback (most recent call last):
File "<doctest numpy.random._generator:Generator.permutation[0]>", line 1, in <module>
rng.permutation("abc")
~~~~~~~~~~~~~~~^^^^^^^
File "numpy/random/_generator.pyx", line 4965, in numpy.random._generator.Generator.permutation
numpy.exceptions.AxisError: axis 0 is out of bounds for array of dimension 0
arr = np.arange(9).reshape((3, 3))
✓rng.permutation(arr, axis=1)
✗array([[0, 2, 1],
[3, 5, 4],
[6, 8, 7]])
Aliases
numpy.random.Generator.permutation