numpy.random.normal

random.normal(loc=0.0, scale=1.0, size=None)

从正态(高斯)分布中随机抽取样本。

正态分布的概率密度函数,首先由de moivre推导,200年后由高斯和拉普拉斯独立推导。 [2], 由于其特征形状,通常称为钟形曲线(见下例)。

正态分布在自然界中经常发生。例如,它描述了受大量微小的随机干扰影响的样品的常见分布,每个干扰都有其独特的分布。 [2].

注解

新代码应该使用 normal A方法 default_rng() 请参阅 快速启动 .

参数
loc浮点数或类似浮点数的数组

分布的平均值(“中心”)。

scale浮点数或类似浮点数的数组

分布的标准偏差(分布或“宽度”)。必须为非负。

sizeint或int的元组,可选

输出形状。如果给定的形状是,例如, (m, n, k) 然后 m * n * k 取样。如果尺寸是 None (默认),如果 locscale 都是标量。否则, np.broadcast(loc, scale).size 取样。

返回
outndarray或scalar

从参数化正态分布中提取样本。

参见

scipy.stats.norm

概率密度函数、分布或累积密度函数等。

Generator.normal

应该用于新代码。

笔记

高斯分布的概率密度是

System Message: WARNING/2 (p(x)=\frac{1}{\sqrt{2\pi\sigma^2}})

latex exited with error [stdout] This is pdfTeX, Version 3.14159265-2.6-1.40.19 (TeX Live 2019/dev/Debian) (preloaded format=latex) restricted \write18 enabled. entering extended mode (./math.tex LaTeX2e <2018-12-01> (/usr/share/texlive/texmf-dist/tex/latex/base/article.cls Document Class: article 2018/09/03 v1.4i Standard LaTeX document class (/usr/share/texlive/texmf-dist/tex/latex/base/size12.clo)) (/usr/share/texlive/texmf-dist/tex/latex/base/inputenc.sty) (/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsmath.sty For additional information on amsmath, use the `?' option. (/usr/share/texlive/texmf-dist/tex/latex/amsmath/amstext.sty (/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsgen.sty)) (/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsbsy.sty) (/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsopn.sty)) (/usr/share/texlive/texmf-dist/tex/latex/amscls/amsthm.sty) (/usr/share/texlive/texmf-dist/tex/latex/amsfonts/amssymb.sty (/usr/share/texlive/texmf-dist/tex/latex/amsfonts/amsfonts.sty)) (/usr/share/texlive/texmf-dist/tex/latex/anyfontsize/anyfontsize.sty) (/usr/share/texlive/texmf-dist/tex/latex/tools/bm.sty) (./math.aux) (/usr/share/texlive/texmf-dist/tex/latex/amsfonts/umsa.fd) (/usr/share/texlive/texmf-dist/tex/latex/amsfonts/umsb.fd) ! Package inputenc Error: Unicode character ( (U+FF08) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ...�=\frac{1}{\sqrt{2\pi\sigma^2}}\end{split} ! Package inputenc Error: Unicode character ) (U+FF09) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ...�=\frac{1}{\sqrt{2\pi\sigma^2}}\end{split} ! Package inputenc Error: Unicode character ( (U+FF08) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ...�=\frac{1}{\sqrt{2\pi\sigma^2}}\end{split} ! Package inputenc Error: Unicode character ) (U+FF09) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ...�=\frac{1}{\sqrt{2\pi\sigma^2}}\end{split} [1] (./math.aux) ) (see the transcript file for additional information) Output written on math.dvi (1 page, 460 bytes). Transcript written on math.log.

在哪里? \mu 是均值和 \sigma 标准偏差。标准差的平方, \sigma^2 ,称为方差。

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

工具书类

1

维基百科,“正态分布”,https://en.wikipedia.org/wiki/normal_distribution

2(1,2,3)

P.R.Peebles Jr.,“概率、随机变量和随机信号原理”中的“中心极限定理”,第4版,2001年,第51、51、125页。

实例

从分发中抽取样本:

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

验证平均值和方差:

>>> abs(mu - np.mean(s))
0.0  # may vary
>>> abs(sigma - np.std(s, ddof=1))
0.1  # may vary

显示样本的直方图,以及概率密度函数:

>>> 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

n(3,6.25)中的2×4个样本阵列:

>>> 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