Python: Scipy.optimize Levenberg-marquardt method
<p>I have a question about how to use the Levenberg-Marquardt optimize method in Python </p>

<p>In the library SCIPY there are many methods of optimize --> <a href="http://docs.scipy.org/doc/scipy/reference/tutorial/optimize.html" rel="nofollow">http://docs.scipy.org/doc/scipy/reference/tutorial/optimize.html</a></p>

<p>I prove two methods(Nelder-Mead y Basinhopping) and the both works well whit the follow sentence:</p>

<p>Nelder mead ---> res0_10 = optimize.minimize(f0_10, x0, method='Nelder-Mead', options={'disp': True, 'maxiter': 2000})</p>

<p>Basinhopping ---> res0_10 = optimize.basinhopping(f0_10, x0, niter=100, disp=True)</p>

<p>The problem emerge when I use the Levenberg-Marquardt(I copy only the part of error, because the program is long)</p>

<pre><code>def f0_10(x):
    m, u, z, s = x 
    for i in range(alt_max):
         if i==alt_min: suma=0
         if i &gt; 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')
</code></pre>

<p>I only change the last sentence --> res0_10 = root..............</p>

<p>The program compile well, but when I ejecute the program:</p>

<p>Exception in Tkinter callback</p>

<p>Traceback (most recent call last):</p>

<p>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 <strong>call</strong>
return self.func(*args)</p>

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

<p>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)</p>

<p>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)</p>

<p>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)</p>

<p>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)))</p>

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

<p>IndexError: invalid index to scalar variable.</p>

<hr>

<p>I dont know because happen this error. I am student and I need this for my last work on Degree but my teacher dont know python</p>

<p>Thanks</p>

<p>From documentation:</p>

<pre><code>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.
</code></pre>

<p>So, your function 'f0_10' needs to return two values, because you set <code>jac</code> to <code>True</code></p>

python optimization scipy levenberg-marquardt