text_plain_output_1.png
from sklearn.linear_model import ElasticNet

from sklearn.metrics import mean_squared_error

from sklearn.model_selection import RandomizedSearchCV

from sklearn.model_selection import train_test_split

from sklearn.utils import shuffle

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

df = 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')

df['Province_State'].fillna('state', inplace=True)
df['Country_Region'] = [country_name.replace("'", '') for country_name in df['Country_Region']]

data = []
countries = df.Country_Region.unique()
for country in countries:
    provinces = df[df.Country_Region == country].Province_State.unique()
    for province in provinces:
        temp_df = df[(df['Country_Region'] == country) & (df['Province_State'] == province)]
        for i in range(0, 77):
            Iday1 = float(temp_df.iloc[i].ConfirmedCases)
            Iday2 = float(temp_df.iloc[i + 1].ConfirmedCases)
            Iday3 = float(temp_df.iloc[i + 2].ConfirmedCases)
            Iday4 = float(temp_df.iloc[i + 3].ConfirmedCases)
            Iday5 = float(temp_df.iloc[i + 4].ConfirmedCases)
            Iday6 = float(temp_df.iloc[i + 5].ConfirmedCases)
            Iday7 = float(temp_df.iloc[i + 6].ConfirmedCases)
            Fday1 = float(temp_df.iloc[i].Fatalities)
            Fday2 = float(temp_df.iloc[i + 1].Fatalities)
            Fday3 = float(temp_df.iloc[i + 2].Fatalities)
            Fday4 = float(temp_df.iloc[i + 3].Fatalities)
            Fday5 = float(temp_df.iloc[i + 4].Fatalities)
            Fday6 = float(temp_df.iloc[i + 5].Fatalities)
            Fday7 = float(temp_df.iloc[i + 6].Fatalities)
            target_infection = float(temp_df.iloc[i + 7].ConfirmedCases)
            target_fatal = float(temp_df.iloc[i + 7].Fatalities)
            data.append({'Iday1': Iday1, 'Iday2': Iday2, 'Iday3': Iday3, 'Iday4': Iday4, 'Iday5': Iday5, 'Iday6': Iday6, 'Iday7': Iday7, 'Fday1': Fday1, 'Fday2': Fday2, 'Fday3': Fday3, 'Fday4': Fday4, 'Fday5': Fday5, 'Fday6': Fday6, 'Fday7': Fday7, 'target_infection': target_infection, 'target_fatal': target_fatal})

new_data = pd.DataFrame(data)

X_y = shuffle(new_data)
y_cases = X_y['target_infection']
y_fatal = X_y['target_fatal']
X = X_y.drop(['target_infection', 'target_fatal'], axis=1)
X_train_cases, X_test_cases, y_train_cases, y_test_cases = train_test_split(X, y_cases, test_size=0.33)
X_train_fatal, X_test_fatal, y_train_fatal, y_test_fatal = train_test_split(X, y_fatal, test_size=0.33)

reg_case = ElasticNet(random_state=42, l1_ratio=0.1, max_iter=2700)
params = [{'alpha': [10 ** (-4), 10 ** (-3), 10 ** (-2), 10 ** (-1), 10 ** 0, 10 ** 1, 10 ** 2, 10 ** 3, 10 ** 4]}]
clf = RandomizedSearchCV(reg_case, params, cv=5, scoring='neg_root_mean_squared_error', return_train_score=True)
search = clf.fit(X_train_cases, y_train_cases)
results = pd.DataFrame.from_dict(clf.cv_results_)

best_alpha = 1000
best_itr = 2700
final_reg_case = ElasticNet(random_state=42, alpha=best_alpha, l1_ratio=0.1, max_iter=best_itr)
final_reg_case.fit(X_train_cases, y_train_cases)

reg_fatal = ElasticNet(random_state=42, l1_ratio=0.1, max_iter=3800)
params = [{'alpha': [10 ** (-4), 10 ** (-3), 10 ** (-2), 10 ** (-1), 10 ** 0, 10 ** 1, 10 ** 2, 10 ** 3, 10 ** 4]}]
clf = RandomizedSearchCV(reg_fatal, params, cv=5, scoring='neg_root_mean_squared_error', return_train_score=True)
search = clf.fit(X_train_fatal, y_train_fatal)
results = pd.DataFrame.from_dict(clf.cv_results_)

