python機器學習庫numpy---7.1、生成隨機數-均勻分布


python機器學習庫numpy---7.1、生成隨機數-均勻分布

一、總結

一句話總結:

均勻分布常用主要四個方法,表示[0, 1)之間均勻分布的rand和random,表示[low, high)之間的uniform,隨機整數randint
a、服從[0, 1)之間的均勻分布:
numpy.random.rand(d0, d1, ..., dn)

b、[0, 1)之間均勻抽樣:
numpy.random.random(size=None)
和rand函數功能一樣,參數不同而已

c、在區間[low, high)中均勻分布:
numpy.random.uniform(low=0.0, high=1.0, size=None)

d、隨機整數:在區間[low, high)中離散均勻抽樣:
numpy.random.randint(low, high=None, size=None, dtype='l')

 

 

 

 

二、生成隨機數-均勻分布

博客對應課程的視頻位置:7.1、生成隨機數-均勻分布-范仁義-讀書編程筆記
https://www.fanrenyi.com/video/38/347

 

7、生成隨機數

(1)、均勻分布

a、服從[0, 1)之間的均勻分布:
numpy.random.rand(d0, d1, ..., dn)

b、[0, 1)之間均勻抽樣:
numpy.random.random(size=None)
和rand函數功能一樣,參數不同而已

c、在區間[low, high)中均勻分布:
numpy.random.uniform(low=0.0, high=1.0, size=None)

d、隨機整數:在區間[low, high)中離散均勻抽樣:
numpy.random.randint(low, high=None, size=None, dtype='l')


python中,random.random( )和random.rand( )功能完全一樣,numpy為什么要留兩個表示0-1均勻分布的函數?

歷史原因,可能是為了使 Matlab 用戶更容易學習 python+numpy 的組合。如果把其中一個函數去掉,所帶來的麻煩遠大於好處,因為有很多現存的代碼分別使用了這兩個函數。

(2)、正態分布

a、標准正態分布(均值為0,標准差為1):
numpy.random.randn(d0, d1, ..., dn)

b、服從μ=loc,σ=scale 的正態分布:
numpy.random.normal(loc=0.0, scale=1.0, size=None)


(3)、隨機種子

RandomState:定義種子類:RandomState是一個種子類,提供了各種種子方法,最常用seed

seed([seed]):定義全局種子:參數為整數或者矩陣

代碼示例:

np.random.seed(1234) #設置隨機種子為1234

(1)、均勻分布

(a)、服從[0, 1)之間的均勻分布:numpy.random.rand(d0, d1, ..., dn)

作用:

產生一個給定形狀的數組(其實應該是ndarray對象或者是一個單值),數組中的值服從[0, 1)之間的均勻分布。

參數:

d0, d, ..., dn : int,可選。如果沒有參數則返回一個float型的隨機數,該隨機數服從[0, 1)之間的均勻分布。

返回值:

ndarray對象或者一個float型的值
In [2]:
import numpy as np print(np.random.rand()) 
0.5453255015277448
In [3]:
print(np.random.rand(3)) 
[0.90757678 0.03145185 0.95313326]
In [4]:
print(np.random.rand(3,4)) 
[[0.97597192 0.28732266 0.17960605 0.85840045]
 [0.8457097  0.11786519 0.59764586 0.5142609 ]
 [0.68330079 0.44224353 0.36096329 0.32893185]]
In [5]:
import matplotlib.pyplot as plt # 導入matplotlib模塊,用於圖表輔助分析 %matplotlib inline # 解決中文亂碼 plt.rcParams["font.sans-serif"]=["SimHei"] plt.rcParams["font.family"]="sans-serif" # 解決負號無法顯示的問題 plt.rcParams['axes.unicode_minus'] =False data_x = np.random.rand(1000) data_y = np.random.rand(1000) plt.scatter(data_x,data_y) 
Out[5]:
<matplotlib.collections.PathCollection at 0x21cf86ec448>
(b)、[0, 1)之間均勻抽樣:numpy.random.random(size=None)

作用:

返回從[0, 1)之間均勻抽樣的數組,size指定形狀。

參數:

size:

int型或int型的元組,如果不提供則返回一個服從該分布的隨機數

返回值:

