- 關於隨機:對於隨機,我們采用.rand()
numpy.random.rand(d0,d1,...,dn):
創建指定形狀的數組,然後
用隨機值填充它。
參數:d0, d1, ..., dn : [int, optional] Dimension of the returned array we require, If no argument is given a single Python float is returned.
返回:
Array of defined shape, filled with random values.
-
關於正常:對於隨機,我們采用.normal()
numpy.random.normal(loc = 0.0,scale = 1.0,size = None):創建一個指定形狀的數組,並用隨機值填充它,這實際上是Normal(Gaussian)Distribution的一部分。由於其特征形狀,此分布也稱為鍾形曲線。
參數:
loc : [float or array_like]Mean of the distribution. scale : [float or array_like]Standard Derivation of the distribution. size : [int or int tuples]. Output shape given as (m, n, k) then m*n*k samples are drawn. If size is None(by default), then a single value is returned.
返回:
Array of defined shape, filled with random values following normal distribution.
代碼1:隨機構造一維數組
# Python Program illustrating
# numpy.random.rand() method
import numpy as geek
# 1D Array
array = geek.random.rand(5)
print("1D Array filled with random values : \n", array)
輸出:
1D Array filled with random values : [ 0.84503968 0.61570994 0.7619945 0.34994803 0.40113761]
代碼2:根據高斯分布隨機構造一維數組
# Python Program illustrating
# numpy.random.normal() method
import numpy as geek
# 1D Array
array = geek.random.normal(0.0, 1.0, 5)
print("1D Array filled with random values "
"as per gaussian distribution : \n", array)
# 3D array
array = geek.random.normal(0.0, 1.0, (2, 1, 2))
print("\n\n3D Array filled with random values "
"as per gaussian distribution : \n", array)
輸出:
1D Array filled with random values as per gaussian distribution : [-0.99013172 -1.52521808 0.37955684 0.57859283 1.34336863] 3D Array filled with random values as per gaussian distribution : [[[-0.0320374 2.14977849]] [[ 0.3789585 0.17692125]]]
Code3:Python程序,說明NumPy中隨機與正常的圖形表示
# Python Program illustrating
# graphical representation of
# numpy.random.normal() method
# numpy.random.rand() method
import numpy as geek
import matplotlib.pyplot as plot
# 1D Array as per Gaussian Distribution
mean = 0
std = 0.1
array = geek.random.normal(0, 0.1, 1000)
print("1D Array filled with random values "
"as per gaussian distribution : \n", array);
# Source Code :
# https://docs.scipy.org/doc/numpy-1.13.0/reference/
# generated/numpy-random-normal-1.py
count, bins, ignored = plot.hist(array, 30, normed=True)
plot.plot(bins, 1/(std * geek.sqrt(2 * geek.pi)) *
geek.exp( - (bins - mean)**2 / (2 * std**2) ),
linewidth=2, color='r')
plot.show()
# 1D Array constructed Randomly
random_array = geek.random.rand(5)
print("1D Array filled with random values : \n", random_array)
plot.plot(random_array)
plot.show()
輸出:
1D Array filled with random values as per gaussian distribution : [ 0.12413355 0.01868444 0.08841698 ..., -0.01523021 -0.14621625 -0.09157214] 1D Array filled with random values : [ 0.72654409 0.26955422 0.19500427 0.37178803 0.10196284]
重要:
在代碼3中,圖1清楚地顯示了高斯分布,它是根據通過random.normal()方法生成的值創建的,因此遵循高斯分布。
圖2不遵循任何分布,因為它是根據random.rand()方法生成的隨機值創建的。
相關用法
注:本文由純淨天空篩選整理自 rand vs normal in Numpy.random in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。