text_html_output_1.png
import datetime as dt

import matplotlib.pyplot as plt

import numpy as np

import pandas as pd

import scipy.optimize as opt

train = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-4/train.csv')
test = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-4/test.csv')

train_ = train[train['ConfirmedCases'] >= 0]

EMPTY_VAL = 'EMPTY_VAL'
def fillState(state, country):
    if state == EMPTY_VAL:
        return country
    return state
train_['Province_State'].fillna(EMPTY_VAL, inplace=True)
train_['Province_State'] = train_.loc[:, ['Province_State', 'Country_Region']].apply(lambda x: fillState(x['Province_State'], x['Country_Region']), axis=1)
test['Province_State'].fillna(EMPTY_VAL, inplace=True)
test['Province_State'] = test.loc[:, ['Province_State', 'Country_Region']].apply(lambda x: fillState(x['Province_State'], x['Country_Region']), axis=1)

train_['row_number'] = train_.groupby(['Country_Region', 'Province_State']).cumcount()
x = train_[train_["Country_Region"] == 'US'][train_["Province_State"] == 'New York']['row_number']
y = train_[train_["Country_Region"] == 'US'][train_["Province_State"] == 'New York']['ConfirmedCases']
y_ = train_[train_["Country_Region"] == 'US'][train_["Province_State"] == 'New York']['Fatalities']

def f(x, L, b, k, x_0):
    return L / (1. + np.exp(-k * (x - x_0))) + b

def logistic(xs, L, k, x_0):
    result = []
    for x in xs:
        xp = k*(x-x_0)
        if xp >= 0:
            result.append(L / ( 1. + np.exp(-xp) ) )
        else:
            result.append(L * np.exp(xp) / ( 1. + np.exp(xp) ) )
    return result

p0 = [max(y), 0.0,max(x)]
p0_ = [max(y_), 0.0,max(x)]
x_ = np.arange(0, 100, 1).tolist()
try:
    popt, pcov = opt.curve_fit(logistic, x, y,p0)
    yfit = logistic(x_, *popt)
    popt_, pcov_ = opt.curve_fit(logistic, x, y_,p0_)
    yfit_ = logistic(x_, *popt_)
except:
    popt, pcov = opt.curve_fit(f, x, y, method="lm", maxfev=5000)
    yfit = f(x_, *popt)
    popt_, pcov_ = opt.curve_fit(f, x, y_, method="lm", maxfev=5000)
    yfit_ = f(x_, *popt_)
    #print("problem")

fig, ax = plt.subplots(1, 1, figsize=(10, 8))
ax.plot(x, y, 'o', label ='Actual Cases')
ax.plot(x_, yfit, '-', label ='Fitted Cases')

ax.plot(x, y_, 'o', label ='Actual Fatalities')
ax.plot(x_, yfit_, '-', label ='Fitted fatalities')
ax.title.set_text('US - New York')
plt.legend(loc="center right")
plt.show()

unique = pd.DataFrame(train_.groupby(['Country_Region', 'Province_State'], as_index=False).count())

import datetime as dt
def date_day_diff(d1, d2):
    delta = dt.datetime.strptime(d1, '%Y-%m-%d') - dt.datetime.strptime(d2, '%Y-%m-%d')
    return delta.days
log_regions = []
for index, region in unique.iterrows():
    st = region['Province_State']
    co = region['Country_Region']
    rdata = train_[(train_['Province_State'] == st) & (train_['Country_Region'] == co)]
    t = rdata['Date'].values
    t = [float(date_day_diff(d, t[0])) for d in t]
    y = rdata['ConfirmedCases'].values
    y_ = rdata['Fatalities'].values
    p0 = [max(y), 0.0, max(t)]
    p0_ = [max(y_), 0.0, max(t)]
    try:
        popt, pcov = opt.curve_fit(logistic, t, y, p0, maxfev=10000)
        try:
            popt_, pcov_ = opt.curve_fit(logistic, t, y_, p0_, maxfev=10000)
        except:
            popt_, pcov_ = opt.curve_fit(f, t, y_, method='trf', maxfev=10000)
        log_regions.append((co, st, popt, popt_))
    except:
        popt, pcov = opt.curve_fit(f, t, y, method='trf', maxfev=10000)
        popt_, pcov_ = opt.curve_fit(f, t, y_, method='trf', maxfev=10000)
        log_regions.append((co, st, popt, popt_))
print('All done!')