float型或者float型的ndarray對象
In [6]:
# 無參數
print(np.random.random()) 
0.25733366399724056
In [8]:
print(np.random.random(size=(3,))) 
[0.87588153 0.41083416 0.92811127]
In [9]:
# 元組做參數
print(np.random.random((4,3))) 
[[0.03027158 0.09068682 0.08516664]
 [0.88818931 0.7542361  0.97077598]
 [0.58459742 0.66082839 0.51209488]
 [0.52616617 0.99295998 0.9026425 ]]
In [10]:
import matplotlib.pyplot as plt # 導入matplotlib模塊,用於圖表輔助分析 %matplotlib inline # 解決中文亂碼 plt.rcParams["font.sans-serif"]=["SimHei"] plt.rcParams["font.family"]="sans-serif" # 解決負號無法顯示的問題 plt.rcParams['axes.unicode_minus'] =False data_x = np.random.random((1000,)) data_y = np.random.random((1000,)) plt.scatter(data_x,data_y) 
Out[10]:
<matplotlib.collections.PathCollection at 0x21cf877de88>
(c)、在區間[low, high)中均勻分布:numpy.random.uniform(low=0.0, high=1.0, size=None)

作用:

返回一個在區間[low, high)中均勻分布的數組,size指定形狀。

參數:

low, high:float型或者float型的類數組對象。指定抽樣區間為[low, high),low的默認值為0.0,hign的默認值為1.0

size:

int型或int型元組。指定形狀,如果不提供size,則返回一個服從該分布的隨機數。
In [11]:
print(np.random.uniform()) 
0.8464163023853011
In [12]:
print(np.random.uniform(10,100,(4,5))) 
[[86.86036611 82.50530283 51.3847694  14.54433224 85.78924098]
 [88.45675899 80.67287362 88.57930721 79.2433418  95.33601246]
 [97.38877669 97.10142936 75.48331286 47.14349932 50.931656  ]
 [57.54116023 34.31330871 39.62217741 79.27942515 34.2372894 ]]
In [13]:
import matplotlib.pyplot as plt # 導入matplotlib模塊,用於圖表輔助分析 %matplotlib inline # 解決中文亂碼 plt.rcParams["font.sans-serif"]=["SimHei"] plt.rcParams["font.family"]="sans-serif" # 解決負號無法顯示的問題 plt.rcParams['axes.unicode_minus'] =False data_x = np.random.uniform(10,100,(1000,)) data_y = np.random.uniform(10,100,(1000,)) plt.scatter(data_x,data_y) 
Out[13]:
<matplotlib.collections.PathCollection at 0x21cf8812ac8>
(d)、隨機整數:在區間[low, high)中離散均勻抽樣:numpy.random.randint(low, high=None, size=None, dtype='l')

作用:

返回一個在區間[low, high)中離散均勻抽樣的數組,size指定形狀,dtype指定數據類型。

參數:

low, high:int型,指定抽樣區間[low, high)
size:int型或int型的元組,指定形狀
dypte:可選參數,指定數據類型,比如int,int64等,默認是np.int

返回值:

如果指定了size,則返回一個int型的ndarray對象,否則返回一個服從該分布的int型隨機數。
In [16]:
print(np.random.randint(10)) 
7
In [ ]:
# 只有一個參數的時候表示最大值
print(np.random.randint(10)) 
In [17]:
help(np.random.randint) 
Help on built-in function randint:

