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

前端 未结 2 1000
心在旅途
心在旅途 2021-01-13 09:53

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

Scipy error using optimization module. Failure converting array to fortran

http://numpy-discussio

相关标签:
2条回答
  • 2021-01-13 10:35

    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.

    0 讨论(0)
  • 2021-01-13 10:45

    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.

    0 讨论(0)
提交回复
热议问题