简体   繁体   English

使用任意数量的参数创建python lmfit模型

[英]Creating a python lmfit Model with arbitrary number of parameters

Is there a way to construct a an lmfit Model based on a function with an arbitrary number of dependent variables? 有没有一种方法可以基于具有任意数量因变量的函数构造lmfit模型? For example: 例如:

from lmfit import Model

def my_poly(x, *params):
  func = 0
  for i in range(len(params)):
    func+= params[i]*z**i
  return func

#note: below does not work
my_model = Model(my_poly, independent_vars = ['x'], param_names = ['A','B','C'])

Something similar to the above would be wonderful if I am interested in a polynomial series and want to test the performance as the series grows or shrinks. 如果我对一个多项式级数感兴趣并且想要随着级数的增长或缩小来测试性能,那么与上述类似的事情将是很棒的。

Since Model() uses function argument names to build parameter names, using *params won't work easily (how would one know to call them A , B , C , and not coeff0 , coeff1 , coeff2 , or something else?). 由于Model()使用函数参数名称来构建参数名称,因此使用*params并不容易(一个人怎么称呼它们ABC而不是coeff0coeff1coeff2或其他名称?)。

I don't know that a truly arbitrary number could be supported, but it should be possible to do a very large number. 我不知道可以支持一个真正任意的数字 ,但是应该可以做一个很大的数字。 The Polynomial Model (see http://lmfit.github.io/lmfit-py/builtin_models.html#polynomialmodel and https://github.com/lmfit/lmfit-py/blob/master/lmfit/models.py#L126 for implementation) supports up to 7 coefficients. 多项式模型(请参见http://lmfit.github.io/lmfit-py/builtin_models.html#polynomialmodelhttps://github.com/lmfit/lmfit-py/blob/master/lmfit/models.py#L126用于实施)最多支持7个系数。 It should be no problem to extend that to a much larger number. 将其扩展到更大的数量应该没有问题。 It might easily lead to computational problems, but I think that is what you are expecting to explore. 它可能很容易导致计算问题,但是我认为这是您期望探索的。

If you're willing to make a small change, it is be possible to do something like you're looking for. 如果你愿意做一个小的变化,这有可能做这样的事情,你要寻找的。 This uses keyword arguments instead of positional arguments, and relies on parameter name order (that is with sort ) to indicate which coefficient goes with what exponent, rather than order of the positional arguments. 这使用关键字参数而不是位置参数,并依靠参数名称顺序(即sort )来指示哪个系数与指数相乘,而不是位置参数的顺序。 This might be close to what you're looking for: 这可能与您要查找的内容接近:

import numpy as np

from lmfit import Model, Parameters

def my_poly(x, **params):
    val= 0.0
    parnames = sorted(params.keys())
    for i, pname in enumerate(parnames):
        val += params[pname]*x**i
    return val

my_model = Model(my_poly)

# Parameter names and starting values
params = Parameters()
params.add('C00', value=-10)
params.add('C01', value=  5)
params.add('C02', value=  1)
params.add('C03', value=  0)
params.add('C04', value=  0)

x = np.linspace(-20, 20, 101)
y = -30.4 + 7.8*x - 0.5*x*x + 0.03 * x**3 + 0.009*x**4
y = y + np.random.normal(size=len(y), scale=0.2)

out = my_model.fit(y, params, x=x)
print(out.fit_report())

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM