Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000

Scipy’s

     curve_fit() 

uses iterations to search for optimal parameters. If the number of iterations exceeds the default number of 800, but the optimal parameters are still not found, then this error will be raised.

    Optimal parameters not found: Number of calls to function has reached maxfev = 800

You can provide some initial guess parameters for curve_fit(), then try again. Or, you can increase the allowable iterations. Or do both!

Here is an example:

    popt, pcov = curve_fit(exponenial_func, x, y, p0=[1,0,1], maxfev=5000)

p0 is the guess

maxfev is the max number of iterations

You can also try setting bounds which will help the function find the solution.
However, you cannot set bounds and a max_nfev at the same time.

    popt, pcov = curve_fit(exponenial_func, x, y, p0=[1,0,1], bounds=(1,3))

Source1: https://github.com/scipy/scipy/issues/6340

Source2: My own testing and finding that the about github is not 100% accurate

Also, other recommendations about not using 0 as an ‘x’ value are great recommendations. Start your ‘x’ array with 1 to avoid divide by zero errors.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)