[SciPy-user] Cannot get optimize.fmin_bfgs to work

Williamson, Ross Ross.Williamson@usap....
Tue Mar 3 03:15:08 CST 2009


Hi All

I'm trying to get optimize.fmin_bfgs to work with no luck - I have an x,y array of numbers (about 100) to which I'm trying to fit a curve to. Both the auto fprime calculator and my own Jacbian do not work:

ValueError: setting an array element with a sequence.

I'm presuming I'm doing something really silly but I cannot find a simple example on the web (The one using the Rosenbrock function does not help).

The reason for doing this is that I want to progress to the bounded version of this function as leastsq works (but does not allow for bounded data).

Please find code below:

from numpy import zeros
from scipy import optimize
def fitfunc(p, x):
   
   a_white = p[0]
   a_pink = p[1]
   a_red = p[2]
   a_phot = p[3]
   fk = p[4]  # photon_noise roll-off ~ 11.4 Hz mean 2008 season 
   alpha2 = p[5]
   
   #result = wlevel*(1.+(f_knee/x)^alpha)
   result = a_white + a_pink * x ** (-1.0) + a_red * x ** (-1.0 * alpha2) + a_phot / (1. + x ** 2 / fk ** 2)

   return result

def fitgrad(p,x,y,err):
   #Calculate Jacobian
   J = zeros(len(p),float)
   J[0] = 1.0
   J[1] = 1.0/x
   J[2] = x ** (-1.0 * alpha2)
   J[3] = 1.0/(1. + x ** 2 / fk ** 2)
   J[4] = (2 * a_phot * x**2)/(fk**3 * ((x**2/a_phot**2) + 1)**2)
   J[5] = a_red * x **(-1.0* alpha2) * log(x)
   
   return J

def errorfunc(p,x,y,err):
    err_r = (y-fitfunc(p,x))/err
    print err_r
    return err_r

def test(x,y,noise):

    pinit = [0.00125645, 0.00062822510, 0.00062822510,0.00062822510, 11.0, 2.0]

    out = optimize.leastsq(errorfunc,pinit,args=(x,y,noise), full_output=1)
    out2 = optimize.fmin_bfgs(errorfunc,pinit,fprime=fitgrad,args=(x,y,noise))
    print out[0]
    print out[1]

    return out



More information about the SciPy-user mailing list