Curve_Fit 返回错误 "Result from function Call is not a proper array of floats"

Curve_Fit returrns error "Result from function Call is not a proper array of floats"

我正在尝试使用正确的方式调用 scipy curve_fit():

但是我收到错误: ValueError:对象对于所需的数组来说太深

函数调用的结果不是正确的浮点数组。

我正在计算的模型函数是:

The mathematical expression that optimizes model_f,我们试图从中找到最佳的 alpha,gamma。

函数model_f计算图片中附加的数学表达式。

with open("Data_case_3.csv",'r') as i:           #open a file in directory of this script for reading 
rawdata = list(csv.reader(i,delimiter=","))   #make a list of data in file

exampledata = np.array(rawdata[1:],dtype=np.float)    #convert to data array
xdata = exampledata[:,0]
ydata = exampledata[:,1]

m = 0.5 
omega0 = 34.15
k = np.square(omega0)*m


def model_f(x,a,g):
    zetaeq = (a*np.sqrt(np.pi)*(x**(g-1))*omega0*math.gamma(g/2))/(2*np.pi*k*math.gamma((3+g)/2))
    return zetaeq

#---------------------------------------- ----------------------------------

funcdata = model_f(xdata,0.3,0.1)                 
plt.plot(xdata,funcdata,label="Model")
plt.legend()

popt, pcov = curve_fit(model_f, xdata, ydata, p0=[0.3,0.1])

我附上提到的变量的数据类型:

Variable types and shapes of the script 你能帮我理解我做错了什么吗?

将这 2 个调用与 curve_fit 进行比较:

In [217]: xdata, ydata = np.ones(5), np.ones(5)
In [218]: curve_fit(model_f, xdata, ydata, p0=[0.3, 0.1])
Out[218]: 
(array([0.74436049, 0.02752099]),
 array([[2.46401533e-16, 9.03501810e-18],
        [9.03501810e-18, 3.31294823e-19]]))

In [219]: xdata, ydata = np.ones((5,1)), np.ones((5,1))
In [220]: curve_fit(model_f, xdata, ydata, p0=[0.3, 0.1])
ValueError: object too deep for desired array

Traceback (most recent call last):
  Input In [220] in <module>
    curve_fit(model_f, xdata, ydata, p0=[0.3, 0.1])
  File /usr/local/lib/python3.8/dist-packages/scipy/optimize/_minpack_py.py:789 in curve_fit
    res = leastsq(func, p0, Dfun=jac, full_output=1, **kwargs)
  File /usr/local/lib/python3.8/dist-packages/scipy/optimize/_minpack_py.py:423 in leastsq
    retval = _minpack._lmdif(func, x0, args, full_output, ftol, xtol,
error: Result from function call is not a proper array of floats.

哪个更接近您的体验?