randint(...) method of numpy.random.mtrand.RandomState instance
    randint(low, high=None, size=None, dtype='l')
    
    Return random integers from `low` (inclusive) to `high` (exclusive).
    
    Return random integers from the "discrete uniform" distribution of
    the specified dtype in the "half-open" interval [`low`, `high`). If
    `high` is None (the default), then results are from [0, `low`).
    
    .. note::
        New code should use the ``integers`` method of a ``default_rng()``
        instance instead; see `random-quick-start`.
    
    Parameters
    ----------
    low : int or array-like of ints
        Lowest (signed) integers to be drawn from the distribution (unless
        ``high=None``, in which case this parameter is one above the
        *highest* such integer).
    high : int or array-like of ints, optional
        If provided, one above the largest (signed) integer to be drawn
        from the distribution (see above for behavior if ``high=None``).
        If array-like, must contain integer values
    size : int or tuple of ints, optional
        Output shape.  If the given shape is, e.g., ``(m, n, k)``, then
        ``m * n * k`` samples are drawn.  Default is None, in which case a
        single value is returned.
    dtype : dtype, optional
        Desired dtype of the result. All dtypes are determined by their
        name, i.e., 'int64', 'int', etc, so byteorder is not available
        and a specific precision may have different C types depending
        on the platform. The default value is `np.int_`.
    
        .. versionadded:: 1.11.0
    
    Returns
    -------
    out : int or ndarray of ints
        `size`-shaped array of random integers from the appropriate
        distribution, or a single such random int if `size` not provided.
    
    See Also
    --------
    random_integers : similar to `randint`, only for the closed
        interval [`low`, `high`], and 1 is the lowest value if `high` is
        omitted.
    Generator.integers: which should be used for new code.
    
    Examples
    --------
    >>> np.random.randint(2, size=10)
    array([1, 0, 0, 0, 1, 1, 0, 0, 1, 0]) # random
    >>> np.random.randint(1, size=10)
    array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
    
    Generate a 2 x 4 array of ints between 0 and 4, inclusive:
    
    >>> np.random.randint(5, size=(2, 4))
    array([[4, 0, 2, 1], # random
           [3, 2, 2, 0]])
    
    Generate a 1 x 3 array with 3 different upper bounds
    
    >>> np.random.randint(1, [3, 5, 10])
    array([2, 2, 9]) # random
    
    Generate a 1 by 3 array with 3 different lower bounds
    
    >>> np.random.randint([1, 5, 7], 10)
    array([9, 8, 7]) # random
    
    Generate a 2 by 4 array using broadcasting with dtype of uint8
    
    >>> np.random.randint([1, 3, 5, 7], [[10], [20]], dtype=np.uint8)
    array([[ 8,  6,  9,  7], # random
           [ 1, 16,  9, 12]], dtype=uint8)

In [20]:
print(np.random.randint(low=10,size=(3,4),high=100)) 
[[60 19 81 87]
 [49 67 85 46]
 [69 25 92 44]]
In [21]:
import matplotlib.pyplot as plt # 導入matplotlib模塊,用於圖表輔助分析 %matplotlib inline # 解決中文亂碼 plt.rcParams["font.sans-serif"]=["SimHei"] plt.rcParams["font.family"]="sans-serif" # 解決負號無法顯示的問題 plt.rcParams['axes.unicode_minus'] =False data_x = np.random.randint(10,100,(1000,)) data_y = np.random.randint(10,100,(1000,)) plt.scatter(data_x,data_y) 
Out[21]:
<matplotlib.collections.PathCollection at 0x21cf8922348>

(2)、正態分布

(a)、標准正態分布(均值為0,標准差為1):numpy.random.randn(d0, d1, ..., dn)

作用:

返回一個指定形狀的數組,數組中的值服從標准正態分布(均值為0,標准差為1)。

參數:

d0, d, ..., dn : int,可選。如果沒有參數,則返回一個服從標准正態分布的float型隨機數。

返回值:

ndarray對象或者float
In [5]:
# 無參數
import numpy as np print(np.random.randn()) 
0.13376456884878418
In [6]:
print(np.random.randn(3)) 
[ 0.00692413  1.7347139  -0.98832561]
In [7]:
# 3行4列 的正態分布
print(np.random.randn(3,4)) # 正態分布,所以我們可以看到結果有正有負 
[[ 0.46288376 -0.9402719  -1.3791829   0.12095743]
 [ 1.47022746 -0.77955805 -0.38801507  0.52498791]
 [ 0.12182416  1.74959633  1.20357299 -1.10387593]]
In [8]:
import matplotlib.pyplot as plt # 導入matplotlib模塊,用於圖表輔助分析 %matplotlib inline # 解決中文亂碼 plt.rcParams["font.sans-serif"]=["SimHei"] plt.rcParams["font.family"]="sans-serif" # 解決負號無法顯示的問題 plt.rcParams['axes.unicode_minus'] =False data_x = np.random.randn(1000) data_y = np.random.randn(1000) plt.scatter(data_x,data_y) 
Out[8]:
<matplotlib.collections.PathCollection at 0x264250a59c8>
(b)、服從μ=loc,σ=scale 的正態分布:numpy.random.normal(loc=0.0, scale=1.0, size=None)

作用:

返回一個由size指定形狀的數組,數組中的值服從 μ=loc,σ=scale 的正態分布。

參數:

loc : float型或者float型的類數組對象,指定均值 μ
scale : float型或者float型的類數組對象,指定標准差 σ
size : int型或者int型的元組,指定了數組的形狀。如果不提供size,且loc和scale為標量(不是類數組對象),則返回一個服從該分布的隨機數。

