python numpy np.random.normal

背景知识

Normal distribution

np.random.normal

%matplotlib inline
import numpy as np
mu, sigma = 0, 0.1
s = np.random.normal(mu, sigma, 1000)
# abs(mu - np.mean(s))
# abs(sigma - np.std(s, ddof=1))

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

在这里插入图片描述

%matplotlib inline
import numpy as np
mu, sigma = 0, 1
s = np.random.normal(mu, sigma, (10000, 2))
# abs(mu - np.mean(s))
# abs(sigma - np.std(s, ddof=1))

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

在这里插入图片描述

import numpy as np
help(np.random.normal)
normal(...) method of numpy.random.mtrand.RandomState instance
    normal(loc=0.0, scale=1.0, size=None)
    Parameters
    ----------
    loc : float or array_like of floats
        Mean ("centre") of the distribution.
    scale : float or array_like of floats
        Standard deviation (spread or "width") of the distribution. Must be
        non-negative.
    size : int or tuple of ints, optional
        Output shape.  If the given shape is, e.g., ``(m, n, k)``, then
        ``m * n * k`` samples are drawn.  If size is ``None`` (default),
        a single value is returned if ``loc`` and ``scale`` are both scalars.
        Otherwise, ``np.broadcast(loc, scale).size`` samples are drawn.
    Returns
    -------
    out : ndarray or scalar
        Drawn samples from the parameterized normal distribution.

在这里插入图片描述

  • loc : 分布关于loc对称,即 μ \mu μ ,是所有随机变量的均值
  • scale : 是标准差, σ \sigma σ
  • size : 输出的分布数据

See Also
--------
scipy.stats.norm : probability density function, distribution or
cumulative density function, etc.
Generator.normal: which should be used for new code.

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 * sigma2) ),
… linewidth=2, color=‘r’)

plt.show()
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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值