【问题标题】:python lmfit, non-modifiable array as parameter for Model class?python lmfit,不可修改的数组作为模型类的参数?
【发布时间】:2018-07-06 09:36:58
【问题描述】:

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

当我调用 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 相关系数。

我设置模型类的人:

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()

【问题讨论】:

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


    【解决方案1】:

    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)
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多