best_alpha = 100
best_iter = 3800
final_reg_fatal = ElasticNet(random_state=42, alpha=best_alpha, l1_ratio=0.1, max_iter=best_iter)
final_reg_fatal.fit(X_train_fatal, y_train_fatal)

data = []
countries = df.Country_Region.unique()
for country in countries:
    provinces = df[df.Country_Region == country].Province_State.unique()
    for province in provinces:
        temp_df = df[(df['Country_Region'] == country) & (df['Province_State'] == province)]
        for i in range(0, 77):
            Iday1 = float(temp_df.iloc[i].ConfirmedCases)
            Iday2 = float(temp_df.iloc[i + 1].ConfirmedCases)
            Iday3 = float(temp_df.iloc[i + 2].ConfirmedCases)
            Iday4 = float(temp_df.iloc[i + 3].ConfirmedCases)
            Iday5 = float(temp_df.iloc[i + 4].ConfirmedCases)
            Iday6 = float(temp_df.iloc[i + 5].ConfirmedCases)
            Iday7 = float(temp_df.iloc[i + 6].ConfirmedCases)
            Fday1 = float(temp_df.iloc[i].Fatalities)
            Fday2 = float(temp_df.iloc[i + 1].Fatalities)
            Fday3 = float(temp_df.iloc[i + 2].Fatalities)
            Fday4 = float(temp_df.iloc[i + 3].Fatalities)
            Fday5 = float(temp_df.iloc[i + 4].Fatalities)
            Fday6 = float(temp_df.iloc[i + 5].Fatalities)
            Fday7 = float(temp_df.iloc[i + 6].Fatalities)
            if Iday6 == 0:
                iavg = 1
            else:
                iavg = Iday7 / Iday6
            if Fday6 == 0:
                favg = 1
            else:
                favg = Fday7 / Fday6
            target_infection = float(temp_df.iloc[i + 7].ConfirmedCases)
            target_fatal = float(temp_df.iloc[i + 7].Fatalities)
            data.append({'Iday1': Iday1, 'Iday2': Iday2, 'Iday3': Iday3, 'Iday4': Iday4, 'Iday5': Iday5, 'Iday6': Iday6, 'Iday7': Iday7, 'Fday1': Fday1, 'Fday2': Fday2, 'Fday3': Fday3, 'Fday4': Fday4, 'Fday5': Fday5, 'Fday6': Fday6, 'Fday7': Fday7, 'iratio': iavg, 'fratio': favg, 'target_infection': target_infection, 'target_fatal': target_fatal})

featured = pd.DataFrame(data)
X_y_f = shuffle(featured)
y_cases_f = X_y_f['target_infection']
y_fatal_f = X_y_f['target_fatal']
X_f = X_y_f.drop(['target_infection', 'target_fatal'], axis=1)
X_train_cases_f, X_test_cases_f, y_train_cases_f, y_test_cases_f = train_test_split(X_f, y_cases_f, test_size=0.33)
X_train_fatal_f, X_test_fatal_f, y_train_fatal_f, y_test_fatal_f = train_test_split(X_f, y_fatal_f, test_size=0.33)

best_alpha = 10000
best_itr = 4500
final_reg_case_f = ElasticNet(random_state=42, alpha=best_alpha, l1_ratio=0.1, max_iter=best_itr)
final_reg_case_f.fit(X_train_cases_f, y_train_cases_f)

pred_f = final_reg_case_f.predict(X_test_cases_f)

best_alpha = 100
best_itr = 2700
final_reg_fatal_f = ElasticNet(random_state=42, alpha=best_alpha, l1_ratio=0.1, max_iter=best_itr)
final_reg_fatal_f.fit(X_train_fatal_f, y_train_fatal_f)

pred_f = final_reg_fatal_f.predict(X_test_fatal_f)
print('RMSE is:', mean_squared_error(y_test_fatal_f, pred_f) ** 0.5)