【问题标题】:Scaling of fitted pdf for a histogram直方图拟合 pdf 的缩放
【发布时间】:2018-05-19 00:30:15
【问题描述】:

我正在处理一些时间序列数据,并尝试使用伽马分布拟合数据。但问题是拟合的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()

【问题讨论】:

    标签: python pdf scipy histogram data-fitting


    【解决方案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)
    

    【讨论】:

      猜你喜欢
      • 2016-04-25
      • 2015-06-16
      • 1970-01-01
      • 1970-01-01
      • 2014-06-20
      • 2017-08-31
      • 1970-01-01
      • 2016-02-22
      相关资源
      最近更新 更多