numpy.random.Generator.permuted¶
method
- random.Generator.permuted(x, axis=None, out=None)¶
Randomly permute x along axis axis.
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 x in this axis are shuffled. Each slice is shuffled independently of the others. If axis is None, the flattened array is shuffled.
- outndarray, optional
If given, this is the destinaton of the shuffled array. If out is None, a shuffled copy of the array is returned.
- Returns
- ndarray
If out is None, a shuffled copy of x is returned. Otherwise, the shuffled array is stored in out, and out is returned
See also
Examples
Create a
numpy.random.Generator
instance:>>> 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]])
x has 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 x in-place, pass x as the out parameter:
>>> 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
out
parameter is given, the return value isout
:>>> y is x True