numpy.random使用

numpy.random.normal (loc = 0.0, = 1.0, = None)大小¶
从正态(高斯)分布中抽取随机样本。

正态分布的概率密度函数,最初由德莫夫尔推导,200年后由高斯和拉普拉斯分别推导,由于其特征形状,常被称为钟形曲线(见下例)。

自然界中经常出现正态分布。例如,它描述了受大量微小、随机扰动影响的常见样本分布,每个扰动都有其独特的分布[2]。

请注意
新代码应该使用default_rng()实例的常规方法;看到random-quick-start。

参数
浮点数的locfloat或array_like
分布的平均值(“中心”)。

浮点数的scalefloat或array_like
分布的标准差(散布或“宽度”)。必须是非负数。

int的大小或元组,可选
输出的形状。如给定形状为(m, n, k),则绘制m * n * k个样本。如果size为None(默认),如果loc和scale都是标量,则返回单个值。否则,np。广播(loc、规模)。抽取大小样本。

返回
outndarray或标量
从参数化正态分布中抽取样本。

另请参阅
scipy.stats.norm
概率密度函数、分布或累积密度函数等。

Generator.normal
应该用于新代码。

笔记

高斯分布的概率密度是

p(x) = \frac bbb {\sqrt{2 \pi \sigma^2}
e^{- \frac{(x - \mu)^2} {2 \sigma^2},

是均值,是标准差。标准差的平方,\ ^2,称为方差。

函数在平均值处有其峰值,其“扩展”随标准差增加(函数在x + \sigma和x - \sigma[2]处达到其最大值的0.607倍)。这意味着正态分布更可能返回接近均值的样本,而不是远离均值的样本。

 

References

1
Wikipedia, “Normal distribution”, https://en.wikipedia.org/wiki/Normal_distribution

2(1,2,3)
P. R. Peebles Jr., “Central Limit Theorem” in “Probability, Random Variables and Random Signal Principles”, 4th ed., 2001, pp. 51, 51, 125.

Examples

Draw samples from the distribution:

>>>
mu, sigma = 0, 0.1 # mean and standard deviation
s = np.random.normal(mu, sigma, 1000)
Verify the mean and the variance:

>>>
abs(mu - np.mean(s))
0.0  # may vary
>>>
abs(sigma - np.std(s, ddof=1))
0.1  # may vary
Display the histogram of the samples, along with the probability density function:

>>>
import matplotlib.pyplot as plt
count, bins, ignored = plt.hist(s, 30, density=True)
plt.plot(bins, 1/(sigma * np.sqrt(2 * np.pi)) *
               np.exp( - (bins - mu)**2 / (2 * sigma**2) ),
         linewidth=2, color='r')
plt.show()
../../../_images/numpy-random-normal-1_00_00.png
Two-by-four array of samples from N(3, 6.25):

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值