首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python: Scipy.optimize Levenberg-marquardt方法

Python: Scipy.optimize Levenberg-marquardt方法
EN

Stack Overflow用户
提问于 2016-09-06 17:15:14
回答 1查看 5.6K关注 0票数 1

我有一个关于如何在Python中使用Levenberg-Marquardt优化方法的问题。在SCIPY库中有很多optimization methods

我尝试了两种方法(内尔德-米德和盆地跳跃),这两种方法都可以使用以下命令很好地工作:

代码语言:javascript
复制
# Nelder-Mead
res0_10 = optimize.minimize(f0_10, x0, method='Nelder-Mead', options={'disp': True, 'maxiter': 2000})

# Basin-hopping
res0_10 = optimize.basinhopping(f0_10, x0, niter=100, disp=True)

当我使用Levenberg-Marquardt时,问题就出现了(我只复制了错误的部分,因为程序很长)

代码语言:javascript
复制
def f0_10(x):
    m, u, z, s = x 
    for i in range(alt_max):
         if i==alt_min: suma=0
         if i > alt_min:
         suma = suma + (B(x, i)-b0_10(x, i))**2
    return np.sqrt(suma/alt_max)

x0 = np.array([40., 0., 500., 50.])

res0_10 = root(f0_10, x0, jac=True, method='lm')

我只修改了最后一句话(res0_10 = root...)。程序编译得很好,但当我执行程序时:

代码语言:javascript
复制
Exception in Tkinter callback

Traceback (most recent call last):

File "C:\Users\Quini SB\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.7.4.3348.win-x86_64\lib\lib-tk\Tkinter.py", line 1536, in __call__
return self.func(*args)

File "C:\Users\Quini SB\Desktop\tfg\Steyn - levmar.py", line 384, in askopenfilename
res0_10 = root(f0_10, x0, jac=True, method='lm')

File "C:\Users\Quini SB\AppData\Local\Enthought\Canopy\User\lib\site-packages\scipy\optimize\_root.py", line 188, in root
sol = _root_leastsq(fun, x0, args=args, jac=jac, **options)

File "C:\Users\Quini SB\AppData\Local\Enthought\Canopy\User\lib\site-packages\scipy\optimize\_root.py", line 251, in _root_leastsq
factor=factor, diag=diag)

File "C:\Users\Quini SB\AppData\Local\Enthought\Canopy\User\lib\site-packages\scipy\optimize\minpack.py", line 377, in leastsq
shape, dtype = _check_func('leastsq', 'func', func, x0, args, n)

File "C:\Users\Quini SB\AppData\Local\Enthought\Canopy\User\lib\site-packages\scipy\optimize\minpack.py", line 26, in _check_func
res = atleast_1d(thefunc(*((x0[:numinputs],) + args)))

File "C:\Users\Quini SB\AppData\Local\Enthought\Canopy\User\lib\site-packages\scipy\optimize\optimize.py", line 64, in __call__
self.jac = fg[1]

IndexError: invalid index to scalar variable.

为什么会发生这个错误?

EN

回答 1

Stack Overflow用户

发布于 2016-09-06 17:30:28

来自文档:

代码语言:javascript
复制
jac : bool or callable, optional

    If jac is a Boolean and is True, fun is assumed to return the value
    of Jacobian along with the objective function. If False, the 
    Jacobian will be estimated numerically. jac can also be a callable 
    returning the Jacobian of fun. In this case, it must accept the
    same arguments as fun.

因此,您的函数'f0_10‘需要返回两个值,因为您将jac设置为True

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39345139

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档