numpy.random.Generator.permutation#

方法

random.Generator.permutation(x, axis=0)#

随机置换序列,或返回置换范围。

参数
x整数或类数组

如果 x 是整数,则随机置换 np.arange(x)。如果 x 是数组,则创建副本并随机打乱元素。

axis整数,可选

x 进行打乱的轴。默认值为 0。

返回
outndarray

置换后的序列或数组范围。

示例

>>> rng = np.random.default_rng()
>>> rng.permutation(10)
array([1, 7, 4, 3, 0, 9, 2, 5, 8, 6]) # random
>>> rng.permutation([1, 4, 9, 12, 15])
array([15,  1,  9,  4, 12]) # random
>>> arr = np.arange(9).reshape((3, 3))
>>> rng.permutation(arr)
array([[6, 7, 8], # random
       [0, 1, 2],
       [3, 4, 5]])
>>> rng.permutation("abc")
Traceback (most recent call last):
    ...
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], # random
       [3, 5, 4],
       [6, 8, 7]])