來自:https://blog.csdn.net/brucewong0516/article/details/79012233
將數組打亂隨機排列 兩種方法:
- np.random.shuffle(x):在原數組上進行,改變自身序列,無返回值。
- 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)不改變自身數組。