scipy curve_fit:函数调用的结果不是一个正确的浮点数组

时间:2018-03-07 12:07:02

标签: python scipy curve-fitting

尝试使用SciPy的curve_fit拟合曲线。

这是我的模特功能:

def fnc(t, mni, mej, e):
    tm = 40.87*sqrt(k)*(mej**3/e)**(1/4)
    y = tm/2/tNi
    s = y*(tCo-tNi)/tCo
    lst = []
    F = 64.e33*mej/sqrt(e)/86400
    G = 16.1*F
    for ti in t:
        if ti <= phthreshold:
            x = ti/tm
            A = lambda x: 64.44*x*exp(-2*x*y+x*x)
            B = lambda x: 13.56*x*exp(-2*x*y+2*x*s+x*x)
            iA = it.quad(A, 0, x)[0]
            iB = it.quad(B, 0, x)[0]
            li=2*mni*exp(-x**2)*(iA+iB)
            lst.append(li)
        else:
            li=78.0*mni*exp(-ti/6.075)+13.56*mni*(exp(-ti/77.233)-exp(-ti/6.075))*(0.81*(1-exp(-(F/ti)**2))+0.164*(1-exp(-(F/ti)**2))*(1-exp(-(G/ti)**2))+0.036*(1-exp(-(G/ti)**2)))
            lst.append(li)
    return np.array([1.e42*li for li in lst], dtype='float')

我按如下方式调用curve_fit:

par0 = [1,1,1]
par, cov = curve_fit(fnc, t, L, p0=par0)

这是我的数组:

[0.25000000000000006, 2.2500000000000004, 3.2500000000000004, 4.25,     5.250000000000001, 6.250000000000001, 7.250000000000001, 10.25, 11.25, 12.25, 13.25, 14.25, 15.25, 16.25, 17.25, 18.25, 19.25, 20.25, 21.25, 22.25, 23.25, 24.25, 25.25, 26.150000000000002, 26.25, 27.25, 28.25, 60.150000000000006, 62.150000000000006, 65.15, 66.15]

这里是fnc(t,1,1,1)的返回值:

[8.38866647e+39 5.66407251e+41 1.08038001e+42 1.69040245e+42
 2.36195439e+42 3.06742370e+42 3.78488909e+42 5.85551882e+42
 6.48390143e+42 7.07042759e+42 7.61141064e+42 8.10460466e+42
 8.54893916e+42 8.94429806e+42 9.29133462e+42 9.59131604e+42
 9.84599242e+42 1.00574857e+43 1.02281955e+43 1.03607185e+43
 1.04577805e+43 1.05221776e+43 1.05567267e+43 1.05646129e+43
 1.05642239e+43 1.05474094e+43 1.05089381e+43 6.28882967e+42
 6.12724518e+42 5.89298948e+42 5.81698332e+42]

所以这是一个适当的numpy数组。但是,运行代码后,我得到以下内容:

ValueError: object too deep for desired array
Traceback (most recent call last):
  File "/home/tajimura/python/fit.py", line 70, in <module>
    par, cov = curve_fit(fnc, t, L, p0=par0)
  File "/home/tajimura/anaconda3/lib/python3.6/site-packages/scipy/optimize    /minpack.py", line 742, in curve_fit
    res = leastsq(func, p0, Dfun=jac, full_output=1, **kwargs)
  File "/home/tajimura/anaconda3/lib/python3.6/site-packages/scipy/optimize/minpack.py", line 387, in leastsq
    gtol, maxfev, epsfcn, factor, diag)
minpack.error: Result from function call is not a proper array of floats.

我完全无法理解这里的问题。有什么想法吗?

0 个答案:

没有答案