Scipy.optimize-固定参数的曲线拟合 [英] Scipy.optimize - curve fitting with fixed parameters

查看:203
本文介绍了Scipy.optimize-固定参数的曲线拟合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 scipy.optimize.leastsq 进行曲线拟合。例如。对于高斯:

I'm performing curve fitting with scipy.optimize.leastsq. E.g. for a gaussian:

def fitGaussian(x, y, init=[1.0,0.0,4.0,0.1]):
    fitfunc = lambda p, x: p[0]*np.exp(-(x-p[1])**2/(2*p[2]**2))+p[3] # Target function
    errfunc = lambda p, x, y: fitfunc(p, x) - y # Distance to the target function
    final, success = scipy.optimize.leastsq(errfunc, init[:], args=(x, y))
    return fitfunc, final

现在,我想选择修复拟合中某些参数的值。我发现建议使用其他包 lmfit ,我想避免,或者非常笼统,例如此处
因为我需要一个解决方案

Now, I want to optionally fix the values of some of the parameters in the fit. I found that suggestions are to use a different package lmfit, which I want to avoid, or are very general, like here. Since I need a solution which


  1. 与numpy / scipy配合使用(没有其他软件包等)

  2. 是独立于参数本身的,

  3. 是灵活的,其中参数是否固定,

我使用每个参数的条件得出以下结论:

I came up with the following, using a condition on each of the parameters:

def fitGaussian2(x, y, init=[1.0,0.0,4.0,0.1], fix = [False, False, False, False]):
    fitfunc = lambda p, x: (p[0] if not fix[0] else init[0])*np.exp(-(x-(p[1] if not fix[1] else init[1]))**2/(2*(p[2] if not fix[2] else init[2])**2))+(p[3] if not fix[3] else init[3]) 
    errfunc = lambda p, x, y: fitfunc(p, x) - y # Distance to the target function
    final, success = scipy.optimize.leastsq(errfunc, init[:], args=(x, y))
    return fitfunc, final

虽然可以正常工作,但既不实用,也不美观。
所以我的问题是:对于固定参数,是否有更好的方法在scipy中执行曲线拟合?还是有包装程序,这些包装程序已经包含了这样的参数修复功能?

While this works fine, it's neither practical, nor beautiful. So my question is: Are there better ways of performing curve fitting in scipy for fixed parameters? Or are there wrappers, which already include such parameter fixing?

推荐答案

使用 scipy ,没有我知道的内置选项。

Using scipy, there are no builtin options that I am aware of. You will always have to do a work-around like the one you already did.

但是,如果您愿意使用包装器,那么我建议使用我自己的 symfit ?它是 scipy 的包装,其可读性和较少的样板代码为其核心原理。在 symfit ,您的问题将解决为:

If you are willing to use a wrapper package however, may I recommend my own symfit? This is a wrapper to scipy with readability and less boilerplate code as its core principles. In symfit, your problem would be solved as:

from symfit import parameters, variables, exp, Fit, Parameter

a, b, c, d = parameters('a, b, c, d')
x, y = variables('x, y')

model_dict = {y: a * exp(-(x - b)**2 / (2 * c**2)) + d}

fit = Fit(model_dict, x=xdata, y=ydata)
fit_result = fit.execute()

a,b ,c,d = parameters('a,b,c,d')产生四个 Parameter 对象。要修复例如将参数 c 设置为其初始值,请在调用 fit.execute()之前在任意位置执行以下操作:

The line a, b, c, d = parameters('a, b, c, d') makes four Parameter objects. To fix e.g. the parameter c to its initial value, do the following anywhere before calling fit.execute():

c.value = 4.0
c.fixed = True

因此,可能的最终结果可能是:

So a possible end result might be:

from symfit import parameters, variables, exp, Fit, Parameter

a, b, c, d = parameters('a, b, c, d')
x, y = variables('x, y')

c.value = 4.0
c.fixed = True

model_dict = {y: a * exp(-(x - b)**2 / (2 * c**2)) + d}

fit = Fit(model_dict, x=xdata, y=ydata)
fit_result = fit.execute()

如果您希望代码更加动态,可以使用 Parameter 对象直接使用:

If you want to be more dynamic in your code, you could make the Parameter objects straight away using:

c = Parameter(4.0, fixed=True)

有关更多信息,请检查文档: http://symfit.readthe docs.io/en/latest/tutorial.html#simple-example

For more info, check the docs: http://symfit.readthedocs.io/en/latest/tutorial.html#simple-example

这篇关于Scipy.optimize-固定参数的曲线拟合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