maxframe.tensor.random.normal#
- maxframe.tensor.random.normal(loc=0.0, scale=1.0, size=None, chunk_size=None, gpu=None, dtype=None)[源代码]#
从正态(高斯)分布中抽取随机样本。
正态分布的概率密度函数最初由 De Moivre 推导出来,200 年后由 Gauss 和 Laplace 独立重新推导 [2],由于其典型的形状常被称为钟形曲线(参见下面的示例)。
正态分布在自然界中经常出现。例如,它描述了受大量微小、随机干扰影响的样本的常见分布,每个干扰都有其独特的分布 [2]。
- 参数:
loc (float or array_like of floats) -- 分布的均值(“中心”)。
scale (float or array_like of floats) -- 分布的标准差(离散度或“宽度”)。
size (int or tuple of ints, optional) -- 输出形状。如果给定形状为,例如
(m, n, k),则会抽取m * n * k个样本。如果 size 为None``(默认),且 ``loc和scale都为标量,则返回单个值。否则,抽取mt.broadcast(loc, scale).size个样本。chunk_size (int or tuple of int or tuple of ints, optional) -- 每个维度上期望的分块大小
gpu (bool, optional) -- 如果为 True,则在 GPU 上分配张量,默认为 False
dtype (data-type, optional) -- 返回张量的数据类型。
- 返回:
out -- 从参数化的正态分布中抽取的样本。
- 返回类型:
Tensor or scalar
参见
scipy.stats.norm概率密度函数、分布或累积密度函数等。
备注
高斯分布的概率密度为
\[p(x) = \frac{1}{\sqrt{ 2 \pi \sigma^2 }} e^{ - \frac{ (x - \mu)^2 } {2 \sigma^2} },\]其中 \(\mu\) 是均值,\(\sigma\) 是标准差。标准差的平方 \(\sigma^2\),称为方差。
该函数在均值处达到峰值,其“离散度”随标准差增加而增大(函数在 \(x + \sigma\) 和 \(x - \sigma\) 处达到最大值的 0.607 倍 [2])。这意味着 numpy.random.normal 更可能返回接近均值的样本,而不是远离均值的样本。
引用
示例
从分布中抽取样本:
>>> import maxframe.tensor as mt
>>> mu, sigma = 0, 0.1 # mean and standard deviation >>> s = mt.random.normal(mu, sigma, 1000)
验证均值和方差:
>>> (abs(mu - mt.mean(s)) < 0.01).execute() True
>>> (abs(sigma - mt.std(s, ddof=1)) < 0.01).execute() True
显示样本的直方图以及概率密度函数:
>>> import matplotlib.pyplot as plt >>> count, bins, ignored = plt.hist(s.execute(), 30, normed=True) >>> plt.plot(bins, (1/(sigma * mt.sqrt(2 * mt.pi)) * ... mt.exp( - (bins - mu)**2 / (2 * sigma**2) )).execute(), ... linewidth=2, color='r') >>> plt.show()