本文介绍了高斯拟合到python中的直方图数据:Trust Region v / s Levenberg Marquardt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的直方图清楚地显示了两个峰。但是,当用双高斯曲线拟合它时,它仅显示一个峰值。遵循stackoverflow中显示的几乎所有答案。但未能获得正确的结果。以前,这是由我在Fortran中的老师完成的,他遇到了两个高峰。
在一项试验中,我使用了python scipy.optimize 中的 leastsq 。我也应该提供数据吗?
这是我的代码。

My histogram plot clearly shows two peaks. But while curve-fitting it with a double gaussian, it shows just one peak. Followed almost every answer shown in stackoverflow. But failed to get the correct result. It has previously been done by my teacher in Fortran and he got two peaks.I used leastsq of python's scipy.optimize in one trial. Should I give my data also?Here is my code.

binss = (max(x) - min(x))/0.05 #0.05 is my bin width
n, bins, patches = plt.hist(x, binss, color = 'grey') #gives the histogram

x_a = []
for item in range(len(bins)-1):
    b = (bins[item]+bins[item+1])/2
    x_a.append(b)

x_avg = np.array(x_a)
y_real = n

def gauss(x, A, mu, sigma):
    gaus = []
    for item in range(len(x)):
        gaus.append(A*e**(-(x[item]-mu)**2./(2.*sigma**2)))
    return np.array(gaus)
A1, A2, m1, m2, sd1, sd2 = [25, 30, 0.3, 0.6, -0.9, -0.9]

#Initial guesses for leastsq
p = [A1, A2, m1, m2, sd1, sd2]
y_init = gauss(x_avg, A1, m1, sd1) + gauss(x_avg, A2, m2, sd2)    #initially guessed y

def residual(p, x, y):
    A1, A2, m1, m2, sd1, sd2 = p
    y_fit = gauss(x, A1, m1, sd1) + gauss(x, A2, m2, sd2)
    err = y - y_fit
    return err

sf = leastsq(residual, p, args = (x_avg , y_real))

y_fitted1 = gauss(x_avg, sf[0][0], sf[0][2], sf[0][4])
y_fitted2 = gauss(x_avg, sf[0][1], sf[0][3], sf[0][5])

y_fitted = y_fitted1 + y_fitted2

plt.plot(x_avg, y_init, 'b', label='Starting Guess')
plt.plot(x_avg, y_fitted, color = 'red', label = 'Fitted Data')
plt.plot(x_avg, y_fitted1, color= 'black', label = 'Fitted1 Data')
plt.plot(x_avg, y_fitted2, color = 'green', label = 'Fitted2 Data')

即使我得到的数字也不是很平滑。 x_avg 中只有54分,请提供帮助。

Even the figure I got is not smooth. It's got only 54 points in x_avg Please do help. Can't even post the figure here.

仅当这显示为3个
高斯人的总和,而不是2。

The correct results come only when this is shown as a sum of 3 individual Gaussians, not 2.

我如何确定要使用哪种算法

How do I get to decide which algo to use and when?

推荐答案

我添加了另一个高斯术语。因此 p 总共使用了9个参数。因此,

I added another gaussian term. so p took 9 parameters in total. Thus

p = [A1, A2, A3, m1, m2, m3, sd1, sd2, sd3]

然后定义了另一个术语 y_fitted3 并将其添加到 y_fitted 。然后,它给出了两个峰完全吻合的正确图形,只是曲线根本不平滑!然后在stackoverflow中搜索导致我使用 spline 。即

Then another term y_fitted3 was defined and added to y_fitted. It then gave a correct figure of two peaks fitting perfectly except for the fact that the curve was not smooth at all! Then searching in stackoverflow led me to use spline. i.e.

from scipy.interpolate import spline

,然后最后,

x_new = np.linspace(x_avg.min(),x_avg.max(),30000)
ysmooth = spline(x_avg, y_fitted, x_new)
plt.plot(x_new, ysmooth)

然后就在那里。
在Wikipedia中检查,它说python中的 L-M 也使用 T-R 。因此,再次尝试 leastsq 会得到结果。
但是,我仍然不清楚 MATLAB 中显示的区别。额外的投入将不胜感激!谢谢。

Then there it was.Checking in wikipedia, it says that L-M in python also uses T-R. So trying again leastsq gave the result.But still, I am not clear about the difference shown in MATLAB. Extra inputs will be appreciated! Thank you.

这篇关于高斯拟合到python中的直方图数据:Trust Region v / s Levenberg Marquardt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 16:32