numpy.random.randn

原文:https://docs.scipy.org/doc/numpy/reference/generated/numpy.random.randn.html

译者:飞龙 UsyiyiCN

校对:(虚位以待)

numpy.random.randn(d0, d1, ..., dn)

从“标准正态”分布返回样本(或样本)。

If positive, int_like or int-convertible arguments are provided, randn generates an array of shape (d0, d1, ..., dn), filled with random floats sampled from a univariate “normal” (Gaussian) distribution of mean 0 and variance 1 (if any of the d_i are floats, they are first converted to integers by truncation). 如果没有提供参数,则返回从分布中随机抽取的单个浮点数。

这是一个方便的功能。如果你想要一个以元组作为第一个参数的接口,请改用numpy.random.standard_normal

参数:

d0,d1,...,dn:int,可选

返回的数组的尺寸应该都是正数。如果没有给出参数,则返回单个Python浮动。

返回:

Z:ndarray或float

A (d0, d1, ..., dn)的来自标准正态分布的浮点样本,或者如果没有提供参数,则使用单个这样的浮点。

也可以看看

random.standard_normal
类似,但是以元组为参数。

笔记

对于N(\mu, \sigma^2)的随机样本,使用:

sigma * np.random.randn(...) + t5>

例子

>>> np.random.randn()
2.1923875335537315 #random

来自N(3,6.25)的2×4数组样本:

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