Python Numpy隨機數總結——numpy.random.rand/randn/randint/random/uniform/seed

在學習一些算法的時候,經常會使用一些隨機數來做實驗,或者說用隨機數來添加一些噪聲。

下面就總結我平常用到的幾個numpy.random庫中的隨機數和seed函數。

目錄

1. rand基本用法

2. randn基本用法

3. 指定數學期望和方差的正態分佈

4. random基本用法及和rand的辨析

5. randint基本用法

6. uniform基本用法

7. seed基本用法


1. rand基本用法

numpy.random.rand(d0, d1, …, dn),產生[0,1)之間均勻分佈的隨機浮點數,其中d0,d1....表示傳入的數組形狀。

示例1

import numpy as np
#產生形狀爲(2,)的數組,也就是相當於有2個元素的一維數組。
temp=np.random.rand(2)
print(temp)  #[0.70284298 0.40041697]
print(type(temp)) # 查看數據類型,<class 'numpy.ndarray'>
print(temp[0])  #查看第一個數
print(type(temp[0])) #查看具體元素的數據類型,<class 'numpy.float64'>

'''
結果如下:
[0.70284298 0.40041697]
<class 'numpy.ndarray'>
0.7028429826756175
<class 'numpy.float64'>
'''

示例2 

#產生一個2*4的數組,數組中的每個元素是[0,1)之間均勻分佈的隨機浮點數
temp=np.random.rand(2,4)
print(temp)
#查看一下數據類型
print(type(temp))


'''
結果如下所示:

[[0.39921792 0.42280677 0.32486705 0.02898902]
 [0.78987787 0.93125733 0.30446905 0.27728128]]
<class 'numpy.ndarray'>

'''

示例3

在rand()裏面也可以沒有參數,返回一個[0,1)之間的隨機浮點數。

#產生一個[0,1)之間的隨機數
temp=np.random.rand() #0.6143086490875544
print(temp) #<class 'float'>
print(type(temp)) # 查看數據類型,<<class 'float'>

 

2. randn基本用法

numpy.random.randn(d0, d1, …, dn)從標準正態分佈中返回一個或多個樣本值。 參數表示樣本的形狀。所謂標準正態分佈就是指這個函數產生的隨機數,服從均值爲0,方差爲1的分佈,使用方法和rand()類似。

arr1=np.random.randn(2,4)  #二行四列,或者說一維大小爲2,二維大小爲4
#均值爲0,方差爲1
print(arr1)
print(type(arr1)) #<class 'numpy.ndarray'>

arr2=np.random.rand()
print(arr2) #0.37338593251088137
print(type(arr2))  #<class 'float'>


'''
結果如下:

[[ 0.56538481  0.41791992  0.73515441  1.73895318]
 [ 2.27590795 -1.17933538 -1.02008043  0.15744222]]
<class 'numpy.ndarray'>
0.37338593251088137
<class 'float'>

'''

 

3. 指定數學期望和方差的正態分佈

很多時候,我們不滿足於僅僅產生服從標準正態分佈的一組隨機數,而是希望能夠靈活的指定均值和方差,可用如下方法實現:

For random samples from N(mu, sigma^2), use:

sigma * np.random.randn(...) + mu

如果希望產生的一組隨機數,服從均值爲mu,方差爲sigma^2的正態分佈,可以用上述公式完成。

#Two-by-four array of samples from N(3, 6.25):
arr3=2.5 * np.random.randn(2,4)+3  #2.5是標準差,3是期望
print(arr3)


"""
結果如下:

[[ 2.58150052  6.20108311  1.58737197  9.64447208]
 [ 2.68126136  0.63854145 -1.34499681  1.68725191]]

"""

 

4. random基本用法及和rand的辨析

numpy.random.random()方法返回隨機生成的一個實數(浮點數),它在[0,1)範圍內。

示例:

import numpy as np

x1=np.random.random()
print(x1)  #0.14775128911185142
print(type(x1))  #<class 'float'>

x2=np.random.random((3,3))
print(x2)
'''
[[0.07151945 0.00156449 0.66673237]
 [0.89764384 0.68630955 0.21589147]
 [0.50561697 0.27617754 0.5553978 ]]
'''
print(type(x2))  #<class 'numpy.ndarray'>
print(x2[1,1])  #0.68630955

注意:

這邊需要注意的是這個函數的參數,只有一個參數“size”,有三種取值,None,int型整數,或者int型元組。

而在之前的numpy.random.rand()中可以有多個參數。

比方說,如果我們要產生一個2*4的隨機數組(不考慮服從什麼分佈),那麼在rand中的寫法是:numpy.random.rand(2,4),而在random中的寫法是numpy.random.random( (2,4) ),這裏面是個元組,是有小括弧的。

temp1=np.random.random()
print(temp1)
print(type(temp1))

"""
參數爲None,返回一個隨機數。
0.06708973062154777
<class 'float'>
"""
#int整數和只有一維的int元組,效果一致

temp2=np.random.random(5)
print(temp2)
print(type(temp2))

temp3=np.random.random((5,))
print(temp3)
print(type(temp3))

'''
[0.69630427 0.35781327 0.17793115 0.24212922 0.51985133]
<class 'numpy.ndarray'>

[0.37211634 0.12352443 0.59535439 0.63269044 0.21921476]
<class 'numpy.ndarray'>
'''
temp4=np.random.random((2,4))
print(temp4)

'''
[[0.06215506 0.30826882 0.28453145 0.34466641]
 [0.84119912 0.81675337 0.27229602 0.21637321]]
'''

當然元組可以是多維的,不止二維,但是用的相對較少。

這個函數基礎是產生[0,1)之間的隨機數,但是我們可以通過其他方式,改變這個範圍。

