text_plain_output_1.png image_output_1.png
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.0 + 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.0 + np.exp(-xp)))
        else:
            result.append(L * np.exp(xp) / (1.0 + 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_)
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()