Python Forum
RuntimeError: Optimal parameters not found: Number of calls to function has reached m
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
RuntimeError: Optimal parameters not found: Number of calls to function has reached m
#1
population = float(46750238)
country_df = pd.DataFrame()
country_df['ConfirmedCases'] = train.loc[train['Country_Region']=='Spain'].ConfirmedCases.diff().fillna(0)
country_df = country_df[10:]
country_df['day_count'] = list(range(1,len(country_df)+1))

ydata = [i for i in country_df.ConfirmedCases]
xdata = country_df.day_count
ydata = np.array(ydata, dtype=float)
xdata = np.array(xdata, dtype=float)

N = population
inf0 = ydata[0]
sus0 = N - inf0
rec0 = 0.0

def sir_model(y, x, beta, gamma):
    sus = -beta * y[0] * y[1] / N
    rec = gamma * y[1]
    inf = -(sus + rec)
    return sus, inf, rec

def fit_odeint(x, beta, gamma):
    return integrate.odeint(sir_model, (sus0, inf0, rec0), x, args=(beta, gamma))[:,1]

popt, pcov = optimize.curve_fit(fit_odeint, xdata, ydata)
fitted = fit_odeint(xdata, *popt)

plt.plot(xdata, ydata, 'o')
plt.plot(xdata, fitted)
plt.title("Fit of SIR model for Spain infected cases")
plt.ylabel("Population infected")
plt.xlabel("Days")
plt.show()
print("Optimal parameters: beta =", popt[0], " and gamma = ", popt[1])
Error:
RuntimeError Traceback (most recent call last) <ipython-input-60-01ea0e369b77> in <module> 24 return integrate.odeint(sir_model, (sus0, inf0, rec0), x, args=(beta, gamma))[:,1] 25 ---> 26 popt, pcov = optimize.curve_fit(fit_odeint, xdata, ydata) 27 fitted = fit_odeint(xdata, *popt) 28 C:\ProgramData\Anaconda3\lib\site-packages\scipy\optimize\minpack.py in curve_fit(f, xdata, ydata, p0, sigma, absolute_sigma, check_finite, bounds, method, jac, **kwargs) 746 cost = np.sum(infodict['fvec'] ** 2) 747 if ier not in [1, 2, 3, 4]: --> 748 raise RuntimeError("Optimal parameters not found: " + errmsg) 749 else: 750 # Rename maxfev (leastsq) to max_nfev (least_squares), if specified. RuntimeError: Optimal parameters not found: Number of calls to function has reached maxfev = 600. ​
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  capturing multiline output for number of parameters jss 3 766 Sep-01-2023, 05:42 PM
Last Post: jss
Question How to compare two parameters in a function that has *args? Milan 4 1,183 Mar-26-2023, 07:43 PM
Last Post: Milan
  help RuntimeError: no running event loop marpaslight 5 3,614 Oct-18-2022, 10:04 PM
Last Post: marpaslight
  function accepts infinite parameters and returns a graph with those values edencthompson 0 812 Jun-10-2022, 03:42 PM
Last Post: edencthompson
  Optimal way to search partial correspondence in a large dict genny92c 0 974 Apr-22-2022, 10:20 AM
Last Post: genny92c
  asyncio calls within sync function ( Websocket on_open) orion67 0 1,374 Jan-16-2022, 11:00 AM
Last Post: orion67
  bleak library RuntimeError: This event loop is already running alice93 3 4,026 Sep-30-2021, 08:06 AM
Last Post: alice93
  Checking the number of arguments a function takes Chirumer 3 2,111 Jul-06-2021, 04:56 PM
Last Post: Chirumer
  Calls to Attributes of a Class SKarimi 3 3,336 Apr-22-2021, 04:18 PM
Last Post: SKarimi
  RuntimeError: generator raised StopIteration quest 1 5,750 Mar-28-2021, 08:11 PM
Last Post: quest

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020