Python np.random.normal的非随机抽样版本

Python np.random.normal的非随机抽样版本,python,python-2.7,numpy,random,gaussian,Python,Python 2.7,Numpy,Random,Gaussian,我试图生成一个遵循精确高斯分布的数组。np.random.normal排序是通过从高斯分布中随机抽样来实现的,但是在给定一些平均值和sigma的情况下,我如何复制和精确高斯分布呢。所以这个数组会产生一个直方图,它遵循一个精确的高斯分布,而不仅仅是一个近似的高斯分布,如下所示 mu, sigma = 10, 1 s = np.random.normal(mu, sigma, 1000) fig = figure() ax = plt.axes() totaln, bbins, patches

我试图生成一个遵循精确高斯分布的数组。np.random.normal排序是通过从高斯分布中随机抽样来实现的,但是在给定一些平均值和sigma的情况下,我如何复制和精确高斯分布呢。所以这个数组会产生一个直方图,它遵循一个精确的高斯分布,而不仅仅是一个近似的高斯分布,如下所示

mu, sigma = 10, 1
s = np.random.normal(mu, sigma, 1000)

fig = figure()
ax = plt.axes()

totaln, bbins, patches = ax.hist(s, 10, normed = 1, histtype = 'stepfilled', linewidth = 1.2)

plt.show()

如果你想要一个精确的高斯直方图,不要生成点。你永远无法从观测点得到一个“精确的”高斯分布,仅仅因为你不能在一个柱状图中得到一个点的一小部分

相反,以条形图的形式绘制曲线

import numpy as np
import matplotlib.pyplot as plt

def gaussian(x, mean, std):
    scale = 1.0 / (std * np.sqrt(2 * np.pi))
    return scale * np.exp(-(x - mean)**2 / (2 * std**2))

mean, std = 2.0, 5.0
nbins = 30
npoints = 1000

x = np.linspace(mean - 3 * std, mean + 3 * std, nbins + 1)
centers = np.vstack([x[:-1], x[1:]]).mean(axis=0)
y = npoints * gaussian(centers, mean, std)

fig, ax = plt.subplots()
ax.bar(x[:-1], y, width=np.diff(x), color='lightblue')

# Optional...
ax.margins(0.05)
ax.set_ylim(bottom=0)

plt.show()