Python Forum
SIR Model: Saving predicted value
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
SIR Model: Saving predicted value
#1
Hello!

I am experimenting with SIR model for spread of disease using the following code. In particular, at each iteration I feed in different N, I0, and R0 values in the model from the CSV file. For each set of parameters, the model makes predictions for S, I, and R outcomes 1-75 days ahead. Usually, I make a plot and examine the result visually, but how can I record the predicted value for "I" parameter after each iteration in a 7-day time and store it as a separate column in the CSV file?

Your help would be greatly appreciated.

import pandas as pd
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt

df = pd.read_csv('params_temp.csv',  encoding="utf-8")

for n, i, r in zip(df["N"], df["I0"], df["R0"]):
    N = n
    I0, R0 = i, r
    S0 = N - I0 - R0
    beta, gamma = 0.5, 0.05
    t = np.linspace(0, 75, 75) # Days ahead

    def deriv(y, t, N, beta, gamma):
        S, I, R = y
        dSdt = -beta * S * I / N
        dIdt = beta * S * I / N - gamma * I
        dRdt = gamma * I
        return dSdt, dIdt, dRdt

    y0 = S0, I0, R0
    ret = odeint(deriv, y0, t, args=(N, beta, gamma))
    S, I, R = ret.T
Reply
#2
Say, I do:

element=I[6]
print(element)
Then how do I save the result in a column in a table? I have no clue :( Feedback is greatly appreciated.
Reply
#3
Okay, say I do:

import pandas as pd
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt

df = pd.read_csv('params_temp.csv', index_col = 'FIPS', encoding="utf-8”)

for n, i, r in zip(df["N"], df["I0"], df["R0"]):
    N = n
    I0, R0 = i, r
    S0 = N - I0 - R0
    beta, gamma = 0.4, 1./15 
    t = np.linspace(0, 160, 160)

    def deriv(y, t, N, beta, gamma):
        S, I, R = y
        dSdt = -beta * S * I / N
        dIdt = beta * S * I / N - gamma * I
        dRdt = gamma * I
        return dSdt, dIdt, dRdt

    y0 = S0, I0, R0
    ret = odeint(deriv, y0, t, args=(N, beta, gamma))
    S, I, R = ret.T
   
    element=I[6]
    df['pred'] = element
    df.to_csv('params_temp.csv')
But it saves the same value (guess the first predicted "I" for the first FIPS) value across all FIPS in the CSV. What am I doing wrong?..

Trying another approach:

vals=[]

for n, i, r in zip(df["N"], df["I0"], df["R0"]):
    N = n
    I0, R0 = i, r
    S0 = N - I0 - R0
    beta, gamma = 0.4, 1./15 
    t = np.linspace(0, 160, 160)

    def deriv(y, t, N, beta, gamma):
        S, I, R = y
        dSdt = -beta * S * I / N
        dIdt = beta * S * I / N - gamma * I
        dRdt = gamma * I
        return dSdt, dIdt, dRdt

    y0 = S0, I0, R0
    ret = odeint(deriv, y0, t, args=(N, beta, gamma))
    S, I, R = ret.T
    
    vals.append(element)
    
vals
[99.43974077754876,
 99.43974077754876,
 99.43974077754876,
 99.43974077754876,
 99.43974077754876,
 99.43974077754876]
And I still get repeated values :(( What a frustration...
Reply
#4
The solution is:
vals=[]

for n, i, r in zip(df["N"], df["I0"], df["R0"]):
    N = n
    I0, R0 = i, r
    S0 = N - I0 - R0
    beta, gamma = 0.4, 1./15 
    t = np.linspace(0, 160, 160)

    def deriv(y, t, N, beta, gamma):
        S, I, R = y
        dSdt = -beta * S * I / N
        dIdt = beta * S * I / N - gamma * I
        dRdt = gamma * I
        return dSdt, dIdt, dRdt

    y0 = S0, I0, R0
    ret = odeint(deriv, y0, t, args=(N, beta, gamma))
    S, I, R = ret.T
    element=I[6]
    vals.append(element)
    
vals
Thanks everybody for help!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  model.fit and model.predict errors hatflyer 6 1,308 Nov-10-2023, 01:39 AM
Last Post: hatflyer
  FileNotFoundError: [Errno 2] No such file or directory: 'model/doc2vec.model/Articles Anldra12 10 5,773 Jun-11-2021, 04:48 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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