numpy.random.Generator.shuffle#
method
- random.Generator.shuffle(x,axis=0)#
Modify an array or sequence in-place by shuffling its contents.
The order of sub-arrays is changed but their contents remains the same.
- Parameters:
- xndarray or MutableSequence
The array, list or mutable sequence to be shuffled.
- axisint, optional
The axis whichx is shuffled along. Default is 0.It is only supported on
ndarrayobjects.
- Returns:
- None
See also
Notes
An important distinction between methods
shuffleandpermutedishow they both treat theaxisparameter which can be found atHandling the axis parameter.Examples
>>>rng=np.random.default_rng()>>>arr=np.arange(10)>>>arrarray([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])>>>rng.shuffle(arr)>>>arrarray([2, 0, 7, 5, 1, 4, 8, 9, 3, 6]) # random
>>>arr=np.arange(9).reshape((3,3))>>>arrarray([[0, 1, 2], [3, 4, 5], [6, 7, 8]])>>>rng.shuffle(arr)>>>arrarray([[3, 4, 5], # random [6, 7, 8], [0, 1, 2]])
>>>arr=np.arange(9).reshape((3,3))>>>arrarray([[0, 1, 2], [3, 4, 5], [6, 7, 8]])>>>rng.shuffle(arr,axis=1)>>>arrarray([[2, 0, 1], # random [5, 3, 4], [8, 6, 7]])
On this page