0

I am currently using scipy.optimize.fmin() function and I am experiencing a problem with it. When I look at the documentation it says:

Returns:

    xopt : ndarray
        Parameter that minimizes function.

    fopt : float
        Value of function at minimum: fopt = func(xopt).

    iter : int
        Number of iterations performed.

    funcalls : int
        Number of function calls made.

    warnflag : int
        1 : Maximum number of function evaluations made. 2 : Maximum number of iterations reached.

    allvecs : list
        Solution at each iteration.

But when I try this:

res, min = opt.fmin(optim, self._params, (param_optim, self._paramsIni, Qmes, critere_efficacite, self, codeBV, interval), maxiter=5)

I get this error:

ValueError: too many values to unpack (expected 2)

Anyone has an idea why? I mean is the documention wrong (I guess not) or am I doing something wrong? I am using scipy 0.19 and Python34

Thanks in advance.

5
  • The documentation you stated shows you the function returns six values, yet you assign it to two variables. Commented May 3, 2017 at 8:24
  • I tried res, min, a, b, c, d = opt.fmin(optim, self._params, (param_optim, self._paramsIni, Qmes, critere_efficacite, self, codeBV, interval), maxiter=5), it does not work either. Commented May 3, 2017 at 8:27
  • The only one that is working is res = opt.fmin(optim, self._params, (param_optim, self._paramsIni, Qmes, critere_efficacite, self, codeBV, interval), maxiter=5) but I really need this second returned value. Commented May 3, 2017 at 8:28
  • 1
    Unfortunately, fmin is one of those scipy functions that changes its output depending on its output. The relevant input parameter from the documentation: "full_output : bool, optional. Set to True if fopt and warnflag outputs are desired." So fmin outputs either 1 value, or 6 values, depending on the full_output input flag. Commented May 3, 2017 at 8:30
  • Thank you Evert! It solved my problem. I was wondering what was this full_output flag for, as it was not displaying anything. Commented May 3, 2017 at 8:33

1 Answer 1

3

To be slightly more exact: The function returns either a tuple of 6 values (

full_output : bool, optional Set to True if fopt and warnflag outputs are desired.

) or one (if it is left at False which is the default). If you want to have only the second value of the full output, I recommend you set full_output=True and pattern-match as suggested in the comments. Alternativly you can store the result in one tuple res = opt.fmin(<your arguments>) and then access r=res[0] min=res[1].

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.