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 包有办法解决这个问题吗?

4

2 回答 2

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)
于 2017-08-25T13:03:31.317 回答
0

我刚刚在这里找到了我自己的非常相似的问题的答案:

Scipy curve_fit 边界和条件

我希望这会有所帮助,这些对我有帮助:

https://lmfit.github.io/lmfit-py/constraints.html

http://blog.danallan.com/projects/2013/model/

于 2017-08-29T09:50:45.087 回答