On this page
numpy.random.Generator.permuted
method
- random.Generator.permuted(x, axis=None, out=None)
-
Randomly permute
xalong axisaxis.Unlike
shuffle, each slice along the given axis is shuffled independently of the others.- Parameters
-
- xarray_like, at least one-dimensional
-
Array to be shuffled.
- axisint, optional
-
Slices of
xin this axis are shuffled. Each slice is shuffled independently of the others. Ifaxisis None, the flattened array is shuffled. - outndarray, optional
-
If given, this is the destinaton of the shuffled array. If
outis None, a shuffled copy of the array is returned.
- Returns
-
- ndarray
-
If
outis None, a shuffled copy ofxis returned. Otherwise, the shuffled array is stored inout, andoutis returned
See also
Notes
An important distinction between methods
shuffleandpermutedis how they both treat theaxisparameter which can be found at Handling the axis parameter.Examples
Create a
numpy.random.Generatorinstance:>>> rng = np.random.default_rng()Create a test array:
>>> x = np.arange(24).reshape(3, 8) >>> x array([[ 0, 1, 2, 3, 4, 5, 6, 7], [ 8, 9, 10, 11, 12, 13, 14, 15], [16, 17, 18, 19, 20, 21, 22, 23]])Shuffle the rows of
x:>>> y = rng.permuted(x, axis=1) >>> y array([[ 4, 3, 6, 7, 1, 2, 5, 0], # random [15, 10, 14, 9, 12, 11, 8, 13], [17, 16, 20, 21, 18, 22, 23, 19]])xhas not been modified:>>> x array([[ 0, 1, 2, 3, 4, 5, 6, 7], [ 8, 9, 10, 11, 12, 13, 14, 15], [16, 17, 18, 19, 20, 21, 22, 23]])To shuffle the rows of
xin-place, passxas theoutparameter:>>> y = rng.permuted(x, axis=1, out=x) >>> x array([[ 3, 0, 4, 7, 1, 6, 2, 5], # random [ 8, 14, 13, 9, 12, 11, 15, 10], [17, 18, 16, 22, 19, 23, 20, 21]])Note that when the
outparameter is given, the return value isout:>>> y is x True
© 2005–2022 NumPy Developers
Licensed under the 3-clause BSD License.
https://numpy.org/doc/1.23/reference/random/generated/numpy.random.Generator.permuted.html