text_plain_output_1.png
from fastai.tabular import add_datepart

from sklearn.preprocessing import OrdinalEncoder

import pandas as pd

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

df_train['Date'] = pd.to_datetime(df_train['Date'], format='%Y-%m-%d')
df_test['Date'] = pd.to_datetime(df_test['Date'], format='%Y-%m-%d')

def categoricalToInteger(df):
    df.Province_State.fillna('NaN', inplace=True)
    oe = OrdinalEncoder()
    df[['Province_State', 'Country_Region']] = oe.fit_transform(df.loc[:, ['Province_State', 'Country_Region']])
    return df

add_datepart(df_train, 'Date', drop=False)
df_train.drop('Elapsed', axis=1, inplace=True)

df_train = categoricalToInteger(df_train)

def lag_feature(df, lags, col):
    tmp = df[['Dayofyear', 'Country_Region', 'Province_State', col]]
    for i in lags:
        shifted = tmp.copy()
        shifted.columns = ['Dayofyear', 'Country_Region', 'Province_State', col + '_lag_' + str(i)]
        shifted['Dayofyear'] += i
        df = pd.merge(df, shifted, on=['Dayofyear', 'Country_Region', 'Province_State'], how='left')
    return df

df_train = lag_feature(df_train, [1, 2, 3, 6, 11], 'ConfirmedCases')

df_train = lag_feature(df_train, [1, 2, 3, 6, 11], 'Fatalities')

df_train.columns

lags = [1, 2, 3, 6, 11]
features = ['ConfirmedCases', 'Fatalities', 'Year', 'Month', 'Week', 'Day', 'Dayofweek', 'Dayofyear', 'Is_month_end', 'Is_month_start', 'Is_quarter_end', 'Is_quarter_start', 'Is_year_end', 'Is_year_start']
for lag in lags:
    features.append('ConfirmedCases_lag_' + str(lag))
    features.append('Fatalities_lag_' + str(lag))
corr_matrix = df_train[features].corr()
corr_matrix['ConfirmedCases'].sort_values(ascending=False)