votes up 3

`method` must be 'trf', 'dogbox' or 'lm'.

Package:
scipy
github stars 8546
Exception Class:
ValueError

Raise code

""" 
    >>> res_wrapped = least_squares(f_wrap, (0.1, 0.1), bounds=([0, 0], [1, 1]))
    >>> z = res_wrapped.x[0] + res_wrapped.x[1]*1j
    >>> z
    (0.49999999999925893+0.49999999999925893j)

    """
    if method not in ['trf', 'dogbox', 'lm']:
        raise ValueError("`method` must be 'trf', 'dogbox' or 'lm'.")

    if jac not in ['2-point', '3-point', 'cs'] and not callable(jac):
        raise ValueError("`jac` must be '2-point', '3-point', 'cs' or "
                         "callable.")

    if tr_solver not in [None, 'exact', 'lsmr']:
        raise ValueError("`tr_solver` must be None, 'exact' or 'lsmr'.")
🙏 Scream for help to Ukraine
Today, 3rd July 2022, Russia continues bombing and firing Ukraine. Don't trust Russia, they are bombing us and brazenly lying in same time they are not doing this 😠, civilians and children are dying too! We are screaming and asking exactly you to help us, we want to survive, our families, children, older ones.
Please spread the information, and ask your governemnt to stop Russia by any means. We promise to work extrahard after survival to make the world safer place for all.

Ways to fix

votes up 2 votes down

Here is a simple explanation where and how this exception is raised. This exception is raised inside the function scipy.optimize.least_squares() .

The cause of this exception is a wrong or invalid value of method passed when the function is called.

The valid values of the parameter method are, according to the documentation,

{‘trf’, ‘dogbox’, ‘lm’}

These are used to specify the choice of the algorithm to perform optimization.

Steps for reproducing this exception:

$ pipenv install scipy numpy

$ pipenv shell

from scipy.optimize import least_squares
import numpy as np
def fun_rosenbrock(x):
    return np.array([10 * (x[1] - x[0]**2), (1 - x[0])])
x0_rosenbrock = np.array([2, 2])
res_1 = least_squares(fun_rosenbrock, x0_rosenbrock,method="dogbx")
print(res_1.x)
print(res_1.cost)
print(res_1.optimality)

---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-8-428f8c1936ae> in <module>()  8   9 x0_rosenbrock = np.array([2, 2]) ---> 10 res_1 = least_squares(fun_rosenbrock, x0_rosenbrock,method="dogbx")  11 print(res_1.x)  12 print(res_1.cost) 
/usr/local/lib/python3.7/dist-packages/scipy/optimize/_lsq/least_squares.py in least_squares(fun, x0, jac, bounds, method, ftol, xtol, gtol, x_scale, loss, f_scale, diff_step, tr_solver, tr_options, jac_sparsity, max_nfev, verbose, args, kwargs)  747 """  748 if method not in ['trf', 'dogbox', 'lm']: --> 749 raise ValueError("`method` must be 'trf', 'dogbox' or 'lm'.")  750   751 if jac not in ['2-point', '3-point', 'cs'] and not callable(jac): 
ValueError: `method` must be 'trf', 'dogbox' or 'lm'.

Fixed:

from scipy.optimize import least_squares
import numpy as np
def fun_rosenbrock(x):
    return np.array([10 * (x[1] - x[0]**2), (1 - x[0])])
x0_rosenbrock = np.array([2, 2])
res_1 = least_squares(fun_rosenbrock, x0_rosenbrock,method="dogbox")
print(res_1.x)
print(res_1.cost)
print(res_1.optimality)

[1. 1.] 9.860761315262648e-30 8.881784263175701e-14

Oct 03, 2021 kellemnegasi answer
kellemnegasi 30.0k

Add a possible fix

Please authorize to post fix