我想为某些数据拟合S形曲线。由于SageMath包含find_fit
功能失败,我尝试直接使用scipy.optimize.curve_fit
。它没有做得很好,结果对最初的猜测非常敏感,经常被卡在上面,所以我试图提供一个雅各布派,希望这会有所帮助。雅可比函数需要采用什么形式?现在,我的代码如下:
#Compute the Jacobian (using SageMath)
c=8
x4Sigmoid(x) = c*x^n/(h+x^n)
sigjac = jacobian(x4Sigmoid, [n,h])
from scipy.optimize import curve_fit
from numpy import inf, power
#Define sigmoid function as Python function
def sigmoid(x, n, h):
return 8*power(x,n)/(h+power(x,n))
def jacfun(x, *args):
h,n = args[0]
return sigjac(x,h=h,n=n)
#Separate x and y values of data
x4xData = [val[0] for val in x4ciscoperchRelAbundanceData]
x4yData = [val[1] for val in x4ciscoperchRelAbundanceData]
#Do the fitting
popt, pcov = curve_fit(sigmoid, x4xData, x4yData, p0=[3,4], bounds = ([1, 0], [inf, inf]), jac=jacfun)
show(popt)
这会产生错误消息ValueError: too many values to unpack
。我如何为SciPy提供它可以使用的雅可比?或者我应该尝试别的吗?