Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need helping using curve_fit on python to find optimal values for alpha and beta In [1]: from scipy.optimize import curve_fit from sklearn.metrics import mean_squared_error import

Need helping using curve_fit on python to find optimal values for alpha and beta

image text in transcribed

In [1]: from scipy.optimize import curve_fit from sklearn.metrics import mean_squared_error import numpy as np from scipy.integrate import odeint import matplotlib.pyplot as plt In [7]: infected = np.array([ 25, 75, 227, 296, 258, 236, 192, 126, 71, 28, 11, 7]) days = np.array([3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]) alpha = 0.3 beta = 0.0025 y0 = 738, 25, 2 def model(y, t, alpha, beta): S, I, R = y dsdt = -beta * I * S dIdt = beta * I * S - alpha * I dRdt = alpha * I return dsdt, didt, dRdt pend = odeint(func=model, y0 = (738, 25, 2), t=days, args=(alpha, beta)) S, I, R = pend.T In [9]: guess = (alpha, beta] c, cov = curve_fit(model, days, infected, guess [0], guess[1]) print(c) ValueError Traceback (most recent call last) ipython-input be4 2ca5> in 1 guess = [alpha, beta] ----> 2c, cov curve_fit (model, days, infected, guess [0], guess [1]) 3 print(c) -/opt/anaconda3/lib/python3.8/site-packages/scipy/optimize/minpack.py in curve_fit(f, xdata, ydata, po, sigma, absolu te sigma, check_finite, bounds, method, jac, **kwargs) 763 raise ValueError(""sigma" must be positive definite.") 764 else: --> 765 raise ValueError(""sigma" has incorrect shape.") 766 else: 767 transform = None ValueError: "sigma has incorrect shape. In [1]: from scipy.optimize import curve_fit from sklearn.metrics import mean_squared_error import numpy as np from scipy.integrate import odeint import matplotlib.pyplot as plt In [7]: infected = np.array([ 25, 75, 227, 296, 258, 236, 192, 126, 71, 28, 11, 7]) days = np.array([3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]) alpha = 0.3 beta = 0.0025 y0 = 738, 25, 2 def model(y, t, alpha, beta): S, I, R = y dsdt = -beta * I * S dIdt = beta * I * S - alpha * I dRdt = alpha * I return dsdt, didt, dRdt pend = odeint(func=model, y0 = (738, 25, 2), t=days, args=(alpha, beta)) S, I, R = pend.T In [9]: guess = (alpha, beta] c, cov = curve_fit(model, days, infected, guess [0], guess[1]) print(c) ValueError Traceback (most recent call last) ipython-input be4 2ca5> in 1 guess = [alpha, beta] ----> 2c, cov curve_fit (model, days, infected, guess [0], guess [1]) 3 print(c) -/opt/anaconda3/lib/python3.8/site-packages/scipy/optimize/minpack.py in curve_fit(f, xdata, ydata, po, sigma, absolu te sigma, check_finite, bounds, method, jac, **kwargs) 763 raise ValueError(""sigma" must be positive definite.") 764 else: --> 765 raise ValueError(""sigma" has incorrect shape.") 766 else: 767 transform = None ValueError: "sigma has incorrect shape

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

How can you help current or prospective customers with videos?

Answered: 1 week ago