缩放安装的pdf以获得直方图

时间:2018-05-19 00:30:15

标签: python pdf scipy histogram data-fitting

我正在编写一些时间序列数据,并尝试使用gamma分布拟合数据。但问题是拟合pdf的幅度远低于直方图的幅度。这是我的代码和情节。情节有什么问题?

# the data
data = contents[0][1:]

# normalized histogram
weights = np.ones_like(data)/len(data)
plt.hist(data, bins = 20, color = 'w', edgecolor = 'black', alpha = 0.5, weights = weights)

# fit with gamma distribution and plot the pdf
dist = getattr(scipy.stats, 'gamma')
param = dist.fit(data)
x = np.linspace(min(data), max(data), 100)
pdf_fit = dist.pdf(x, *param[:-2], loc = param[-2], scale = param[-1])
plt.plot(x, pdf_fit/sum(pdf_fit), label = 'Gamma')
plt.legend(loc = 'upper right')
plt.show()

enter image description here

1 个答案:

答案 0 :(得分:1)

在致电plt.hist()时,请使用参数weights=np.ones_like(data)/len(data)

,而不是使用normed=True
plt.hist(data, bins = 20, color = 'w', edgecolor = 'black', alpha = 0.5, normed = True)