Kaggle competition: COVID19 Global Forecasting (Week 4)
<SOS> metric: MCRMSLE Kaggle data source: covid19-global-forecasting-(week-4)<feature_engineering>
import numpy as np
import pandas as pd
import math
from sklearn.preprocessing import MinMaxScaler
import datetime as dt
from sklearn.preprocessing import OrdinalEncoder
from xgboost import XGBRegressor
import matplotlib.pyplot as plt
from sklearn.metrics import mean_squared_log_error
n=5
commits_df.loc[n,'commit_num'] = 7
commits_df.loc[n,'dropout_model'] = 0.36
commits_df.loc[n,'hidden_dim_first'] = 128
commits_df.loc[n,'hidden_dim_second'] = 192
commits_df.loc[n,'hidden_dim_third'] = 128
commits_df.loc[n,'LB_score'] = 0.25877<feature_engineering>
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" )
n=6
commits_df.loc[n,'commit_num'] = 8
commits_df.loc[n,'dropout_model'] = 0.36
commits_df.loc[n,'hidden_dim_first'] = 128
commits_df.loc[n,'hidden_dim_second'] = 224
commits_df.loc[n,'hidden_dim_third'] = 128
commits_df.loc[n,'LB_score'] = 0.25868<feature_engineering>
train['Date'] = pd.to_datetime(train['Date'], format = '%Y-%m-%d')
 test['Date'] = pd.to_datetime(test['Date'], format = '%Y-%m-%d' )
n=7
commits_df.loc[n,'commit_num'] = 9
commits_df.loc[n,'dropout_model'] = 0.36
commits_df.loc[n,'hidden_dim_first'] = 128
commits_df.loc[n,'hidden_dim_second'] = 288
commits_df.loc[n,'hidden_dim_third'] = 128
commits_df.loc[n,'LB_score'] = 0.25881<feature_engineering>
def create_features(df):
df['day'] = df['Date'].dt.day
df['month'] = df['Date'].dt.month
df['dayofweek'] = df['Date'].dt.dayofweek
df['dayofyear'] = df['Date'].dt.dayofyear
df['quarter'] = df['Date'].dt.quarter
df['weekofyear'] = df['Date'].dt.weekofyear
return df
def train_dev_split(df, days):
date = df['Date'].max() - dt.timedelta(days=days)
 return df[df['Date'] <= date], df[df['Date'] > date]
def categoricalToInteger(df):
df.Province_State.fillna('None', inplace=True)
 oe = OrdinalEncoder()
df[['Province_State','Country_Region']] = oe.fit_transform(df.loc[:,['Province_State','Country_Region']])
 return df
n=8
commits_df.loc[n,'commit_num'] = 10
commits_df.loc[n,'dropout_model'] = 0.36
commits_df.loc[n,'hidden_dim_first'] = 128
commits_df.loc[n,'hidden_dim_second'] = 248
commits_df.loc[n,'hidden_dim_third'] = 128
commits_df.loc[n,'LB_score'] = 0.25850<feature_engineering>
df_train, df_dev = train_dev_split(df_train,0)
 columns = ['day','month','dayofweek','dayofyear','quarter','weekofyear','Province_State', 'Country_Region','ConfirmedCases','Fatalities']
df_train = df_train[columns]
df_dev = df_dev[columns]
n=9
commits_df.loc[n,'commit_num'] = 11
commits_df.loc[n,'dropout_model'] = 0.36
commits_df.loc[n,'hidden_dim_first'] = 128
commits_df.loc[n,'hidden_dim_second'] = 240
commits_df.loc[n,'hidden_dim_third'] = 128
commits_df.loc[n,'LB_score'] = 0.25868<feature_engineering>
df_test = categoricalToInteger(test)
 df_test = create_features(test)
 columns = ['day','month','dayofweek','dayofyear','quarter','weekofyear','Province_State', 'Country_Region']
n=10
commits_df.loc[n,'commit_num'] = 12
commits_df.loc[n,'dropout_model'] = 0.35
commits_df.loc[n,'hidden_dim_first'] = 128
commits_df.loc[n,'hidden_dim_second'] = 248
commits_df.loc[n,'hidden_dim_third'] = 128
commits_df.loc[n,'LB_score'] = 0.25884<feature_engineering>
def XGB() :
model = XGBRegressor(n_estimators=1300)
 return model
n=11
commits_df.loc[n,'commit_num'] = 13
commits_df.loc[n,'dropout_model'] = 0.3
commits_df.loc[n,'hidden_dim_first'] = 128
commits_df.loc[n,'hidden_dim_second'] = 248
commits_df.loc[n,'hidden_dim_third'] = 128
commits_df.loc[n,'LB_score'] = 0.25879<feature_engineering>
submission = []
for country in df_train.Country_Region.unique() :
df_train1 = df_train[df_train["Country_Region"]==country]
for state in df_train1.Province_State.unique() :
df_train2 = df_train1[df_train1["Province_State"]==state]
train = df_train2.values
X_train, y_train = train[:,:-2], train[:,-2:]
model1 = XGBRegressor(n_estimators=1100)
 model1.fit(X_train, y_train[:,0])
 model2 = XGBRegressor(n_estimators=1100)
 model2.fit(X_train, y_train[:,1])
 df_test1 = df_test[(df_test["Country_Region"]==country)&(df_test["Province_State"] == state)]
ForecastId = df_test1.ForecastId.values
df_test2 = df_test1[columns]
y_pred1 = np.round(model1.predict(df_test2.values),5)
 y_pred2 = np.round(model2.predict(df_test2.values),5)
 for i in range(len(y_pred1)) :
d = {'ForecastId':ForecastId[i], 'ConfirmedCases':y_pred1[i], 'Fatalities':y_pred2[i]}
submission.append(d )
<feature_engineering><EOS> 
df_submit = pd.DataFrame(submission)
 df_submit.to_csv(r'/submission.csv', index=False )