2017-10-21 44 views
1

我相信我成功实现了使用来自scipy的曲线拟合的高斯拟合。但是我遇到的问题是...适合性不是很好,因为优化的参数正在改变质心。使用*** curve_fit ***从scipy和python 3.x改进高斯拟合

data =np.loadtxt('mock.txt') 
    my_x=data[:,0] 
    my_y=data[:,1] 

    def gauss(x,mu,sigma,A): 
     return A*np.exp(-(x-mu)**2/2/sigma**2) 
    def trimodal_gauss(x,mu1,sigma1,A1,mu2,sigma2,A2,mu3,sigma3,A3): 
     return gauss(x,mu1,sigma1,A1)+gauss(x,mu2,sigma2,A2)+gauss(x,mu3,sigma3,A3) 



    """"" 
    Gaussian fitting parameters recognized in each file 
    """"" 
    first_centroid=(10180.4*2+9)/9 
    second_centroid=(10180.4*2+(58.6934*1)+7)/9 
    third_centroid=(10180.4*2+(58.6934*2)+5)/9 
    centroid=[] 
    centroid+=(first_centroid,second_centroid,third_centroid) 

    apparent_resolving_power=1200 
    sigma=[] 
    for i in range(len(centroid)): 
     sigma.append(centroid[i]/((apparent_resolving_power)*2.355)) 

    height=[1,1,1] 

    p=[]  

    p = np.array([list(t) for t in zip(centroid, sigma, height)]).flatten() 


    popt, pcov = curve_fit(trimodal_gauss,my_x,my_y,p0=p) 

输出:enter image description here

据我所知,在这里有很多山峰,但我真的需要它,以适应只有三个高斯,但在正确的质心(在我最初的猜测给出)。换句话说,我真的不希望我给出的质心不变。有没有人遇到过这样的挑战?并且可以请我帮助我做些什么来实现目标?

+0

一般情况下,似乎你最好配合正确的号码(至少5个,也许6个),并且只采用你实际关心的三个结果。你目前的方法会做不好的工作,因为你不适合的高峰会影响你关心的三个高峰的结果。它会“认为”额外的东西是三个峰值的一部分。 – NichtJens

回答

0

如果使用lmfit模块(https://github.com/lmfit/lmfit-py),你可以很容易地把边界上的高斯函数的重心,甚至解决这些问题。 Lmfit还可以轻松建立多峰模型。

你没有给一个完整的例子或链接到你的数据,但lmfit到您的数据的拟合可能是这样的:

import numpy as np 
from lmfit import GaussianModel 
data =np.loadtxt('mock.txt') 
my_x=data[:,0] 
my_y=data[:,1] 

model = (GaussianModel(prefix='p1_') + 
      GaussianModel(prefix='p2_') + 
      GaussianModel(prefix='p3_')) 

params = model.make_params(p1_amplitude=100, p1_sigma=2, p1_center=2262, 
          p2_amplitude=100, p2_sigma=2, p2_center=2269, 
          p3_amplitude=100, p3_sigma=2, p3_center=2276, 
         ) 

# set boundaries on the Gaussian Centers: 
params['p1_center'].min = 2260 
params['p1_center'].max = 2264 

params['p2_center'].min = 2267 
params['p2_center'].max = 2273 

params['p3_center'].min = 2274 
params['p3_center'].max = 2279 

# or you could just fix one of the centroids like this: 
params['p3_center'].vary = False 

# if needed, you could force all the sigmas to be the same value 
# or related by simple mathematical expressions 
params['p2_sigma'].expr = 'p1_sigma' 
params['p3_sigma'].expr = '2*p1_sigma' 

# fit this model to data: 
result = model.fit(my_y, params, x=my_x) 

# print results 
print(result.fit_report()) 

# evaluate individual gaussian components: 
peaks = model.eval_components(params=result.params, x=my_x) 

# plot results: 
plt.plot(my_x, my_y, label='data') 
plt.plot(my_x, result.best_fit, label='best fit') 
plt.plot(my_x, peaks['p1_']) 
plt.plot(my_x, peaks['p2_']) 
plt.plot(my_x, peaks['p3_']) 
plt.show() 
+0

'scipy.curve_fit'知道当前版本的界限。看到这里:https://stackoverflow.com/questions/16760788/python-curve-fit-library-that-allows-me-to-assign-bounds-to-parameters – NichtJens

0

您应该为中心定义三个固定值的单独函数。然后你只适合剩余参数的这些函数的求和函数。

简而言之,您的trimodal_gauss()不应采取mu s,但只有A s和sigma s。 mu应该是常数。

这样做的一个微不足道的(但不是很普遍)的方法是:

def trimodal_gauss(x, sigma1, A1, sigma2, A2, sigma3, A3): 
    mu1 = 1234 # put your mu's here 
    mu2 = 2345 
    mu3 = 3456 
    g1 = gauss(x, mu1, sigma1, A1) 
    g2 = gauss(x, mu2, sigma2, A2) 
    g3 = gauss(x, mu3, sigma3, A3) 
    return g1 + g2 + g3 

从这个人们可以通过一个“发电机”为trimodal_gauss函数需要三个概括的想法mu(或n?) s并创建其他参数的功能。像这样:

def make_trimodal_gauss(mu1, mu2, mu3): 
    def result_function(x, sigma1, A1, sigma2, A2, sigma3, A3): 
     g1 = gauss(x, mu1, sigma1, A1) 
     g2 = gauss(x, mu2, sigma2, A2) 
     g3 = gauss(x, mu3, sigma3, A3) 
     return g1 + g2 + g3 
    return result_function 


mu1 = 1234 # put your mu's here 
mu2 = 2345 
mu3 = 3456 

trimodal_gauss = make_trimodal_gauss(mu1, mu2, mu3) 

#usage like this: trimodal_gauss(x, sigma1, A1, sigma2, A2, sigma3, A3) 
+0

感谢您的评论。如果mu不是函数的一部分,我们如何确保高斯函数适合于感兴趣的质心? – user7852656

+0

我添加了一个方法来做到我的答案。 – NichtJens