亲爱的 Stackoverflow 社区,
我想用curve_fit拟合以下函数:
def fct(A, Ravg,sigma,pos,omega, x):
K=integrate.quad(lambda R:np.exp( - (R-Ravg)**2 / (2 * sigma**2) ),Ravg-sigma,Ravg+sigma)
data=[]
for i in range(0,len(x)):
data.append(integrate.quad(lambda R: (3*(np.sin(x[i]*R)-x[i]*R*np.cos(x[i]*R))/(x[i]*R)**3)**2*1/K[0] *np.exp( - (R-Ravg)**2 / (2 * sigma**2) ),Ravg-sigma,Ravg+sigma))
gafs=array(data)
return A*gafs[:,0]
我从curve_fit得到这个代码:
fitPars, covMatrix = curve_fit(fct, xaxis, yaxis)
以下错误消息:
TypeError: object of type 'numpy.float64' has no len()
我知道错误来自 for 循环。您知道如何使我的函数适合于曲线拟合吗?
完整的错误轨迹是:
TypeError Traceback (most recent call last)
<ipython-input-5-3e25e14bdca2> in <module>()
1
----> 2 fitPars, covMatrix = curve_fit(fct, xaxis, yaxis)
C:\Programfiles\Anaconda3\lib\site-packages\scipy\optimize\minpack.py in curve_fit(f, xdata, ydata, p0, sigma, absolute_sigma, check_finite, bounds, method, jac, **kwargs)
734 # Remove full_output from kwargs, otherwise we're passing it in twice.
735 return_full = kwargs.pop('full_output', False)
--> 736 res = leastsq(func, p0, Dfun=jac, full_output=1, **kwargs)
737 popt, pcov, infodict, errmsg, ier = res
738 cost = np.sum(infodict['fvec'] ** 2)
C:\Programfiles\Anaconda3\lib\site-packages\scipy\optimize\minpack.py in leastsq(func, x0, args, Dfun, full_output, col_deriv, ftol, xtol, gtol, maxfev, epsfcn, factor, diag)
375 if not isinstance(args, tuple):
376 args = (args,)
--> 377 shape, dtype = _check_func('leastsq', 'func', func, x0, args, n)
378 m = shape[0]
379 if n > m:
C:\Programfiles\Anaconda3\lib\site-packages\scipy\optimize\minpack.py in _check_func(checker, argname, thefunc, x0, args, numinputs, output_shape)
24 def _check_func(checker, argname, thefunc, x0, args, numinputs,
25 output_shape=None):
---> 26 res = atleast_1d(thefunc(*((x0[:numinputs],) + args)))
27 if (output_shape is not None) and (shape(res) != output_shape):
28 if (output_shape[0] != 1):
C:\Programfiles\Anaconda3\lib\site-packages\scipy\optimize\minpack.py in func_wrapped(params)
452 if transform is None:
453 def func_wrapped(params):
--> 454 return func(xdata, *params) - ydata
455 elif transform.ndim == 1:
456 def func_wrapped(params):
<ipython-input-3-4ae12bf8a77b> in fct(A, Ravg, sigma, pos, omega, x)
52 K=integrate.quad(lambda R:np.exp( - (R-Ravg)**2 / (2 * sigma**2) ),Ravg-sigma,Ravg+sigma)
53 data=[]
---> 54 for i in range(0,len(x)):
55 data.append(integrate.quad(lambda R: (3*(np.sin(x[i]*R)-x[i]*R*np.cos(x[i]*R))/(x[i]*R)**3)**2*1/K[0] *np.exp( - (R-Ravg)**2 / (2 * sigma**2) ),Ravg-sigma,Ravg+sigma))
56 gafs=array(data)
TypeError: object of type 'numpy.float64' has no len()