numpy.random模組中,有很多可以生成隨機數的函式。
在此,對以下的內容進行說明:
生成正態分佈的隨機數。
生成二項式的隨機數。
生成beta分布的隨機數。
生成gamma分布的隨機數。
生成卡方分布的隨機數。
以上內容,使用**進行說明。
numpy.random.rand()返回一組0.0到1.0之間的隨機數。
import numpy as np
rand = np.random.rand(
)# 1個隨機數
print
(rand)
# 0.5399711080561779
arr = np.random.rand(3)
# 長度為3的隨機數陣列
print
(arr)
# [ 0.22393312 0.56723404 0.41167989]
arr = np.random.rand(4,
4)# 4 x 4的隨機數陣列
print
(arr)
# [[ 0.98109413 0.43272792 0.78808567 0.14697466]
# [ 0.14529422 0.66417579 0.62020433 0.39887021]
# [ 0.78762131 0.3616854 0.76995665 0.25530529]
# [ 0.59426681 0.64130992 0.41451061 0.2356074 ]]
arr = np.random.rand(3,
2,2)
# 3 x 2 x 2的隨機數陣列
print
(arr)
# [[[ 0.26072665 0.63424568]
# [ 0.43025675 0.88787556]]
## [[ 0.58189813 0.57733422]
# [ 0.4312518 0.12558879]]
## [[ 0.58588003 0.58290714]
# [ 0.58133023 0.05894161]]]
numpy.random.random_sample()和np.random.rand()相同,返回一組0.0到1.0之間的隨機數。不同的是指定引數的型別。
numpy.random.random_sample()的引數型別為tuple(元組)。
rand = np.random.random_sample(
)# 1個隨機數
print
(rand)
# 0.7812294650415362
arr = np.random.random_sample(3)
# 長度為3的隨機數陣列
print
(arr)
# [ 0.31315625 0.75406842 0.51955327]
arr = np.random.random_sample((4
,4))
# 4 x 4的隨機數陣列
print
(arr)
# [[ 0.17710988 0.63316992 0.00101942 0.94233375]
# [ 0.9439738 0.58700846 0.97807038 0.56899597]
# [ 0.62050185 0.23601975 0.57513176 0.56947325]
# [ 0.64393715 0.92796497 0.83784537 0.1544701 ]]
arr = np.random.random_sample((3
,2,2
))# 3 x 2 x 2的隨機數陣列
print
(arr)
# [[[ 0.26624554 0.33096779]
# [ 0.1814989 0.12867246]]
## [[ 0.1435755 0.45274324]
# [ 0.12867198 0.98908694]]
## [[ 0.86989893 0.67003622]
# [ 0.17530006 0.03146698]]]
print
(np.random.random_sample is np.random.random)
# true
其他:
上述函式的函式名雖然不同,但功能完全相同。
np.random.randint()返回一組任意值範圍的整數隨機數。
引數的指定順序為最小值,最大值,尺寸,型別。
arr = np.random.randint(4,
10,(3
,3))
# 4到10的3 x 3的隨機整數陣列
print
(arr)
# [[9 5 6]
# [4 8 9]
# [9 8 5]]
np.random.randn()返回一組平均為0,標準差為1,服從正態分佈的隨機數。
arr = np.random.randn(3,
3)print
(arr)
# [[-0.25630308 0.86118703 0.7163906 ]
# [-0.63096426 -2.09983061 1.28259567]
# [ 1.45971205 0.2939326 0.64207751]]
numpy.random.normal()返回一組平均為loc,標準差為scale,服從正態分佈的隨機數。
numpy.random.normal(loc=0.0, scale=1.0, size=none)引數size的型別為tuple(元組)
arr = np.random.normal(-2
,0.5,(
3,3)
)print
(arr)
# [[-2.40461812 -2.76365861 -1.70312821]
# [-2.29453302 -1.53210319 -1.49669024]
# [-1.90580765 -1.45375908 -2.44137036]]
numpy.random.binomial()返回一組服從引數n,p的二項式隨機數。
引數size的型別為tuple(元組)
arr = np.random.binomial(10,
0.5,10)
print
(arr)
# [7 4 7 5 7 8 5 5 3 4]
numpy.random.beta()返回一組服從引數a,b的beta分布的隨機數。
引數size的型別為tuple(元組)
arr = np.random.beta(2,
2,10)
print
(arr)
# [ 0.36516508 0.86746749 0.54430968 0.31152791 0.57359928 0.48281988
# 0.70518635 0.57312808 0.09019818 0.87831173]
numpy.random.gamma()返回一組服從引數shape,比例scale的gamma分布的隨機數。
引數size的型別為tuple(元組)
arr = np.random.gamma(5,
1,10)
print
(arr)
# [ 3.86630062 1.69144819 3.07071675 3.14181626 3.61405871 8.37772201
# 5.47063142 4.80523142 3.68531649 4.43143731]
numpy.random.chisquare()返回一組服從自由度df的卡方分布的隨機數。
引數size的型別為tuple(元組)
arr = np.random.chisquare(3,
10)print
(arr)
# [ 0.49617849 2.39966829 2.84359974 3.5340327 0.71761612 2.04619564
# 0.35930769 4.00448281 1.2907048 2.99259386]
06 numpy陣列內部操作
import numpy as np a np.array 1,2,3,4,5,6,7,8,9 print np.resize a,3,3 n 改變陣列的形狀 b np.array 1,2,3 4,5,6 1 2 3 10 11 12 4 5 6 13 14 15 2.insert c np.arr...
06 numpy 陣列的索引切片和遍歷
和python裡的列表類似,numpy裡的ndarray也支援索引和下標運算元據。na np.arange 10 print na 2 10 print na 6 2 print na 5 2 print na 1 b np.random.randint 3,21,size 5,4,6 print ...
numpy的隨機數組
建立指定大小的隨機數組,取值範圍 0,1 import numpy as np 建立2行2列取值範圍為 0,1 的陣列 arr np.random.rand 2,2 或import numpy as np 建立一維陣列,元素個數為10,取值範圍為 0,1 arr1 np.random.rand 10...