numpy.random.standard_normal#

random.standard_normal(size=None)#

從標準常態分佈(平均值=0,標準差=1)中抽取樣本。

注意

新程式碼應改用 standard_normal 方法,此方法屬於 Generator 實例;請參閱快速入門

參數:
sizeint 或 整數元組,選填

輸出形狀。如果給定的形狀是,例如,(m, n, k),則會抽取 m * n * k 個樣本。預設值為 None,在這種情況下會返回單一值。

回傳值:
outfloat 或 ndarray

形狀為 size 的浮點數陣列,其中包含抽取的樣本;如果未指定 size,則為單一樣本。

另請參閱

normal

具有額外 locscale 參數的等效函數,用於設定平均值和標準差。

random.Generator.standard_normal

新程式碼應使用此方法。

註解

若要從平均值為 mu 且標準差為 sigma 的常態分佈中抽取隨機樣本,請使用以下其中一種

mu + sigma * np.random.standard_normal(size=...)
np.random.normal(mu, sigma, size=...)

範例

>>> np.random.standard_normal()
2.1923875335537315 #random
>>> s = np.random.standard_normal(8000)
>>> s
array([ 0.6888893 ,  0.78096262, -0.89086505, ...,  0.49876311,  # random
       -0.38672696, -0.4685006 ])                                # random
>>> s.shape
(8000,)
>>> s = np.random.standard_normal(size=(3, 4, 2))
>>> s.shape
(3, 4, 2)

從平均值為 3 且標準差為 2.5 的常態分佈中抽取的 2x4 陣列樣本

>>> 3 + 2.5 * np.random.standard_normal(size=(2, 4))
array([[-4.49401501,  4.00950034, -1.81814867,  7.29718677],   # random
       [ 0.39924804,  4.68456316,  4.99394529,  4.84057254]])  # random