numpy.random.shuffle()與numpy.random.permutation()的區別


參考API:https://docs.scipy.org/doc/numpy/reference/routines.random.html


1. numpy.random.shuffle()

  API中關於該函數是這樣描述的:

Modify a sequence in-place by shuffling its contents.
This function only shuffles the array along the first axis of a multi-dimensional array. The order of sub-arrays is changed but their contents remains the same.

  也就是說,numpy.random,shuffle(x)是進行原地洗牌,直接改變x的值,而無返回值。對於多維度的array來說,只對第一維進行洗牌,比如一個 $ 3 \times 3 $ 的array,只對行之間進行洗牌,而行內內容保持不變。
  例子:


2. numpy.random.permutation()

  API中關於該函數是這樣描述的:

Randomly permute a sequence, or return a permuted range.
If x is a multi-dimensional array, it is only shuffled along its first index.

  也就是說,numpy.random,permutation(x)是返回一個被洗牌過的array,而x不變。對於多維度的array來說,只對第一維進行洗牌,比如一個 $ 3 \times 3 $ 的array,只對行之間進行洗牌,而行內內容保持不變。
  例子:


免責聲明!

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



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