class method as a model function for scipy.optimize.curve_fit

喜你入骨 提交于 2019-12-07 10:12:58

问题


There is a statement in the manual of curve_fit that

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

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


回答1:


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.



来源:https://stackoverflow.com/questions/16543610/class-method-as-a-model-function-for-scipy-optimize-curve-fit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!