由于我相当精细的函数和数据准备过程(将分析模型数据拟合到某些测量值) ,我想使用一个__call__
类的方法作为 Numpy curve_fit 函数的输入。通过定义一个函数它工作得很好,但我不能让它与类一起工作。
要重新创建我的问题,您可以运行:
import numpy as np
from scipy.optimize import curve_fit
#WORKS:
#def goal(x,a1,a2,a3,a4,a5):
# y=a1*x**4*np.sin(x)+a2*x**3+a3*x**2+a4*x+a5
# return y
# DOES NOT WORK:
class func():
def __call__(self,x,a1,a2,a3,a4,a5):
y=a1*x**4*np.sin(x)+a2*x**3+a3*x**2+a4*x+a5
return y
goal=func()
#data prepraration ***********
xdata=np.linspace(0,50,100)
ydata=goal(xdata,-2.1,-3.5,6.6,-1,2)
# ****************************
popt, pcov = curve_fit(goal, xdata, ydata)
print 'optimial parameters',popt
print 'The estimated covariance of optimial parameters',pcov
我得到的错误是:
Traceback (most recent call last):
File "D:\...some path...\test_minimizacija.py", line 35, in <module>
popt, pcov = curve_fit(goal, xdata, ydata)
File "C:\Python26\lib\site-packages\scipy\optimize\minpack.py", line 412, in curve_fit
args, varargs, varkw, defaults = inspect.getargspec(f)
File "C:\Python26\lib\inspect.py", line 803, in getargspec
raise TypeError('arg is not a Python function')
TypeError: arg is not a Python function
我怎样才能使这项工作?