【问题标题】:lmfit- tying fitting parameters one to the otherlmfit- 将拟合参数相互关联
【发布时间】:2017-08-29 17:50:45
【问题描述】:

我正在使用 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 包有什么办法吗?

【问题讨论】:

    标签: python lmfit function-fitting


    【解决方案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)
    

    【讨论】:

      【解决方案2】:

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

      Scipy curve_fit bounds and conditions

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

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

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

      【讨论】:

        猜你喜欢
        • 2018-10-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-01
        • 2022-10-12
        相关资源
        最近更新 更多