numpy.random.permutation
numpy.random.permutation(x)
随机产生一个序列,或是返回一个排列范围。
如果x是一个多维数组,它仅沿其第一个索引随机排列。
(原数组不会改变,结果返回一个新数组)
参数:
x : int or array_like | 如果x是整数,则随机置换np.arange(x)。 如果x是一个数组,请进行复制并随机排列这些元素。 |
---|
返回:
out : ndarray | 排列的序列或数组范围。 |
---|
官网例子
>>> np.random.permutation(10)
array([1, 7, 4, 3, 0, 9, 2, 5, 8, 6])
>>> np.random.permutation([1, 4, 9, 12, 15])
array([15, 1, 9, 4, 12])
>>> arr = np.arange(9).reshape((3, 3))
>>> np.random.permutation(arr)
array([[6, 7, 8],
[0, 1, 2],
[3, 4, 5]])
类似用法
numpy.random.shuffle 将原数组进行重新排列(原数组改变)。