輸出:

ndarray對象或者一個標量
In [9]:
# 無參數
print(np.random.normal()) 
1.3048521009886291
In [10]:
# 3行4列 的正態分布
print(np.random.normal(10,2,(3,4))) 
[[ 6.64607268 14.28870815 11.04433027 12.77813207]
 [ 8.38923977 11.63867271 11.28538438 12.20541604]
 [10.08465579  8.88528815 12.41447794  7.58024792]]
In [11]:
import matplotlib.pyplot as plt # 導入matplotlib模塊,用於圖表輔助分析 %matplotlib inline # 解決中文亂碼 plt.rcParams["font.sans-serif"]=["SimHei"] plt.rcParams["font.family"]="sans-serif" # 解決負號無法顯示的問題 plt.rcParams['axes.unicode_minus'] =False data_x = np.random.normal(10,2,(1000)) data_y = np.random.normal(10,2,(1000)) plt.scatter(data_x,data_y) 
Out[11]:
<matplotlib.collections.PathCollection at 0x26425174688>

(3)、隨機種子

計算機實現的隨機數生成通常為偽隨機數生成器,為了使得具備隨機性的代碼最終的結果可復現,需要設置相同的種子值


電腦產生隨機數需要明白以下幾點:

(1)隨機數是由隨機種子根據一定的計算方法計算出來的數值。所以,只要計算方法一定,隨機種子一定,那么產生的隨機數就不會變。 
(2)只要用戶不設置隨機種子,那么在默認情況下隨機種子來自系統時鍾(即定時/計數器的值) 
(3)隨機數產生的算法與系統有關,Windows和Linux是不同的,也就是說,即便是隨機種子一樣,不同系統產生的隨機數也不一樣。 



隨機種子方法

RandomState:定義種子類:RandomState是一個種子類,提供了各種種子方法,最常用seed

seed([seed]):定義全局種子:參數為整數或者矩陣

代碼示例:

np.random.seed(1234) #設置隨機種子為1234

In [3]:
#1為種子,種子不一樣,生成的隨機數也不一樣,但是對每個種子來說,每次運行所生成的隨機數是相同的
import numpy as np rs = np.random.RandomState(1) data = rs.rand(30) print(data) 
[4.17022005e-01 7.20324493e-01 1.14374817e-04 3.02332573e-01
 1.46755891e-01 9.23385948e-02 1.86260211e-01 3.45560727e-01
 3.96767474e-01 5.38816734e-01 4.19194514e-01 6.85219500e-01
 2.04452250e-01 8.78117436e-01 2.73875932e-02 6.70467510e-01
 4.17304802e-01 5.58689828e-01 1.40386939e-01 1.98101489e-01
 8.00744569e-01 9.68261576e-01 3.13424178e-01 6.92322616e-01
 8.76389152e-01 8.94606664e-01 8.50442114e-02 3.90547832e-02
 1.69830420e-01 8.78142503e-01]
In [3]:
# 如果沒設置隨機數
import numpy as np print(np.random.rand(30)) 
[0.91197644 0.6504751  0.54849346 0.1408817  0.79662447 0.7657173
 0.88059378 0.28970806 0.04421655 0.8258623  0.77949267 0.21584071
 0.54239107 0.84065671 0.98545231 0.93971736 0.94389124 0.4268576
 0.24076141 0.79165343 0.35609388 0.29818106 0.0966155  0.27966646
 0.11785404 0.485944   0.89728192 0.25347008 0.81296457 0.63926957]
In [4]:
import numpy as np np.random.seed(1) #設置隨機種子為1 print(np.random.rand(30)) 
[4.17022005e-01 7.20324493e-01 1.14374817e-04 3.02332573e-01
 1.46755891e-01 9.23385948e-02 1.86260211e-01 3.45560727e-01
 3.96767474e-01 5.38816734e-01 4.19194514e-01 6.85219500e-01
 2.04452250e-01 8.78117436e-01 2.73875932e-02 6.70467510e-01
 4.17304802e-01 5.58689828e-01 1.40386939e-01 1.98101489e-01
 8.00744569e-01 9.68261576e-01 3.13424178e-01 6.92322616e-01
 8.76389152e-01 8.94606664e-01 8.50442114e-02 3.90547832e-02
 1.69830420e-01 8.78142503e-01]
In [ ]:
 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM