Kaggle competition: COVID19 Global Forecasting (Week 3)
<SOS> metric: MCRMSLE Kaggle data source: covid19-global-forecasting-week-3<find_best_params>
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from itertools import cycle, islice
import seaborn as sb
import matplotlib.dates as dates
import datetime as dt
from sklearn import preprocessing
from xgboost import XGBRegressor
from lightgbm import LGBMRegressor
from sklearn.ensemble import RandomForestRegressor
from sklearn.preprocessing import StandardScaler
import plotly.graph_objects as go
import plotly_express as px
from sklearn.preprocessing import OrdinalEncoder
interp = ClassificationInterpretation.from_learner(learn)
 losses,idxs = interp.top_losses()
len(data.valid_ds)==len(losses)==len(idxs )<predict_on_test>
train = pd.read_csv("/kaggle/input/covid19-global-forecasting-week-3/train.csv")
 test = pd.read_csv("/kaggle/input/covid19-global-forecasting-week-3/test.csv")
 train.head()
test_preds = learn.get_preds(DatasetType.Test)
 test_df['predicted'] = test_preds[0].argmax(dim=1 )<save_to_csv>
train['Date'] = pd.to_datetime(train['Date'], format = '%Y-%m-%d')
 test['Date'] = pd.to_datetime(test['Date'], format = '%Y-%m-%d' )
csv_path ='/kaggle/working/submission.csv'
test_df.to_csv(csv_path, index=False )<set_options>
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
%matplotlib inline 
<load_from_csv>
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
dr = pd.read_csv(".. /input/RegularSeasonDetailedResults.csv")
<prepare_output>
columns = ['day','month','dayofweek','dayofyear','quarter','weekofyear','Province_State', 'Country_Region','ConfirmedCases','Fatalities']
df_train = df_train[columns]
simple_df_1 = pd.DataFrame()
simple_df_1[["team1", "team2"]] =dr[["Wteam", "Lteam"]].copy()
simple_df_1["pred"] = 1
simple_df_2 = pd.DataFrame()
simple_df_2[["team1", "team2"]] =dr[["Lteam", "Wteam"]]
simple_df_2["pred"] = 0
simple_df = pd.concat(( simple_df_1, simple_df_2), axis=0)
 simple_df.head() 
<count_unique_values>
df_test = categoricalToInteger(test)
 df_test = create_features(test)
 columns = ['day','month','dayofweek','dayofyear','quarter','weekofyear','Province_State', 'Country_Region']
n = simple_df.team1.nunique()
n 
<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 )
<normalization><EOS> 
df_submit = pd.DataFrame(submission)
 df_submit.to_csv(r'submission.csv', index=False )