Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scipy fmin_slsqp error "failed in converting 8th argument `g' of _slsqp.slsqp to C/Fortran array"

I have seen this question or a variant asked elsewhere e.g.

Scipy error using optimization module. Failure converting array to fortran

http://numpy-discussion.10968.n7.nabble.com/minimize-Algorithmen-Problem-with-boundarys-td37709.html

But they are not really put with a simple hackable example code. Nor are there any real answers (probably because of the lack of a simple demo of the problem).

The problem is, when trying to fit a function using scipy.optimise fmin_slsqp method you get this pretty opaque error

"failed in converting 8th argument `g' of _slsqp.slsqp to C/Fortran array"

In the code below I fit a linear function to random correlated data using the leastsq method. From the .docs I can see no reason that the same syntax shouldn't do the same thing using fmin_slsqp, but it doesn't.

Does anybody know why?

import numpy as nm
from scipy.optimize import leastsq, fmin_slsqp
import matplotlib.pyplot as plt

# residuals of linear function
def res(params,x,y_real):
  y_fit = params[0] +x*params[1]
  res = y_fit-y_real
  return res


#generate correlated data
xx = nm.array([-0.51, 51.2])
yy = nm.array([0.33, 51.6])
means = [xx.mean(), yy.mean()]  
stds = [xx.std() / 3, yy.std() / 3]
corr = 0.8         # correlation
covs = [[stds[0]**2          , stds[0]*stds[1]*corr], 
        [stds[0]*stds[1]*corr,           stds[1]**2]] 

m = nm.random.multivariate_normal(means, covs, 100)
x = m[:,0]
y = m[:,1]

# Initial values of parameters
initvals = [0,0]

fit1,j = leastsq(res, initvals, args=(x,y))


#Plot fit 1

y_fit = fit1[0] + fit1[1]*x

plt.scatter(x,y)
plt.plot(x,y_fit)
plt.show()

fit2 = fmin_slsqp(res, initvals, args=(x,y))
like image 645
Harry Matthews Avatar asked Nov 10 '22 18:11

Harry Matthews


1 Answers

I get the same error when the return from the Objective function is not a scalar. A minimal example which causes this error is

from scipy.optimize import fmin_slsqp
def fn(x):
    return [0.,1.]
x = [0, 1., 2.]
minsoln = fmin_slsqp(fn, x)

while the following does not raise the error,

from scipy.optimize import fmin_slsqp 
def fn(x):
    return 0.
x = [0, 1., 2.]
minsoln = fmin_slsqp(fn, x)

I think this is either a bug or should have a clearer error message. I've raise an issue.

UPDATE:

This has now been resolved by b-carter to give a clear error message,

"Objective function must return a scalar" 

with the documentation updated, see this thread for discussion.

like image 167
Ed Smith Avatar answered Nov 14 '22 23:11

Ed Smith