【问题标题】:Define model function parameters as a function of independent variable using lmfit python使用lmfit python将模型函数参数定义为自变量的函数
【发布时间】:2019-07-03 08:48:21
【问题描述】:

我正在尝试为我的数据拟合模型函数。数据是时间(t)系列。模型函数需要在特定时间更改(在这种情况下,t=7 和 t=14),以便在每个时间点添加另一个表达式。因此我想要一个参数是时间的函数,即 c = 0 if t 。


rate() 是我的模型函数,ak 是我要优化的参数,c1, c2 是上面讨论的时间相关系数。 我使用 .make_params 方法来定义我的参数,并将 c1c2 的相关表达式传递到 .add em> 方法。

from numpy import exp
from lmfit import Model


# model function
def rate(x, a, k, c1, c2):
    def rate_unit(z):
        return a * (exp(-k * (z - 0.5)) - exp(-k * (z + 0.5)))

    return rate_unit(x) + c1 * rate_unit(x - 7) + c2 * rate_unit(x - 14)

# define independent and dependent variables
t = data.index.values
y = data.values

# setup the model
rate_model = Model(rate)

# setup parameters
parameters = rate_model.make_params()
parameters.add('a', value=200)
parameters.add('k', value=0.5)
parameters._asteval.symtable['t'] = t
parameters.add('c1', expr='0 if t < 7 else 1')
parameters.add('c2', expr='0 if t < 14 else 1')

# fit model to data
fit_result = rate_model.fit(y, parameters, x=t)

数据是熊猫系列:

In [32]: data                                                                                                                                               
Out[32]: 
days
0      0.000000
1     50.986817
3      8.435668
7      0.519960
8     80.628749
10    10.067202
14     6.065180
15    88.029249
21     4.854688
Name: ORG, dtype: float64

这是我得到的错误:

ValueError
   <_ast.Module object at 0x7fab7d47f278>
The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Traceback (most recent call last):
  File "model_dynamics.py", line 58, in <module>
    parameters.add('c1', expr='0 if t < 7 else 1')

ValueError: at expr='<_ast.Module object at 0x7fab7d47f278>'

如果有任何建议,我将不胜感激,

干杯,

【问题讨论】:

  • 这个(常见)错误表明您正在将多个值(数组)与一个值(布尔值)进行比较,正如它所说的那样模棱两可。根据您的需要,您可以使用 a.any() 或 a.all() 进行比较。您可能希望将特定时间瞬间与您的阈值进行比较
  • 也许将expr='0 if t &lt; 7 else 1'这一行改为expr='1*(t&gt;=7)'
  • 谢谢@BlueRineS。我尝试了
    expr='1*(t>=7)',但得到了同样的错误 ValueError: The truth value of an array with multiple element is ambiguous.
  • 你能举例说明data应该是什么吗?这样我们会更容易提供帮助。
  • 我编辑了问题并添加了@BlueRineS 的数据

标签: python lmfit


【解决方案1】:

lmfit 中的参数旨在包含单个值。它们将在每个拟合步骤(即每次调用您的目标/模型函数)评估一次,并且不会针对每个数据点单独评估。

无论如何,您想要使用“numpy.where()”来代替比较运算符。

但是,我认为在代码中而不是在参数表达式中执行“where”会更加明显和可行,例如:

import numpy as np
# model function
def rate(x, a, k):
    def rate_unit(z):
        return a * (np.exp(-k * (z - 0.5)) - enp.xp(-k * (z + 0.5)))
    c1 = np.zeros(len(x))
    c2 = np.zeros(len(x))
    c1[np.where(x>7)] = 1
    c2[np.where(x>14)] = 1
    return rate_unit(x) + c1 * rate_unit(x-7) + c2 * rate_unit(x-14)


# setup the model
rate_model = Model(rate)

# setup parameters
parameters = rate_model.make_params(a=200, k=0.5)

# fit model to data
fit_result = rate_model.fit(y, parameters, x=t)

提前计算一次c1c2 可能更有效,更接近于您所做的工作。然后,您可以告诉 lmfit 将这些视为独立的、不变的参数:

import numpy as np
# helper function (define once, not each time `rate` is called!)
def rate_unit(z):
    return a * (np.exp(-k * (z - 0.5)) - enp.xp(-k * (z + 0.5)))

# model function
def rate(x, a, k, c1, c2):
    return rate_unit(x) + c1 * rate_unit(x-7) + c2 * rate_unit(x-14)

# setup the model
rate_model = Model(rate, independent_vars=('x', 'c1', 'c2'))

# setup parameters
parameters = rate_model.make_params(a=200, k=0.5)

c1 = np.zeros(len(t))
c2 = np.zeros(len(t))
c1[np.where(t>7)] = 1
c2[np.where(t>14)] = 1
# fit model to data
fit_result = rate_model.fit(y, parameters, x=t, c1=c1, c2=c2)

当然,结果应该是一样的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-12
    • 1970-01-01
    相关资源
    最近更新 更多