numpy.random.permutation#

random.permutation(x)#

隨機排列序列,或返回排列後的範圍。

如果 x 是一個多維陣列,則僅沿其第一個索引洗牌。

注意

新程式碼應使用 permutation 方法,此方法屬於 Generator 實例;請參閱快速入門

參數:
xint 或 array_like

如果 x 是一個整數,隨機排列 np.arange(x)。如果 x 是一個陣列,則複製並隨機洗牌元素。

返回:
outndarray

排列後的序列或陣列範圍。

另請參閱

random.Generator.permutation

新程式碼應使用此方法。

範例

>>> np.random.permutation(10)
array([1, 7, 4, 3, 0, 9, 2, 5, 8, 6]) # random
>>> np.random.permutation([1, 4, 9, 12, 15])
array([15,  1,  9,  4, 12]) # random
>>> arr = np.arange(9).reshape((3, 3))
>>> np.random.permutation(arr)
array([[6, 7, 8], # random
       [0, 1, 2],
       [3, 4, 5]])