numpy.random模組常用函數解析

來源:互聯網
上載者:User

標籤:nbsp   integer   ref   div   sam   return   target   樣本   hal   

numpy.random模組中常用函數解析

numpy.random模組官方文檔


1. numpy.random.rand(d0, d1, ..., dn)
Create an array of the given shape and populate it with random samples from a uniform distribution over [0, 1)
按照給定形狀產生一個多維陣列,每個元素在0到1之間
注意: 這裡定義數組形狀時,不能採用tuple

 import numpy as np np.random.rand(2, 3)
 array([[ 0.44590044,  0.36234046,  0.51609462],        [ 0.45733218,  0.80836224,  0.31628453]])

2. numpy.random.randn(d0, d1, ..., dn)
generates an array of shape (d0, d1, ..., dn), filled with random floats sampled from a univariate “normal” distribution of mean 0 and variance 1
按照給定形狀產生一個多維陣列,數組中的元素服從標準常態分佈

若要產生服從N(mu, sigma^2)分布的樣本, 使用sigma * np.random.randn(...) + mu

例如產生 2 * 4 samples from N(3, 6.25):

2.5 * np.random.randn(2, 4) + 3
array([[ 2.90478558,  6.05670578,  6.21539068,  3.3955507 ],       [ 0.11594363,  3.17433693,  5.35625762,  1.4824643 ]])

3. numpy.random.randint(low, high=None, size=None, dtype=‘l‘)
Return random integers from low (inclusive) to high (exclusive).

按照給定的形狀和範圍產生隨機整數

np.random.randint(0, 10, size=(2, 4))
array([[2, 7, 2, 1],       [3, 2, 4, 1]])
4. numpy.random.random_integers(low, high=None, size=None)

Random integers of type np.int between low and high, inclusive.

np.random.random_integers(1, 10, size=(2, 5))
array([[ 3,  3,  8,  4,  5],       [ 2,  7,  8, 10,  2]])

5. numpy.random.random_sample(size=None)
6. numpy.random.random(size=None)
7. numpy.random.ranf(size=None)
8. numpy.random.sample(size=None)
Return random floats in the half-open interval [0.0, 1.0).

以上四種方式都是產生[0,1)之間的浮點數

To sample Unif[a, b), b > a multiply the output of random_sample by (b-a) and add a:

(b - a) * random_sample() + a

1 import numpy as np2 print(‘random_sample:\n‘, np.random.random_sample((2, 3)))3 print(‘random:\n‘, np.random.random((2, 3)))4 print(‘ranf:\n‘, np.random.ranf((2, 3)))5 print(‘sample:\n‘, np.random.sample((2, 3)))
 1 random_sample: 2  [[ 0.87996593  0.2706701   0.42158973] 3  [ 0.91952234  0.99470239  0.07363656]] 4 random: 5  [[ 0.44572326  0.23595379  0.1061901 ] 6  [ 0.48362249  0.4270327   0.12281262]] 7 ranf: 8  [[ 0.07180002  0.25542854  0.55630057] 9  [ 0.38181471  0.91512916  0.04020929]]10 sample:11  [[ 0.80390231  0.0024602   0.95974309]12  [ 0.32902852  0.62796713  0.42254831]]

9. numpy.random.choice(a, size = None, replace=True, p=None)
從給定的一維數組中產生隨機數

如a是一個int數, 則產生的數組的元素都在np.arange(a)中

如a是一個1-D array-like, 則產生的數組的元素都在a中

1 print(‘1:\n‘, np.random.choice(5))2 print(‘2:\n‘, np.random.choice(5, 2, p=[0.1, 0.4, 0.3, 0.1, 0.1]))3 print(‘3:\n‘, np.random.choice(5, (2, 3)))4 print(‘4:\n‘, np.random.choice([1, 3, 4, 6], (2, 5), p=[0.1, 0.3, 0.1, 0.5]))
 1 1: 2  4 3 2: 4  [1 4] 5 3: 6  [[2 1 4] 7  [0 2 3]] 8 4: 9  [[3 6 1 6 1]10  [3 3 3 3 1]]
10. numpy.random.seed(None)

設定相同的seed,每次產生的隨機數相同。如果不設定seed,則每次會產生不同的隨機數

1 np.random.seed(2)2 np.random.rand(2, 3)
1 array([[ 0.4359949 ,  0.02592623,  0.54966248],2        [ 0.43532239,  0.4203678 ,  0.33033482]])
1 np.random.seed(2)2 np.random.rand(2, 3)
1 array([[ 0.4359949 ,  0.02592623,  0.54966248],2        [ 0.43532239,  0.4203678 ,  0.33033482]])
1 np.random.rand(2, 3)
1 array([[ 0.20464863,  0.61927097,  0.29965467],2        [ 0.26682728,  0.62113383,  0.52914209]])

 

numpy.random模組常用函數解析

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.