类方法作为scipy.optimize.curve_fit的模型函数 [英] class method as a model function for scipy.optimize.curve_fit
问题描述
手册 curve_fit
中有一条声明
模型函数f(x,...)它必须将自变量作为第一个参数,并将参数作为单独的其余参数容纳。
The model function, f(x, ...). It must take the independent variable as the first argument and the parameters to fit as separate remaining arguments.
但是,我想使用作为模型函数,该类的方法定义为:
However, I would like to use as a model function a method of the class which is defined as:
def model_fun(self,x,par):
因此,如您所见,第一个参数不是自变量。有什么方法可以将类的方法用作curve_fit的模型函数
So, the first argument is not an independent variable, as you can see. Is there any way how I can use the method of a class as a model function for curve_fit
推荐答案
当然,请创建实例并通过其 bound 方法:
Sure, create an instance and pass its bound method:
class MyClass(object):
...
def model_fun(self,x,par): ...
obj = MyClass(...)
curve_fit(obj.model_fun, ...)
您可以找到有关绑定/未绑定/等的很好解释。在此问题中。
You can find a good explanation about bound/unbound/etc. in this question.
这篇关于类方法作为scipy.optimize.curve_fit的模型函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!