python lmfit,不可修改的数组作为Model类的参数?

时间:2018-07-06 09:36:59

标签: python python-3.x least-squares lmfit

我只是想拟合一个函数,以检索两个数组之间的相关皮尔逊系数。这两个数组作为输入参数传递给函数,但它们不会改变。对于函数,应将它们解释为常量。我找到了一个参数选项,可以固定一个参数,即它不能改变,但仅适用于标量值。

当我调用Model.make_params()时,模型类尝试检查这些数组是否比最小/最大数组低或高。因为它们是常数,所以不需要此评估。

我的功能:

def __lin_iteration2__(xref, yref_scaled, xobs, yobs, slope, offset, verbose=False, niter=None):

    Acal = 1 + (offset + slope*xref)/xref
    xr_new = xref * Acal
    obs_interp1d = interp1d(xobs, yobs, kind='cubic')
    yobs_new = scale_vector(obs_interp1d(xr_new))
    rho = Pearson(yref_scaled, yobs_new)

    return rho

其中xref,yref_scaled,xobs和yobs是不变的数组,即常量。 “ interp1d”是来自scipy.interpolate的内插器运算符,“ scale_vector”在-1和1之间缩放矢量,而“ Pearson”计算出Pearson相关系数。

我设置了Model类的人

m = Model(corr.__lin_iteration3__)
par = m.make_params(yref_scaled = corr.yref_scaled, \
                    obs_interp1d=corr.obs_interp1d, offset=0, scale=0)
par['yref_scaled'].vary = False
par['obs_interp1d'].vary = False
r = m.fit

我得到的错误(当我调用模型类的“ make_params”函数时位于第二行):

Traceback (most recent call last):

  File "<ipython-input-3-c8f6550e831e>", line 1, in <module>
    runfile('/home/andrey/Noveltis/tests/new_correl_sp/new_correl.py', wdir='/home/andrey/Noveltis/tests/new_correl_sp')

  File "/usr/lib/python3/dist-packages/spyder/utils/site/sitecustomize.py", line 705, in runfile
    execfile(filename, namespace)

  File "/usr/lib/python3/dist-packages/spyder/utils/site/sitecustomize.py", line 102, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "/home/andrey/Noveltis/tests/new_correl_sp/new_correl.py", line 264, in <module>
    obs_interp1d=corr.obs_interp1d, offset=0, scale=0)

  File "/usr/lib/python3/dist-packages/lmfit/model.py", line 401, in make_params
    params.add(par)

  File "/usr/lib/python3/dist-packages/lmfit/parameter.py", line 338, in add
    self.__setitem__(name.name, name)

  File "/usr/lib/python3/dist-packages/lmfit/parameter.py", line 145, in __setitem__
    self._asteval.symtable[key] = par.value

  File "/usr/lib/python3/dist-packages/lmfit/parameter.py", line 801, in value
    return self._getval()

  File "/usr/lib/python3/dist-packages/lmfit/parameter.py", line 786, in _getval
    if self._val > self.max:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

1 个答案:

答案 0 :(得分:0)

lmfit中,模型函数的参数应为标量浮点参数值,但“独立变量”可以是任何python对象。默认情况下,第一个函数参数被假定为自变量,任何具有非数字默认值的关键字参数也被假定为自变量。但是,您可以在创建模型时指定哪些自变量是自变量(可以有多个自变量)。

我认为您想要的是:

m = Model(corr.__lin_iteration3__, independent_vars=['xref', 'yref_scaled', 'xobs', 'yobs'])

而且:您还可以传递任何Python对象,因此您可以将ref和obs数据打包到其他结构中,并执行类似的操作

def lin_iteration(Data, slope, offset, verbose=False, niter=None):

    Acal = 1 + (offset + slope*Data['xref'])/Data['xref']
    xr_new = Data['xref'] * Acal
    # or maybe that would be clearer as just
    #   xr_new = offset + (1+slope)* Data['xref'] 
    obs_interp1d = interp1d(Data['xobs'], Data['yobs'], kind='cubic')
    yobs_new = scale_vector(obs_interp1d(xr_new))
    rho = Pearson(Data['yref_scaled'], yobs_new)

    return rho

m = Model(lin_iteration)
par = m.make_params(offset=0, scale=0)
Data = {'xref': xref, 'yref_scaled': yref_scaled, 'xobs': xobs, 'yobs': yobs}
result = m.fit(Data, params)

当然,这一切都未经测试,但这可能会使您的生活更轻松...