我用Python实现了SIR模型,结果似乎是正确的。
import scipy.integrate
import numpy
import matplotlib.pyplot as plt
def SIR_model(y,t,N,beta,gamma):
S,I,R=y
dS_dt=-beta*S*I/N
dI_dt=beta*S*I/N-gamma*I
dR_dt=gamma*I
return([dS_dt,dI_dt,dR_dt])
N=1000
I0=1
R0=0.0
S0=N-I0-R0
beta=0.2
gamma=0.1
t=numpy.linspace(0,160,160)
sol=scipy.integrate.odeint(SIR_model,[S0,I0,R0],t,args=(N, beta,gamma))
sol=numpy.array(sol)
plt.figure(figsize=[6,4])
plt.plot(t,sol[:,0],label="S(t)")
plt.plot(t,sol[:,1],label="I(t)")
plt.plot(t,sol[:,2],label="R(t)")
plt.grid()
plt.legend()
plt.xlabel("Time")
plt.ylabel("Number")
plt.title("SIR model")
plt.show()
现在我想用这个模型来拟合真实数据( https:/github.compcm-dpcCOVID-19。 ). 我知道 N0
, S0
, I0
, R0
,我需要找到最佳的值为 beta
和 gamma
. 在我以前的项目中,我在定义好的函数上拟合数据(我知道分析表达式),但现在我不知道如何用集成系统的结果来做。我通常使用Numpy curve_fit
的功能。
popt, pcov = curve_fit(f, x_list, y_list, sigma=y_err)
最好的方法是什么?几天前在github上有个不错的项目,但现在作者把它删了,我用Python实现了SIR模型,结果似乎是正确的: import scipy.integration import numpy import matplotlib.pyplot as plt def SIR模型。