0

curve_fit鉴于此功能,我正在尝试使用

def F(xy,*p):
    x,y = xy
    c = np.array(p).ravel()

    n = (len(c)-1)/4
    omega = pi/180.0

    z = c[0]
    for t in range(n):
        z += c[4*t+1] * (cos((t+1)*omega*x))
        z += c[4*t+2] * (cos((t+1)*omega*y))
        z += c[4*t+3] * (sin((t+1)*omega*x))
        z += c[4*t+4] * (sin((t+1)*omega*y))

    return z


def G(xy,*p):
    x,y = xy
    c = np.array(p).ravel()

    ngm = (len(c))/7

    z = 0
    for t in range(ngm):
       a  = c[7*t]
       cx = c[7*t+1]
       mx = c[7*t+2]
       sx = c[7*t+3]
       cy = c[7*t+4]
       my = c[7*t+5]
       sy = c[7*t+6]

       z += a * np.exp(-((cx*(x-mx)**2)/(2*(sx**2)))-((cy*(y-my)**2)/(2*(sy**2))))

 return z

def FG(xy,*p):
    x,y = xy
    c = np.array(p).ravel()

    nf = int(c[0])
    ng = int(c[1])
    print nf,ng

    pf = [c[i] for i in range(2,4*nf+3)]
    pg = [c[i] for i in range(4*nf+3,4*nf+7*ng+3)]

    z1 = F(xy,pf)
    z2 = G(xy,pg)

    return z1+z2

pfit,cov = opt.curve_fit(FG,xy,z,p,bounds=bounds)

我确信两者的形状p都是bounds合适的。我尝试打印nfand ng,并且它们被正确打印,直到经过一些迭代(大约在第 20 次函数调用之后,每次运行都不相同),其中值发生了显着变化。

第 20 次(或更多)运行后,它返回以下错误:

File "/Users/pensieve/calcs/3D_AA/0_codes/fitpkgs.py", line 144, in FGfit
pfit,cov = opt.curve_fit(FG,xy,z,p,bounds=bounds)

File "/Library/Python/2.7/site-packages/scipy-0.18.1-py2.7-macosx-10.10-intel.egg/scipy/optimize/minpack.py", line 683, in curve_fit
**kwargs)

File "/Library/Python/2.7/site-packages/scipy-0.18.1-py2.7-macosx-10.10-intel.egg/scipy/optimize/_lsq/least_squares.py", line 878, in least_squares
tr_options.copy(), verbose)

File "/Library/Python/2.7/site-packages/scipy-0.18.1-py2.7-macosx-10.10-intel.egg/scipy/optimize/_lsq/trf.py", line 128, in trf
loss_function, tr_solver, tr_options, verbose)

File "/Library/Python/2.7/site-packages/scipy-0.18.1-py2.7-macosx-10.10-intel.egg/scipy/optimize/_lsq/trf.py", line 341, in trf_bounds
f_new = fun(x_new)

File "/Library/Python/2.7/site-packages/scipy-0.18.1-py2.7-macosx-10.10-intel.egg/scipy/optimize/_lsq/least_squares.py", line 764, in fun_wrapped
return np.atleast_1d(fun(x, *args, **kwargs))

File "/Library/Python/2.7/site-packages/scipy-0.18.1-py2.7-macosx-10.10-intel.egg/scipy/optimize/minpack.py", line 455, in func_wrapped
return func(xdata, *params) - ydata

File "/Users/pensieve/calcs/3D_AA/0_codes/fitfunctions.py", line 65, in FG
pgm = [c[i] for i in range(4*nf+3,4*nf+7*ng+3)]
IndexError: index out of bounds

作为参考,我使用 scipy 0.18.1。

4

0 回答 0