當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python numpy.random.shuffle()用法及代碼示例

借助numpy.random.shuffle()方法,我們可以在numpy數組中獲得不同整數值的隨機位置,或者可以說數組中的所有值都將被隨機洗牌。

用法:numpy.random.shuffle(x)

Return:返回改組的numpy數組。

範例1:

在此示例中,我們可以看到,通過使用numpy.random.shuffle()方法,我們能夠對numpy數組中的值進行重新排列或更改數組中值的位置。



Python3

# import numpy 
import numpy as np 
import matplotlib.pyplot as plt 
  
gfg = np.arange(10) 
# Using shuffle() method 
np.random.shuffle(gfg) 
  
print(gfg)

輸出:

[7 1 5 0 8 4 3 9 6 2]

範例2:

Python3

# import numpy 
import numpy as np 
import matplotlib.pyplot as plt 
  
gfg = np.arange(16).reshape((4, 4)) 
# Using shuffle() method 
np.random.shuffle(gfg) 
  
print(gfg)

輸出:

[[ 4  5  6  7]

[ 0  1  2  3]

[ 8  9 10 11]

[12 13 14 15]]

相關用法


注:本文由純淨天空篩選整理自Jitender_1998大神的英文原創作品 numpy.random.shuffle() in python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。