Skip to content Skip to sidebar Skip to footer

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/m

Solution 1:

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
deffn(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 
deffn(x):
    return0.
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.

Solution 2:

Hi I had the same error with the following:

def ptf_returns(weights,returns):

    return pd.DataFrame(np.array(returns).T*(weights)).T.mean().mean()

When I add the following it works:

def ptf_returns(weights,returns):

    return float(pd.DataFrame(np.array(returns).T*(weights)).T.mean().mean())

The bug seems to be oriented around the type() of the response.

Post a Comment for "Scipy Fmin_slsqp Error "failed In Converting 8th Argument `g' Of _slsqp.slsqp To C/fortran Array""