Error-Optimal parameters not found: The maximum number of function evaluations is exceeded.

4,401 views
Skip to first unread message

Anusha Kodimela

unread,
Sep 4, 2018, 12:37:09 PM9/4/18
to lmfit-py
Hello Team!

My curvefitting code shows up an error while using Scipy.optimize.curvefit function.I feel this issue has started after I have inserted bounds for my initial guess.

please suggest what has to be done.Attached the code and error trace.

Code:
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import curve_fit

def ssexp_if(x, a, b, c):
arg = (b * x) ** c
g = a * np.exp(-2 * arg)
g[~np.isfinite(g)] = 0.0
return g


datafile = pd.read_excel(r'C:\Users\Trial12.xls', header=None)
x = datafile.iloc[ : , 0]
y = datafile.iloc[ : , 1]

first = 6 #change the index after inserting a heading to the excel
last = 90
index = 10
xdatam = x[first:last]
ydatam = y[first:last]
listfirst = 6
a = np.mean(ydatam[first+listfirst:first+10+listfirst])
b = (np.log(ydatam[first+listfirst]/ydatam[index+first+listfirst]))/(2*(xdatam[index+first+listfirst]-xdatam[first+listfirst]))
c = 1
print("a", a, "b", b, "c", c)
lb = [0, 0, 0]
ub = [100, 1e5, 1]
yfit = ssexp_if(x, a, b, c)
params, pcov = curve_fit(ssexp_if, x, yfit, bounds=(lb, ub))
print(params)
plt.semilogx(x, y, 'ko', label="Original Noised Data")
plt.semilogx(x, ssexp_if(x, *params), 'r-', label="Fitted Curve")
plt.legend()
plt.show()

Error:
C:/Users/CurveFitting_Python_UsingFun.py:8: RuntimeWarning: overflow encountered in exp
  g = a * np.exp(-2 * arg)
C:\Users\\PycharmProjects\CurveFitting_Python\venv\lib\site-packages\scipy\optimize\_lsq\trf.py:355: RuntimeWarning: invalid value encountered in double_scalars
  actual_reduction = cost - cost_new
Traceback (most recent call last):
  File "C:/Users/PycharmProjects/CurveFitting_Python/CurveFitting_Python_UsingFun.py", line 30, in <module>
    params, pcov = curve_fit(ssexp_if, x, yfit, bounds=[lb, ub])
  File "C:\Users\PycharmProjects\CurveFitting_Python\venv\lib\site-packages\scipy\optimize\minpack.py", line 765, in curve_fit
    raise RuntimeError("Optimal parameters not found: " + res.message)
RuntimeError: Optimal parameters not found: The maximum number of function evaluations is exceeded.

Process finished with exit code 1

Renee Otten

unread,
Sep 4, 2018, 1:29:22 PM9/4/18
to lmfi...@googlegroups.com
Anusha, 

presumably you’d be interested in using lmfit when posting a question in a Google Group about this Python package? If so, and as you’ve been told many times now by Matt as well, reading the manual and examples would be a good starting point. Or at least show us what you attempted actually using lmfit… this seems again like a rather random question.

The error message by the way is rather clear:  it didn’t find the optimal parameters within the number of specified function evaluations. If you want to know how to resolve that, I suggest the spicy.optimize.curve_fit manual (https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.curve_fit.html and for the keywords: https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.leastsq.html#scipy.optimize.leastsq).

Renee

--
You received this message because you are subscribed to the Google Groups "lmfit-py" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lmfit-py+u...@googlegroups.com.
To post to this group, send email to lmfi...@googlegroups.com.
Visit this group at https://groups.google.com/group/lmfit-py.
To view this discussion on the web visit https://groups.google.com/d/msgid/lmfit-py/3549247d-f446-4165-b734-da9758dcfa0d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Anusha Kodimela

unread,
Sep 4, 2018, 2:05:57 PM9/4/18
to lmfit-py
Hello Renee!

I have a different post for my Lmfit code and I am not sure if you have seen that before posting your reply.I have been building two different codes for curve fitting and this thread was related the error that I am facing in curvefit function.
Reply all
Reply to author
Forward
This conversation is locked
You cannot reply and perform actions on locked conversations.
0 new messages