【发布时间】:2016-02-22 00:51:48
【问题描述】:
我一直在冲浪,但没有找到正确的方法来执行以下操作。
我有一个使用 matplotlib 完成的直方图:
hist, bins, patches = plt.hist(distance, bins=100, normed='True')
从图中,我可以看到分布或多或少是指数分布(泊松分布)。考虑到我的 hist 和 bins 数组,我怎样才能做到最佳拟合?
更新
我正在使用以下方法:
x = np.float64(bins) # Had some troubles with data types float128 and float64
hist = np.float64(hist)
myexp=lambda x,l,A:A*np.exp(-l*x)
popt,pcov=opt.curve_fit(myexp,(x[1:]+x[:-1])/2,hist)
但我明白了
---> 41 plt.plot(stats.expon.pdf(np.arange(len(hist)),popt),'-')
ValueError: operands could not be broadcast together with shapes (100,) (2,)
【问题讨论】:
-
我不确定您为什么要使用直方图。所有常见的分布都可以通过几个形状/位置参数来固定。这些通常可以从数据本身非常有效地估计出来。
-
我回答了一个类似的问题yesterday,你甚至可以在那里找到如何使用你自己的拟合模型。您应该将
x=(bins[1:]+bins[:-1])/2; y=hist作为拟合程序的输入。 @cel:对于噪声数据,最小二乘拟合比分布矩的原始估计要可靠得多。(citation needed) -
我不太明白你在那里给出了什么。我正在尝试使用
param=spy.expon.fit(distance); a = np.linspace(0,bins[-1],1000, dtype='f'); pdf_fitted=spy.expon.pdf(a,loc=param[0],scale=param[1]);plt.plot(a,pdf_fitted,'r-') -
不要
plt.plot(stats.expon.pdf(np.arange(len(hist)),popt),'-'),而是plt.plot((x[1:]+x[:-1])/2,myexp((x[1:]+x[:-1])/2,*popt),'-')(或者你喜欢的任何x数组)。
标签: python pandas matplotlib scipy data-analysis