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
  function accepts infinite parameters and returns a graph with those values edencthompson 0 286 Jun-10-2022, 03:42 PM
Last Post: edencthompson
  Optimal way to search partial correspondence in a large dict genny92c 0 328 Apr-22-2022, 10:20 AM
Last Post: genny92c
  asyncio calls within sync function ( Websocket on_open) orion67 0 502 Jan-16-2022, 11:00 AM
Last Post: orion67
  bleak library RuntimeError: This event loop is already running alice93 3 2,191 Sep-30-2021, 08:06 AM
Last Post: alice93
  Checking the number of arguments a function takes Chirumer 3 1,221 Jul-06-2021, 04:56 PM
Last Post: Chirumer
  Calls to Attributes of a Class SKarimi 3 2,247 Apr-22-2021, 04:18 PM
Last Post: SKarimi
  RuntimeError: generator raised StopIteration quest 1 4,062 Mar-28-2021, 08:11 PM
Last Post: quest
  How can I write a function with three parameters? MehmetAliKarabulut 1 1,501 Mar-04-2021, 10:47 PM
Last Post: Larz60+
  is there a single function to confine a number between two others? Skaperen 7 1,760 Nov-28-2020, 06:10 PM
Last Post: Skaperen
  How can I found how many numbers are there in a Collatz Sequence that I found? cananb 2 1,551 Nov-23-2020, 05:15 PM
Last Post: cananb

Forum Jump:

User Panel Messages

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