Python: Scipy.optimize Levenberg-marquardt method


I have a question about how to use the Levenberg-Marquart optimization method in come several of these methods: link

I have tried two methods (nelder-mead and basinhopping) and they work correctly with the following sentence:

Nelder mead:

res0_10 = optimize.minimize(f0_10,
                            options={'disp': True, 'maxiter': 2000})


res0_10 = optimize.basinhopping(f0_10, x0, niter=100, disp=True)

The problem arises when I use the Levenberg-Marquardt (I copy only the part of the error, the program is long so I do not copy the rest):

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')

The program compiles well, but at the time of execution, I get the following error:

Exception in Tkinter callback

Traceback (most recent call last):

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

File "C:\Users\Quini SB\Desktop\tfg\Steyn -", 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\", line 188, in root sol = _root_leastsq(fun, x0, args=args, jac=jac,

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

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

IndexError: invalid index to scalar variable.

I'm not sure why, I'm a student and I need it for the TFG, but my teacher does not control , so it's probably silly, but I'm not able to fix it.

asked by Quini 06.09.2016 в 02:59

2 answers


The problem is that the function root that you are calling is not the correct one, but that of the module Tkinter .

If you replace root with scipy.optimize.root should work, as long as you have imported scipy.optimize in your program.

Note that if you imported it with an alias, this is

import scipy.optimize as opti

Then you can use the root function under that alias:

res0_10 = opti.root(f0_10, x0, jac=True, method='lm')
answered by 16.02.2017 в 09:00

The indexerror indicates that there was an error while trying to access a nonexistent position in the memory. Which indicates that you have tried to access an array in an erroneous way.

answered by 10.09.2016 в 05:09