我试图生成一个数组,它遵循精确的高斯分布。np.random.normal是通过从高斯随机抽样来做到这一点的,但是我如何在给定均值和西格玛的情况下再现和精确的高斯。因此,数组将产生一个直方图,它遵循的是精确的高斯,而不仅仅是下面所示的近似高斯。
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()发布于 2016-01-20 17:33:16
如果你想要一个精确的高斯直方图,不要生成点。你永远不可能从观察到的点中得到“精确”的高斯分布,仅仅因为你不能在直方图中得到一个点的分数。
相反,以条形图的形式绘制曲线。
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()

https://stackoverflow.com/questions/34906438
复制相似问题