比如,我們要產生[-5,0)之間的隨機數,可以這樣:5 * numpy.random.random( (3,2) )-5。乘5後範圍變成了[0,5),再減5範圍變成了[-5,0)。

temp5=5 * np.random.random_sample((3, 2)) - 5
print(temp5)

'''
[[-0.6397764  -3.58968428]
 [-2.7789179  -4.74474732]
 [-1.7913781  -1.78819213]]
'''

和rand()的辨析。

一個是參數類型的不同,前文已經提到。

然後參看官方的幫助文檔;

random:Return random floats in the half-open interval [0.0, 1.0).

rand:Create an array of the given shape and populate it with  random samples from a uniform distribution  over ``[0, 1)``.

英文這種東西,只可意會。不可翻譯。

 

5. randint基本用法

用於生成指定範圍內的整數。

具體函數:randint(low, high=None, size=None, dtype='l')

其中low是整型元素,表示範圍的下限,可以取到。high表示範圍的上限,不能取到。也就是左閉右開區間。

high沒有填寫時,默認生成隨機數的範圍是[0,low)

size可以是int整數,或者int型的元組,表示產生隨機數的個數,或者隨機數組的形狀。

dtype表示具體隨機數的類型,默認是int,可以指定成int64。

示例1:

#產生一個[0,10)之間的隨機整數
temp1=np.random.randint(10)
print(temp1)
print(type(temp1))  #<class 'int'>

'''
5
<class 'int'>
'''

可以看出默認類型是int型,可以指定成int64.

示例2:

temp2=np.random.randint(10,dtype="int64")
print(type(temp2))

'''
<class 'numpy.int64'>
'''

示例3:

#產生[0,10)之間的隨機整數8個,以數組的形式返回
temp3=np.random.randint(10,size=8)
print(temp3)
'''
[6 6 0 3 4 2 5 3]
'''

temp4=np.random.randint(10,size=(2,4))
print(temp4)
'''
[[7 5 4 5]
 [5 2 7 6]]
'''

示例4:

temp5=np.random.randint(5,10,size=(2,4))
print(temp5)

'''
[[8 5 8 6]
 [9 8 6 9]]
'''

 

6. uniform基本用法

從指定範圍內產生均勻分佈的隨機浮點數。

函數:uniform(low=0.0, high=1.0, size=None)

low表示範圍的下限,float型,或float型數組,默認爲0.0.

high表示範圍的上限,float型,或float型數組,默認爲1.0.

size表示“形狀”或“個數”,int型,或int型元組,默認爲None。

示例1:

#默認產生一個[0,1)之間隨機浮點數
temp=np.random.uniform()
print(temp) #0.9520851072880187

示例2:

temp=np.random.uniform(1,5,size=5)
print(temp)
'''
[4.94143282 1.56775945 3.74670851 4.06208558 1.25997891]
'''

temp=np.random.uniform(1,5,size=(2,4))
print(temp)
'''
[[2.3823538  3.09401949 3.92113219 1.0436445 ]
 [1.28329041 1.17418269 2.68699106 4.94529039]]
'''

拓展1

我想到一個問題,如果表示範圍的數只有一個是什麼意思?

help查看文檔,low和size都是optional,那只有一個數應該表示爲上限,但是實驗結果並非如此:

temp=np.random.uniform(1.0001,size=10000000)
print(temp)
print(np.all(temp>= 1))
print(np.all(temp<1.0001))

'''
[1.00002898 1.00007222 1.00000062 ... 1.00008765 1.00002739 1.00004577]
True
True
'''

temp=np.random.uniform(0.99,size=10000000)
print(temp)
print(np.all(temp>= 0.99))
print(np.all(temp<1))

'''
[0.9928493  0.99711935 0.99146187 ... 0.9945594  0.99915136 0.99305246]
True
True
'''

temp=np.random.uniform(1,size=10000000)
print(temp)
print(np.all(temp==1))

'''
[1. 1. 1. ... 1. 1. 1.]
True
'''

由上面代碼,得出結果,如果範圍只有一個參數num,

如果num小於1,那麼隨機數的範圍是[0,num)

如果num大於1,那麼隨機數的範圍是[1,num)

如果num等於1,那麼產生的隨機數全是1。

拓展2

help顯示low和high還可以是一個浮點數類型的數組。

就探究了一下到底是什麼回事。

temp=np.random.uniform(low=[1.1, 2.1, 3.1, 4.1],high=[1.2, 2.2, 3.2, 4.2])  
print(temp)

'''
[1.17724259 2.19548916 3.16775407 4.12345112]
'''

可以看出第一個數的範圍1.1和1.2之間,第二個數在2.1到2.2之間,…………

不出意外應該也是左閉右開。

在這種情況下,添加size,只能是一維的,並且大小要對應。

在上面的這段代碼中,size=4 和 size=(4,) 和 size=(1,4)是正確的。size=(2,2),size=(4,1)提示錯誤。

 

7. seed基本用法

np.random.seed(),使得隨機數據可預測。

如果在seed()中傳入的數字相同,那麼接下來生成的隨機數序列都是相同的,僅作用於最接近的那句隨機數產生語句。

np.random.seed(10)
temp1=np.random.rand(4)
print(temp1)
np.random.seed(10)
temp2=np.random.rand(4)
print(temp2)

#這句就不一樣的,因爲僅作用於最接近的那句隨機數產生語句
temp3=np.random.rand(4)
print(temp3)

'''
[0.77132064 0.02075195 0.63364823 0.74880388]
[0.77132064 0.02075195 0.63364823 0.74880388]
[0.49850701 0.22479665 0.19806286 0.76053071]
'''

 

暫時就用到這些。之後用到了再更新。

行筆匆忙,歡迎指錯。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章