2017-08-24 53 views
0

我正在使用python lmfit模块来拟合多个高斯。我想要的是将一个参数加到另一个谷值数学表达式中,例如:将拟合参数互相配合

def gaussian(x,a1,c1,w1,a2,w2,c2): 
     g1=a1*np.exp(-(x-c1)**2/(2*w1**2)) 
     g2=a2*np.exp(-(x-c2)**2/(2*w2**2)) 
     return g1+g2 

gmodel=Model(gaussian) 
result=gmodel.fit(y=y,x=x,params...) 

我想要的是绑定参数,那a1 = a2/2。有没有办法与lmfit包?

回答

1

是的,使用lmfit您可以使用数学表达式来控制任何参数的值。你可以这样做:

from lmfit.models import GaussianModel 

# create model with two Gaussians 
model = GaussianModel(prefix='g1_') + GaussianModel(prefix='g2_') 

# create parameters for composite model, but with default values: 
params = model.make_params() 

# now set starting values, boundaries, constraints 
params['g1_center'].set(5, min=1, max=7) 
params['g2_center'].set(8, min=5, max=10) 

# constrain sigma for 'g2' to be the same as for 'g1' 
params['g2_sigma'].set(expr='g1_sigma') 

# you could also do something like this: 
params['g2_amplitude'].set(expr='g1_amplitude/10.0') 

# now you're ready to fit this model to some data: 
result = model.fit(data, params, x=x)