np.random.shuffle(x)与np.random.permutation(x)

 

将数组打乱随机排列 两种方法:

  1. np.random.shuffle(x):在原数组上进行,改变自身序列,无返回值。
  2. np.random.permutation(x):不在原数组上进行,返回新的数组,不改变自身数组。

 

1. np.random.shuffle(x)

(1)、一维数组

import numpy as np

arr = np.arange(10)
print(arr)

np.random.shuffle(arr)
print(arr)

(2)、对多维数组进行打乱排列时,默认是列维度。

arr = np.arange(12).reshape(3,4)
print(arr)

np.random.shuffle(arr)
print(arr)

 

2. np.random.permutation(x)

(1)、可直接生成一个随机排列的数组

np.random.permutation(10)

(2)、一维数组

np.random.permutation([1, 4, 9, 12, 15])

(3)、多维数组

arr = np.arange(9).reshape((3, 3))
print(arr)

arr2 = np.random.permutation(arr)
print(arr)
print(arr2)

 

3. 区别

从代码可以看出,np.random.shuffle(x)改变自身数组,np.random.permutation(x)不改变自身数组。

 

 

来自:https://blog.csdn.net/brucewong0516/article/details/79012233

 

posted @ 2019-04-30 10:32  做梦当财神  阅读(9939)  评论(1编辑  收藏  举报