我的直方图清楚地显示了两个峰值。但是当用双高斯曲线拟合它时,它只显示一个峰。几乎遵循了 stackoverflow 中显示的每个答案。但未能得到正确的结果。我的老师以前用 Fortran 做过,他有两个高峰。
我在一次试验中使用了 python 的 scipy.optimize
的 leastsq
。我也应该提供我的数据吗?
这是我的代码。
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 分 请帮忙。甚至不能在这里张贴数字。
While plotting on MATLAB, correct results were obtained. Reason: MATLAB uses Trust Region algo instead of Levenberg-Marquardt algo which was not suitable for bound constraints.
The correct results come only when this is shown as a sum of 3 individual Gaussians, not 2.
我如何决定使用哪种算法以及何时使用?
最佳答案
您的问题似乎与高斯混合
也称为高斯混合模型
有关。有几种实现方式。 sklearn
值得考虑。
import numpy as np
from sklearn import mixture
import matplotlib.pyplot as plt
comp0 = np.random.randn(1000) - 5 # samples of the 1st component
comp1 = np.random.randn(1000) + 5 # samples of the 2nd component
x = np.hstack((comp0, comp1)) # merge them
gmm = mixture.GMM(n_components=2) # gmm for two components
gmm.fit(x) # train it!
linspace = np.linspace(-10, 10, 1000)
fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
ax1.hist(x, 100) # draw samples
ax2.plot(linspace, np.exp(gmm.score_samples(linspace)[0]), 'r') # draw GMM
plt.show()
输出是
关于python - 高斯适合 python : Trust Region v/s Levenberg Marquardt 中的直方图数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23758714/