借助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]]
相關用法
- Python numpy.sum()用法及代碼示例
- Python numpy.inner()用法及代碼示例
- Python numpy.std()用法及代碼示例
- Python numpy.ptp()用法及代碼示例
- Python numpy.mean()用法及代碼示例
- Python numpy.irr()用法及代碼示例
- Python numpy.pmt()用法及代碼示例
- Python numpy.gcd()用法及代碼示例
- Python numpy.lcm()用法及代碼示例
- Python numpy.var()用法及代碼示例
- Python numpy.around()用法及代碼示例
- Python numpy.tan()用法及代碼示例
- Python numpy.cos()用法及代碼示例
- Python numpy.sin()用法及代碼示例
- Python numpy.mod()用法及代碼示例
- Python numpy.exp()用法及代碼示例
- Python numpy.log()用法及代碼示例
- Python numpy.fix()用法及代碼示例
- Python numpy.pv()用法及代碼示例
- Python numpy.fv()用法及代碼示例
注:本文由純淨天空篩選整理自Jitender_1998大神的英文原創作品 numpy.random.shuffle() in python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。