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


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

 

將數組打亂隨機排列 兩種方法:

  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)不改變自身數組。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM