Kaggle competition: FIA ML T5
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" )<feature_engineering>
import pandas as pd
import numpy as np
import seaborn as sns
import missingno as msno
import gc
train["Province_State"] = train["Province_State"].fillna('')
 test["Province_State"] = test["Province_State"].fillna('')
 train["Month"], train["Day"] = 0, 0
for i in range(len(train)) :
train["Month"][i] =(train["Date"][i] ).split("-")[1]
train["Day"][i] =(train["Date"][i] ).split("-")[2]
test["Month"], test["Day"] = 0, 0
for i in range(len(test)) :
test["Month"][i] =(test["Date"][i] ).split("-")[1]
test["Day"][i] =(test["Date"][i] ).split("-")[2]<drop_column>
%matplotlib inline
set_matplotlib_formats('pdf', 'png')
 pd.options.display.float_format = '{:.2f}'.format
rc={'savefig.dpi': 75, 'figure.autolayout': False, 'figure.figsize': [12, 8], 'axes.labelsize': 18,\
'axes.titlesize': 18, 'font.size': 18, 'lines.linewidth': 2.0, 'lines.markersize': 8, 'legend.fontsize': 16,\
'xtick.labelsize': 16, 'ytick.labelsize': 16}
sns.set(style='dark',rc=rc )
for i in range(len(train)) :
if train["Province_State"][i] != '':
train["Country_Region"][i] = train["Province_State"][i] + "(" + str(train["Country_Region"][i])+ ")"
for i in range(len(test)) :
if test["Province_State"][i] != '':
test["Country_Region"][i] = test["Province_State"][i] + "(" + str(test["Country_Region"][i])+ ")"
train.drop(columns = "Province_State", inplace=True)
 test.drop(columns = "Province_State", inplace=True)
 train.rename(columns = {"Country_Region" : "Country/State"}, inplace=True)
 test.rename(columns = {"Country_Region" : "Country/State"}, inplace=True)
<feature_engineering>
default_color = '
colormap = plt.cm.cool
i = 0
for value in train["Country/State"].unique() :
if i < len(train):
j = 1
while(train["Country/State"][i] == value):
train["Day"][i] = j
j += 1; i += 1
if i == len(train):
break
i = 0
for value in test["Country/State"].unique() :
if i < len(test):
j = 72
while(test["Country/State"][i] == value):
test["Day"][i] = j
j += 1; i += 1
if i == len(test):
break
train.rename(columns = {"Day" : "No.of days"}, inplace = True)
 test.rename(columns = {"Day" : "No.of days"}, inplace = True )<feature_engineering>
path = '.. /input/'
path_result = '.. /output/'
train["Country/State"].loc[train["Country/State"] == "Taiwan*"] = "Taiwan"
test["Country/State"].loc[test["Country/State"] == "Taiwan*"] = "Taiwan"
train = train.drop(columns = ["Date"])
 test = test.drop(columns = ["Date"])
 countriesorstates = train["Country/State"].unique()
print(countriesorstates )<prepare_x_and_y>
train = pd.read_csv(path + 'train_data.csv')
 test = pd.read_csv(path + 'teste_data.csv')
 train = train.rename(columns={"default": "target", "ids":"id"})
 test = test.rename(columns={"ids":"id"} )
random_picks = random.choices(countriesorstates, k = 5)
 for value in random_picks:
train_temp = train.loc[train["Country/State"] == value]
test_temp = test.loc[test["Country/State"] == value]
train_temp_cc = train_temp["ConfirmedCases"].loc[train["Country/State"] == value]
train_temp_ft = train_temp["Fatalities"].loc[train["Country/State"] == value]
train_temp_X = train_temp.iloc[:, 5]
test_temp_X = test_temp.iloc[:, 3]
x = train_temp_X.to_numpy(dtype = float)
 y_1 = train_temp_cc.to_numpy(dtype = float)
 y_2= train_temp_ft.to_numpy(dtype = float)
 x = x.reshape(-1,)
 plt.plot(x, y_1, color ='red', label = value)
 plt.xlabel("No.of days")
 plt.ylabel('Confirmed cases')
 plt.legend()
plt.title("Place: " + value)
 plt.show()
plt.plot(x, y_2, color ='black', label = value)
 plt.xlabel("No.of days")
 plt.ylabel('Fatalities')
 plt.legend()
plt.title("Place: " + value)
 plt.show()<create_dataframe>
test_id = test.id
poly_reg_cc = PolynomialFeatures(degree = 4)
 poly_reg_ft = PolynomialFeatures(degree = 4)
 reg_cc = LinearRegression()
reg_ft = LinearRegression()
sub = pd.DataFrame({'ForecastId': [], 'ConfirmedCases': [], 'Fatalities': []})
 for value in countriesorstates:
train_temp = train.loc[train["Country/State"] == value]
test_temp = test.loc[test["Country/State"] == value]
train_temp_cc = train_temp["ConfirmedCases"].loc[train["Country/State"] == value].to_frame()
train_temp_ft = train_temp["Fatalities"].loc[train["Country/State"] == value].to_frame()
train_temp_X = train_temp.iloc[:, 4:6]
test_temp_X = test_temp.iloc[:, 2:4]
sc1 = StandardScaler()
train_temp_X = sc1.fit_transform(train_temp_X)
 test_temp_X = sc1.transform(test_temp_X)
 sc_cc = StandardScaler()
sc_ft = StandardScaler()
train_temp_cc = sc_cc.fit_transform(train_temp_cc)
 train_temp_ft = sc_ft.fit_transform(train_temp_ft)
 X_poly = poly_reg_cc.fit_transform(train_temp_X)
 reg_cc.fit(X_poly, train_temp_cc)
 test_cc = sc_cc.inverse_transform(reg_cc.predict(poly_reg_cc.fit_transform(test_temp_X)))
 X_poly = poly_reg_ft.fit_transform(train_temp_X)
 reg_ft.fit(X_poly, train_temp_ft)
 test_ft = sc_ft.inverse_transform(reg_ft.predict(poly_reg_ft.fit_transform(test_temp_X)))
 a = int(train["No.of days"].loc[train["Country/State"] == "India"].max())
 b = int(a - test_temp["No.of days"].min())
 test_cc[0:b+1] = sc_cc.inverse_transform(train_temp_cc)[(a-b-1):(a)]
test_ft[0:b+1] = sc_ft.inverse_transform(train_temp_ft)[(a-b-1):(a)]
test_cc = test_cc.flatten()
test_ft = test_ft.flatten()
sub_temp = pd.DataFrame({'ForecastId': test_temp["ForecastId"].loc[test["Country/State"] == value],
'ConfirmedCases': test_cc, 'Fatalities': test_ft})
 sub = pd.concat([sub, sub_temp], axis = 0 )<save_to_csv>
def missing_values_table(df):
mis_val = df.isnull().sum()
mis_val_percent = 100 * df.isnull().sum() / len(df)
 mis_val_table = pd.concat([mis_val, mis_val_percent], axis=1)
 mis_val_table_ren_columns = mis_val_table.rename(
 columns = {0 : 'Missing Values', 1 : '% of Total Values'})
 mis_val_table_ren_columns = mis_val_table_ren_columns[
mis_val_table_ren_columns.iloc[:,1] != 0].sort_values(
 '% of Total Values', ascending=False ).round(1)
 print("Your selected dataframe has " + str(df.shape[1])+ " columns.
 "
"There are " + str(mis_val_table_ren_columns.shape[0])+
" columns that have missing values.")
 return mis_val_table_ren_columns
sub.ForecastId = sub.ForecastId.astype('int')
 for i in range(len(sub)) :
sub["ConfirmedCases"][i] = int(round(sub["ConfirmedCases"][i]))
sub["Fatalities"][i] = int(round(sub["Fatalities"][i]))
sub.to_csv("submission.csv", index = False )<define_variables>
missing_values_table(train )
week = 4<import_modules>
missingValueColumns = train.columns[train.isnull().any() ].tolist()
df_null = train[missingValueColumns]
import numpy as np
import pandas as pd
import datetime
import os
import matplotlib.pyplot as plt<feature_engineering>
train = train.dropna(subset=['target'] )
train = pd.read_csv(f"/kaggle/input/covid19-global-forecasting-week-{week}/train.csv")
 train.loc[:, "geo"] = np.where(train.loc[:, "Province_State"].isna() , train.loc[:, "Country_Region"], train.loc[:, "Country_Region"] + "_" + train.loc[:, "Province_State"])
 train.loc[:, 'Date'] = pd.to_datetime(train.loc[:, 'Date'])
 train = train.loc[train.loc[:, 'Date'] > '2020-02-20', :]
train_last_date = train.Date.unique() [-1]
print(f"Dataset has training data untill : {str(train_last_date)[:10]}")
 print(f"Training dates: {len(train.Date.unique())}" )<load_from_csv>
missingValueColumns = train.columns[train.isnull().any() ].tolist()
df_null = train[missingValueColumns]
enriched = pd.read_csv("/kaggle/input/data-prep-week3/enriched_covid_19_week_3.csv")
 enriched.loc[:, ["Country_Region", 'total_pop']].groupby('Country_Region' ).agg({'total_pop': np.mean} ).reset_index()<feature_engineering>
def get_meta(train):
data = []
for col in train.columns:
if col == 'target':
role = 'target'
elif col == 'id':
role = 'id'
else:
role = 'input'
if col == 'target' or 'facebook' in col or col == 'gender' or 'bin_' in col:
level = 'binary'
elif train[col].dtype == np.object or col == 'id':
level = 'nominal'
elif train[col].dtype == np.float64:
level = 'interval'
elif train[col].dtype == np.int64:
level = 'ordinal'
keep = True
if col == 'id':
keep = False
dtype = train[col].dtype
col_dict = {
'varname': col,
'role' : role,
'level' : level,
'keep' : keep,
'dtype' : dtype
}
data.append(col_dict)
 meta = pd.DataFrame(data, columns=['varname', 'role', 'level', 'keep', 'dtype'])
 meta.set_index('varname', inplace=True)
 return meta
all_countries = train.groupby(['Date'] ).agg({'ConfirmedCases': np.sum} ).reset_index()
all_countries['sh'] = train.groupby(['Date'] ).agg({'ConfirmedCases': np.sum} ).reset_index().shift(1)['ConfirmedCases']
all_countries['sh'] =(all_countries['ConfirmedCases'] / all_countries['sh'])- 1
all_countries = all_countries.set_index("Date")
 all_countries['sh'].plot()<filter>
meta_data = get_meta(train)
 meta_data
train_old = train.loc[train.loc[:, 'Date'] > '2020-04-01', :]<load_from_csv>
meta_counts = meta_data.groupby(['role', 'level'] ).agg({'dtype': lambda x: x.count() } ).reset_index()
meta_counts
test = pd.read_csv(f"/kaggle/input/covid19-global-forecasting-week-{week}/test.csv")
 test.loc[:, 'Date'] = pd.to_datetime(test.loc[:, 'Date'])
 test_first_date = test.loc[:, 'Date'].values[0]
test_last_date = test.loc[:, 'Date'].values[-1]
print(f'Test period from {str(test_first_date)[:10]} to {str(test_last_date)[:10]}' )<data_type_conversions>
col_ordinal = meta_data[(meta_data.level == 'ordinal')&(meta_data.keep)].index
col_nominal = meta_data[(meta_data.level == 'nominal')&(meta_data.keep)&(meta_data.role != 'target')&(meta_data.role != 'id')].index
col_interval = meta_data[(meta_data.level == 'interval')&(meta_data.keep)].index
col_binary = meta_data[(meta_data.level == 'binary')&(meta_data.keep)&(meta_data.role != 'target')].index
period =(np.array(test_last_date, dtype='datetime64[D]' ).astype(np.int64)- np.array(train_last_date, dtype='datetime64[D]' ).astype(np.int64))<data_type_conversions>
def new_missing_columns(df):
for i in missingValueColumns:
if 'target' not in i:
new_col = 'bin_missing_'+ i
df[new_col] = np.where(df[i].isnull() , True, False)
 return df
print(f"Prediction days: {(np.array(test_last_date, dtype='datetime64[D]' ).astype(np.int64)- np.array(train_last_date, dtype='datetime64[D]' ).astype(np.int64)) +1}")
 print(f"Public set: {(np.array(train_last_date, dtype='datetime64[D]' ).astype(np.int64)- np.array(test_first_date, dtype='datetime64[D]' ).astype(np.int64)) +1}")
 print(f"Full prediction set: {(np.array(test_last_date, dtype='datetime64[D]' ).astype(np.int64)- np.array(test_first_date, dtype='datetime64[D]' ).astype(np.int64)) +1}" )<define_variables>
train = new_missing_columns(train)
 test = new_missing_columns(test )
win = 15
hor = 1<feature_engineering>
def count_label_encoding(train, test,col):
for i in col:
df1 = train[i].value_counts().reset_index(name='freq_'+ i ).rename(columns={'index': 'lc_'+ i})
 train = pd.merge(train,df1,left_on=i, right_on='lc_'+ i, how='left')
 test = pd.merge(test,df1,left_on=i, right_on='lc_'+ i, how='left')
 for i in list(train):
if 'lc_' in i:
train = train.drop(i, axis = 1)
 test = test.drop(i, axis = 1)
 return train, test
train.loc[:, 'ConfirmedCases'] = train.loc[:, 'ConfirmedCases'] - train.groupby('geo')['ConfirmedCases'].shift(periods=1)
 train.loc[:, 'Fatalities'] = train.loc[:, 'Fatalities'] - train.groupby('geo')['Fatalities'].shift(periods=1)
 train = train.groupby('geo' ).tail(train.groupby('geo' ).size().values[0]-1)
 train.loc[train.loc[:, 'ConfirmedCases'] < 0, 'ConfirmedCases'] = 0.0
train.loc[train.loc[:, 'Fatalities'] < 0, 'Fatalities'] = 0.0
train<install_modules>
train, test = count_label_encoding(train, test,col_nominal)
 train, test = count_label_encoding(train, test,col_binary )
%%bash
pip install pytorch_lightning<import_modules>
meta_data = get_meta(train)
 col_ordinal = meta_data[(meta_data.level == 'ordinal')&(meta_data.keep)&(meta_data.role != 'target')].index
col_nominal = meta_data[(meta_data.level == 'nominal')&(meta_data.keep)&(meta_data.role != 'target')].index
col_interval = meta_data[(meta_data.level == 'interval')&(meta_data.keep)&(meta_data.role != 'target')].index
col_binary = meta_data[(meta_data.level == 'binary')&(meta_data.keep)&(meta_data.role != 'target')].index
import torch.nn as nn
import torch
import torch.nn.functional as F
import pytorch_lightning as ptl
from torch import optim
from torch.utils.data import DataLoader
from collections import OrderedDict<compute_test_metric>
meta_counts = meta_data.groupby(['role', 'level'] ).agg({'dtype': lambda x: x.count() } ).reset_index()
meta_counts
def rmsle(predict, target):
return torch.sqrt(((torch.log(predict + 1)- torch.log(target + 1)) **2 ).mean() )<create_dataframe>
from sklearn.model_selection import train_test_split
class MTSFDataset(torch.utils.data.Dataset):
def __init__(self, window, horizon, set_type, tra, validation):
assert type(set_type)== type('str')
 self.window = window
self.horizon = horizon
self.tra = tra
self.validation = validation
self.set_type = set_type
if set_type == 'train':
rawdata = tra
elif set_type == 'validation':
rawdata = validation
_, self.len, self.var_num = rawdata.shape
self.sample_num = max(self.len - self.window - self.horizon + 1, 0)
 self.samples, self.labels = self.__getsamples(rawdata)
 def __getsamples(self, data):
x, y = [], []
for j in range(len(data)) :
X = torch.zeros(( self.sample_num, self.window, self.var_num))
Y = torch.zeros(( self.sample_num, 1, self.var_num))
for i in range(self.sample_num):
start = i
end = i + self.window
X[i, :, :] = torch.from_numpy(data[j, start:end, :])
 Y[i, :, :] = torch.from_numpy(data[j, end+self.horizon-1, :])
 x.append(X)
 y.append(Y)
 return(torch.cat(x), torch.cat(y))
def __len__(self):
return self.sample_num
def __getitem__(self, idx):
sample = [self.samples[idx, :, :], self.labels[idx, :, :]]
return sample<compute_train_metric>
from sklearn.model_selection import train_test_split
class ScaledDotProductAttention(nn.Module):
def __init__(self, temperature, attn_dropout=0.1):
super().__init__()
self.temperature = temperature
self.dropout = nn.Dropout(attn_dropout)
 self.softmax = nn.Softmax(dim=2)
 def forward(self, q, k, v):
attn = torch.bmm(q, k.transpose(1, 2))
attn = attn / self.temperature
attn = self.softmax(attn)
 attn = self.dropout(attn)
 output = torch.bmm(attn, v)
 return output, attn
class MultiHeadAttention(nn.Module):
def __init__(self, n_head, d_model, d_k, d_v, dropout=0.1):
super().__init__()
self.n_head = n_head
self.d_k = d_k
self.d_v = d_v
self.w_qs = nn.Linear(d_model, n_head * d_k)
 self.w_ks = nn.Linear(d_model, n_head * d_k)
 self.w_vs = nn.Linear(d_model, n_head * d_v)
 nn.init.normal_(self.w_qs.weight, mean=0, std=np.sqrt(2.0 /(d_model + d_k)))
 nn.init.normal_(self.w_ks.weight, mean=0, std=np.sqrt(2.0 /(d_model + d_k)))
 nn.init.normal_(self.w_vs.weight, mean=0, std=np.sqrt(2.0 /(d_model + d_v)))
 self.attention = ScaledDotProductAttention(temperature=np.power(d_k, 0.5))
self.layer_norm = nn.LayerNorm(d_model)
 self.fc = nn.Linear(n_head * d_v, d_model)
 nn.init.xavier_normal_(self.fc.weight)
 self.dropout = nn.Dropout(dropout)
 def forward(self, q, k, v):
d_k, d_v, n_head = self.d_k, self.d_v, self.n_head
sz_b, len_q, _ = q.size()
sz_b, len_k, _ = k.size()
sz_b, len_v, _ = v.size()
residual = q
q = self.w_qs(q ).view(sz_b, len_q, n_head, d_k)
 k = self.w_ks(k ).view(sz_b, len_k, n_head, d_k)
 v = self.w_vs(v ).view(sz_b, len_v, n_head, d_v)
 q = q.permute(2, 0, 1, 3 ).contiguous().view(-1, len_q, d_k)
 k = k.permute(2, 0, 1, 3 ).contiguous().view(-1, len_k, d_k)
 v = v.permute(2, 0, 1, 3 ).contiguous().view(-1, len_v, d_v)
 output, attn = self.attention(q, k, v)
 output = output.view(n_head, sz_b, len_q, d_v)
 output = output.permute(1, 2, 0, 3 ).contiguous().view(sz_b, len_q, -1)
 output = self.dropout(self.fc(output))
output = self.layer_norm(output + residual)
 return output, attn
class PositionwiseFeedForward(nn.Module):
def __init__(self, d_in, d_hid, dropout=0.1):
super().__init__()
self.w_1 = nn.Conv1d(d_in, d_hid, 1)
 self.w_2 = nn.Conv1d(d_hid, d_in, 1)
 self.layer_norm = nn.LayerNorm(d_in)
 self.dropout = nn.Dropout(dropout)
 def forward(self, x):
residual = x
output = x.transpose(1, 2)
 output = self.w_2(F.relu(self.w_1(output)))
 output = output.transpose(1, 2)
 output = self.dropout(output)
 output = self.layer_norm(output + residual)
 return output
class EncoderLayer(nn.Module):
def __init__(self, d_model, d_inner, n_head, d_k, d_v, dropout=0.1):
super(EncoderLayer, self ).__init__()
self.slf_attn = MultiHeadAttention(
 n_head, d_model, d_k, d_v, dropout=dropout)
 self.pos_ffn = PositionwiseFeedForward(d_model, d_inner, dropout=dropout)
 def forward(self, enc_input):
enc_output, enc_slf_attn = self.slf_attn(
 enc_input, enc_input, enc_input)
 enc_output = self.pos_ffn(enc_output)
 return enc_output, enc_slf_attn
class DecoderLayer(nn.Module):
def __init__(self, d_model, d_inner, n_head, d_k, d_v, dropout=0.1):
super(DecoderLayer, self ).__init__()
self.slf_attn = MultiHeadAttention(n_head, d_model, d_k, d_v, dropout=dropout)
 self.enc_attn = MultiHeadAttention(n_head, d_model, d_k, d_v, dropout=dropout)
 self.pos_ffn = PositionwiseFeedForward(d_model, d_inner, dropout=dropout)
 def forward(self, dec_input, enc_output, non_pad_mask=None, slf_attn_mask=None, dec_enc_attn_mask=None):
dec_output, dec_slf_attn = self.slf_attn(
 dec_input, dec_input, dec_input, mask=slf_attn_mask)
 dec_output, dec_enc_attn = self.enc_attn(
 dec_output, enc_output, enc_output, mask=dec_enc_attn_mask)
 dec_output = self.pos_ffn(dec_output)
 return dec_output, dec_slf_attn, dec_enc_attn
class Single_Global_SelfAttn_Module(nn.Module):
def __init__(
 self,
window, n_multiv, n_kernels, w_kernel,
d_k, d_v, d_model, d_inner,
n_layers, n_head, drop_prob=0.1):

 super(Single_Global_SelfAttn_Module, self ).__init__()
self.window = window
self.w_kernel = w_kernel
self.n_multiv = n_multiv
self.d_model = d_model
self.drop_prob = drop_prob
self.conv2 = nn.Conv2d(1, n_kernels,(window, w_kernel))
self.in_linear = nn.Linear(n_kernels, d_model)
 self.out_linear = nn.Linear(d_model, n_kernels)
 self.layer_stack = nn.ModuleList([
EncoderLayer(d_model, d_inner, n_head, d_k, d_v, dropout=drop_prob)
 for _ in range(n_layers)])
 def forward(self, x, return_attns=False):
x = x.view(-1, self.w_kernel, self.window, self.n_multiv)
 x2 = F.relu(self.conv2(x))
x2 = nn.Dropout(p=self.drop_prob )(x2)
 x = torch.squeeze(x2, 2)
 x = torch.transpose(x, 1, 2)
 src_seq = self.in_linear(x)
 enc_slf_attn_list = []
enc_output = src_seq
for enc_layer in self.layer_stack:
enc_output, enc_slf_attn = enc_layer(enc_output)
 if return_attns:
enc_slf_attn_list += [enc_slf_attn]
if return_attns:
return enc_output, enc_slf_attn_list
enc_output = self.out_linear(enc_output)
 return enc_output,
class Single_Local_SelfAttn_Module(nn.Module):
def __init__(
 self,
window, local, n_multiv, n_kernels, w_kernel,
d_k, d_v, d_model, d_inner,
n_layers, n_head, drop_prob=0.1):

 super(Single_Local_SelfAttn_Module, self ).__init__()
self.window = window
self.w_kernel = w_kernel
self.n_multiv = n_multiv
self.d_model = d_model
self.drop_prob = drop_prob
self.conv1 = nn.Conv2d(1, n_kernels,(local, w_kernel))
self.pooling1 = nn.AdaptiveMaxPool2d(( 1, n_multiv))
self.in_linear = nn.Linear(n_kernels, d_model)
 self.out_linear = nn.Linear(d_model, n_kernels)
 self.layer_stack = nn.ModuleList([
EncoderLayer(d_model, d_inner, n_head, d_k, d_v, dropout=drop_prob)
 for _ in range(n_layers)])
 def forward(self, x, return_attns=False):
x = x.view(-1, self.w_kernel, self.window, self.n_multiv)
 x1 = F.relu(self.conv1(x))
x1 = self.pooling1(x1)
 x1 = nn.Dropout(p=self.drop_prob )(x1)
 x = torch.squeeze(x1, 2)
 x = torch.transpose(x, 1, 2)
 src_seq = self.in_linear(x)
 enc_slf_attn_list = []
enc_output = src_seq
for enc_layer in self.layer_stack:
enc_output, enc_slf_attn = enc_layer(enc_output)
 if return_attns:
enc_slf_attn_list += [enc_slf_attn]
if return_attns:
return enc_output, enc_slf_attn_list
enc_output = self.out_linear(enc_output)
 return enc_output,
class AR(nn.Module):
def __init__(self, window):
super(AR, self ).__init__()
self.linear = nn.Linear(window, 1)
 def forward(self, x):
x = torch.transpose(x, 1, 2)
 x = self.linear(x)
 x = torch.transpose(x, 1, 2)
 return x
class DSANet(ptl.LightningModule):
def __init__(self, tra, validation, n_multiv, batch_size=16, window=64, local=3, n_kernels=32,
drop_prob=0.1, criterion='rmsle_loss', learning_rate=0.005, horizon=14):
super(DSANet, self ).__init__()
self.batch_size = batch_size
self.window = window
self.local = local
self.n_multiv = n_multiv
self.n_kernels = n_kernels
self.w_kernel = 1
self.d_model = 512
self.d_inner = 2048
self.n_layers = 6
self.n_head = 8
self.d_k = 64
self.d_v = 64
self.drop_prob = drop_prob
self.criterion = criterion
self.learning_rate = learning_rate
self.horizon = horizon
self.tra = tra
self.validation = validation
self.__build_model()
def __build_model(self):
self.sgsf = Single_Global_SelfAttn_Module(
 window=self.window, n_multiv=self.n_multiv, n_kernels=self.n_kernels,
w_kernel=self.w_kernel, d_k=self.d_k, d_v=self.d_v, d_model=self.d_model,
d_inner=self.d_inner, n_layers=self.n_layers, n_head=self.n_head, drop_prob=self.drop_prob)
 self.slsf = Single_Local_SelfAttn_Module(
 window=self.window, local=self.local, n_multiv=self.n_multiv, n_kernels=self.n_kernels,
w_kernel=self.w_kernel, d_k=self.d_k, d_v=self.d_v, d_model=self.d_model,
d_inner=self.d_inner, n_layers=self.n_layers, n_head=self.n_head, drop_prob=self.drop_prob)
 self.ar = AR(window=self.window)
 self.W_output1 = nn.Linear(2 * self.n_kernels, 1)
 self.dropout = nn.Dropout(p=self.drop_prob)
 self.active_func = nn.Tanh()
def forward(self, x):
sgsf_output, *_ = self.sgsf(x)
 slsf_output, *_ = self.slsf(x)
 sf_output = torch.cat(( sgsf_output, slsf_output), 2)
 sf_output = self.dropout(sf_output)
 sf_output = self.W_output1(sf_output)
 sf_output = torch.transpose(sf_output, 1, 2)
 ar_output = self.ar(x)
 output = sf_output + ar_output
output[output < 0] = 0.0
return output
def loss(self, labels, predictions):
if self.criterion == 'l1_loss':
loss = F.l1_loss(predictions, labels)
 elif self.criterion == 'mse_loss':
loss = F.mse_loss(predictions, labels)
 elif self.criterion == 'rmsle_loss':
loss = rmsle(predictions, labels)
 return loss
def training_step(self, data_batch, batch_i):
x, y = data_batch
y_hat = self.forward(x)
 loss_val = self.loss(y, y_hat)
 if self.trainer.use_dp:
loss_val = loss_val.unsqueeze(0)
 output = OrderedDict({
'loss': loss_val
})
 return output
def validation_step(self, data_batch, batch_i):
x, y = data_batch
y_hat = self.forward(x)
 loss_val = self.loss(y, y_hat)
 if self.trainer.use_dp:
loss_val = loss_val.unsqueeze(0)
 output = OrderedDict({
'val_loss': loss_val,
'y': y,
'y_hat': y_hat,
})
 return output
def validation_epoch_end(self, outputs):
loss_sum = 0
for x in outputs:
loss_sum += x['val_loss'].item()
val_loss_mean = loss_sum / len(outputs)
 y = torch.cat(( [x['y'] for x in outputs]), 0)
 y_hat = torch.cat(( [x['y_hat'] for x in outputs]), 0)
 num_var = y.size(-1)
 y = y.view(-1, num_var)
 y_hat = y_hat.view(-1, num_var)
 sample_num = y.size(0)
 y_diff = y_hat - y
y_mean = torch.mean(y)
 y_translation = y - y_mean
val_rrse = torch.sqrt(torch.sum(torch.pow(y_diff, 2)))/ torch.sqrt(torch.sum(torch.pow(y_translation, 2)))
 y_m = torch.mean(y, 0, True)
 y_hat_m = torch.mean(y_hat, 0, True)
 y_d = y - y_m
y_hat_d = y_hat - y_hat_m
corr_top = torch.sum(y_d * y_hat_d, 0)
 corr_bottom = torch.sqrt(( torch.sum(torch.pow(y_d, 2), 0)* torch.sum(torch.pow(y_hat_d, 2), 0)))
 corr_inter = corr_top / corr_bottom
val_corr =(1./ num_var)* torch.sum(corr_inter)
 val_mae =(1./(sample_num * num_var)) * torch.sum(torch.abs(y_diff))
tqdm_dic = {
'val_loss': val_loss_mean,
'RRSE': val_rrse.item() ,
'CORR': val_corr.item() ,
'MAE': val_mae.item()
}
return tqdm_dic
def configure_optimizers(self):
optimizer = optim.Adam(self.parameters() , lr=self.learning_rate)
 scheduler = optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=10)
 return [optimizer], [scheduler]
def __dataloader(self, train):
set_type = train
dataset = MTSFDataset(window=self.window, horizon=self.horizon,
set_type=set_type,
tra=self.tra, validation=self.validation)
 train_sampler = None
batch_size = self.batch_size
should_shuffle = train_sampler is None
loader = DataLoader(
 dataset=dataset,
batch_size=batch_size,
shuffle=should_shuffle,
sampler=train_sampler,
num_workers=4 
)
 return loader
@ptl.data_loader
def train_dataloader(self):
return self.__dataloader(train='train')
 @ptl.data_loader
def val_dataloader(self):
return self.__dataloader(train='validation' )<train_model>
X = pd.concat([train[col_interval],train[col_ordinal],pd.get_dummies(train[col_binary])], axis=1)
 y = pd.DataFrame(train.target)
 X.fillna(-1, inplace=True)
 y.fillna(-1, inplace=True)
 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42 )
model_cases = DSANet(np.array([train_fatal, train_cases]), np.array([valid_fatal, valid_cases]), train_cases.shape[1], window=win,
learning_rate=0.01, horizon=hor, drop_prob=0.5, batch_size=128)
 trainer = ptl.Trainer(val_check_interval=1, max_steps=5000, gpus=1, show_progress_bar=False)
 trainer.fit(model_cases )<load_pretrained>
rf = RandomForestClassifier(n_estimators=150, max_depth=8, min_samples_leaf=30, max_features=0.2, n_jobs=-1, random_state=0)
 rf.fit(X_train, y_train['target'])
 features = X_train.columns.values
print("----- Training Done -----" )
sd = torch.load(glob("/kaggle/working/lightning_logs/version_0/checkpoints/*.ckpt")[0])
 model_cases.load_state_dict(sd['state_dict'] )<data_type_conversions>
from sklearn.metrics import accuracy_score, roc_auc_score
pred_size =(np.array(test_last_date, dtype='datetime64[D]' ).astype(np.int64)- np.array(test_first_date, dtype='datetime64[D]' ).astype(np.int64)) +1<feature_engineering>
acc = accuracy_score(y_train, rf.predict(X_train))
auc = roc_auc_score(y_train, rf.predict(X_train))
print("Accuracy: %.4f" % acc)
 print("AUC: %.4f" % auc )
sub = pd.read_csv(f"/kaggle/input/covid19-global-forecasting-week-{week}/submission.csv")
 sub = test.merge(res, how='left', on=['Date', 'Province_State', 'Country_Region'] ).loc[:, ["ForecastId", "ConfirmedCases", "Fatalities"]]
sub['Fatalities'] = np.array(sub['Fatalities'], dtype=np.int)
 sub["ConfirmedCases"] = np.array(sub["ConfirmedCases"], dtype=np.int)
 sub<save_to_csv>
acc = accuracy_score(y_test, rf.predict(X_test))
auc = roc_auc_score(y_test, rf.predict(X_test))
print("Accuracy: %.4f" % acc)
 print("AUC: %.4f" % auc )
sub.to_csv("submission.csv", index=False )<set_options>
def get_feature_importance_df(feature_importances,
column_names,
top_n=25):

 imp_dict = dict(zip(column_names,
feature_importances))
top_features = sorted(imp_dict,
key=imp_dict.get,
reverse=True)[0:top_n]
top_importances = [imp_dict[feature] for feature
in top_features]
df = pd.DataFrame(data={'feature': top_features,
'importance': top_importances})
 return df
%matplotlib inline 
<load_from_csv>
feature_importance = get_feature_importance_df(rf.feature_importances_, features)
 feature_importance
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_submission = pd.read_csv("/kaggle/input/covid19-global-forecasting-week-4/submission.csv" )<data_type_conversions>
from xgboost import XGBClassifier
from lightgbm import LGBMClassifier
from catboost import CatBoostClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
df_train['Date'] = pd.to_datetime(df_train['Date'])
 df_test['Date'] = pd.to_datetime(df_test['Date'])
<groupby>
def cross_val_model(X,y, model, n_splits=3):
X = np.array(X)
 y = np.array(y)
 folds = list(StratifiedKFold(n_splits=n_splits, shuffle=True, random_state=2017 ).split(X, y))
for j,(train_idx, test_idx)in enumerate(folds):
X_train = X[train_idx]
y_train = y[train_idx]
X_holdout = X[test_idx]
y_holdout = y[test_idx]
print("Fit %s fold %d" %(str(model ).split('(')[0], j+1))
m = model.fit(X_train, y_train)
 cross_score = cross_val_score(model, X_holdout, y_holdout, cv=3, scoring='roc_auc')
 print(" cross_score: %.5f" % cross_score.mean())
 return m
Global_Top_Cases = df_train[df_train['Date'] == df_train['Date'].max() ].groupby(['Country_Region','Date'] )<sort_values>
rf_params = {}
rf_params['n_estimators'] = 200
rf_params['max_depth'] = 6
rf_params['min_samples_split'] = 70
rf_params['min_samples_leaf'] = 30
G_cases = Global_Top_Cases.sum().sort_values(['ConfirmedCases'], ascending = False )<drop_column>
rf_model = RandomForestClassifier(**rf_params, random_state=29,n_jobs = -1 )
G1 = G_cases.drop(['Id'], axis = 1 )<feature_engineering>
cross_val_model(X_train, y_train['target'], rf_model )
G1['ConfirmedCases_percent_change'] = G1['ConfirmedCases'].pct_change()
G1['Fatalities_percent_change'] = G1['Fatalities'].pct_change()<drop_column>
xgb_params = {}
xgb_params['learning_rate'] = 0.02
xgb_params['n_estimators'] = 1000
xgb_params['max_depth'] = 4
xgb_params['subsample'] = 0.9
xgb_params['colsample_bytree'] = 0.9
G2 = G1.drop(['ConfirmedCases', 'Fatalities'], axis = 1 )<groupby>
XGB_model = XGBClassifier(**rf_params, random_state=29,n_jobs=-1 )
us = df_train[df_train['Country_Region'] == 'US'].groupby(['Province_State'] ).max()<sort_values>
cross_val_model(X_train, y_train['target'], XGB_model )
us_cases = us.sort_values(['ConfirmedCases'], ascending = False )<drop_column>
train_ext = train.copy()
test_ext = test.copy()
missing_values_table(train_ext )
us_cases = us_cases.drop(['Id', 'Date','Country_Region'], axis = 1 )<feature_engineering>
def create_extra_features(train_ext):
train_ext['null_sum'] = train_ext[train_ext==-1].count(axis=1)
 train_ext['ord_sum'] = train_ext[col_ordinal].sum(axis=1)
 train_ext['interval_median'] = train_ext[col_interval].sum(axis=1)
 train_ext['new_amount_borrowed_by_income'] = train_ext['amount_borrowed']/train_ext['income']
train_ext['new_amount_borrowed_by_months'] = train_ext['amount_borrowed']/train_ext['borrowed_in_months']
return train_ext
us_cases['ConfirmedCases_percent_change'] = us_cases['ConfirmedCases'].pct_change()
us_cases['Fatalities_percent_change'] = us_cases['Fatalities'].pct_change()<drop_column>
train_ext = create_extra_features(train_ext)
 test_ext = create_extra_features(test_ext)
 train_backup = train_ext.copy()
test_backup = test_ext.copy()
us_cases = us_cases.drop(['ConfirmedCases', 'Fatalities'], axis = 1 )<feature_engineering>
meta_data = get_meta(train_ext)
 col_ordinal = meta_data[(meta_data.level == 'ordinal')&(meta_data.keep)&(meta_data.role != 'target')].index
col_nominal = meta_data[(meta_data.level == 'nominal')&(meta_data.keep)&(meta_data.role != 'target')].index
col_interval = meta_data[(meta_data.level == 'interval')&(meta_data.keep)&(meta_data.role != 'target')].index
col_binary = meta_data[(meta_data.level == 'binary')&(meta_data.keep)&(meta_data.role != 'target')].index
df_train['Country_Region'] = df_train['Country_Region'] + ' ' + df_train['Province_State']
df_test['Country_Region'] = df_test['Country_Region'] + ' ' + df_test['Province_State']<feature_engineering>
meta_counts = meta_data.groupby(['role', 'level'] ).agg({'dtype': lambda x: x.count() } ).reset_index()
meta_counts
df_train['Date'] = list(df_train['Date'].dt.strftime('%Y,%m,%d'))
df_test['Date'] = list(df_test['Date'].dt.strftime('%Y,%m,%d'))<feature_engineering>
ids_targets = meta_data[meta_data['role'] != 'input'].index
def createDateFields(df):
year = []
month = []
day = []
for i in df['Date']:
i = i.split(",")
 year.append(i[0])
 month.append(i[1])
 day.append(i[2])
 df['year'] = year
df['month'] = month
df['day'] = day
return df 
<drop_column>
train_ext.fillna(-1, inplace = True)
 X_ext = pd.concat([train_ext[col_interval],train_ext[col_ordinal], pd.get_dummies(train_ext[col_binary])], axis=1)
 X_ext.head()
df_train = createDateFields(df_train)
 df_test = createDateFields(df_test)
<data_type_conversions>
X_ext = X_ext.drop(columns = ['facebook_profile_False','gender_f'], axis=1 )
df_train['ConfirmedCases']= df_train['ConfirmedCases'].apply(int)
 df_train['Fatalities'] = df_train['Fatalities'].apply(int )<prepare_x_and_y>
test_ext = pd.concat([test_ext[col_interval],test_ext[col_ordinal], pd.get_dummies(test_ext[col_binary])], axis=1)
 test_ext.fillna(-1, inplace = True)
 y_ext = pd.DataFrame(train_ext.target)
 y_ext=y_ext.astype('bool')
 y_ext = y_ext.values
y_ext = y_ext.reshape(-1 )
y1 = df_train.ConfirmedCases
y2 = df_train.Fatalities
del df_train['ConfirmedCases']
del df_train['Fatalities']<import_modules>
test_ext['gender_-1'] = 0
test_ext['facebook_profile_-1'] = 0
test_ext=test_ext.drop(columns = ['facebook_profile_False', 'gender_f'], axis = 1)
 test_ext.head()
from sklearn.preprocessing import LabelEncoder<categorify>
cols = list(X_ext)
 test_ext = test_ext[cols]
lable_encoder = LabelEncoder()
df_train['Country_Region'] = lable_encoder.fit_transform(df_train['Country_Region'])
 df_test['Country_Region'] = lable_encoder.transform(df_test['Country_Region'] )<define_variables>
X_train, X_test, y_train, y_test = train_test_split(X_ext, y_ext, test_size=0.2, random_state=42 )
features = ['Country_Region', 'month', 'day']<prepare_x_and_y>
cross_val_model(X_ext, y_ext, rf_model )
X = df_train[features]<define_variables>
cross_val_model(X_ext, y_ext, XGB_model )
test_features = ['Country_Region', 'month', 'day']<prepare_x_and_y>
gc.collect
X_test = df_test[test_features]<import_modules>
from sklearn.model_selection import GridSearchCV
from sklearn.preprocessing import MinMaxScaler<normalization>
from sklearn.model_selection import GridSearchCV
min_max_scaler = MinMaxScaler()
X = min_max_scaler.fit_transform(X.values)
 X_test = min_max_scaler.transform(X_test.values )<import_modules>
tuned_parameters = [{'max_depth': [4,5,6,7,8,9,10],
'max_features': [4,5,6,7,8,9,10],
'n_estimators':[10,25,50,75]}]
clf = GridSearchCV(RandomForestClassifier(random_state=29), tuned_parameters, cv=3, scoring='roc_auc')
 clf.fit(X_train, y_train )
import xgboost as xgb<choose_model_class>
print("Best parameters set found on development set:")
 print()
print(clf.best_params_)
 print()
print("Grid scores on development set:")
 print()
means = clf.cv_results_['mean_test_score']
stds = clf.cv_results_['std_test_score']
for mean, std, params in zip(means, stds, clf.cv_results_['params']):
print("%0.3f(+/-%0.03f)for %r"
%(mean, std * 2, params))
print()
print("Detailed classification report:")
 print()
print("The model is trained on the full development set.")
 print("The scores are computed on the full evaluation set.")
 print()
acc = accuracy_score(y_test, clf.predict(X_test))
auc = roc_auc_score(y_test, clf.predict(X_test))
print("Accuracy: %.4f" % acc)
 print()
print("AUC: %.4f" % auc)
 print()
xg_reg_model1 = xgb.XGBRegressor(n_estimators = 1500,random_state = 0, max_depth = 15, learning_rate = 0.1 )<train_model>
from hyperopt.pyll.base import scope
from hyperopt.pyll.stochastic import sample
from hyperopt import STATUS_OK, Trials, fmin, hp, tpe
xg_reg_model1.fit(X,y1);<predict_on_test>
N_HYPEROPT_PROBES = 10
EARLY_STOPPING = 80
HOLDOUT_SEED = 123456
HOLDOUT_SIZE = 0.10
HYPEROPT_ALGO = tpe.suggest
DATASET = 'clean'
SEED0 = random.randint(1,1000000000)
 NB_CV_FOLDS = 5
y_pred1 = xg_reg_model1.predict(X_test)
<train_model>
obj_call_count = 0
cur_best_score = 0
xg_reg_model2 = xgb.XGBRegressor(n_estimators = 1500,random_state =0, max_depth = 15, learning_rate = 0.1)
 xg_reg_model2.fit(X,y2);<predict_on_test>
space_RF ={
'n_estimators' : hp.choice('n_estimators', np.arange(10, 200, dtype=int)) ,
'max_depth' : hp.choice("max_depth", np.arange(3, 15, dtype=int)) ,
'min_samples_split' : hp.choice("min_samples_split", np.arange(20, 100, dtype=int)) ,
'min_samples_leaf' : hp.choice("min_samples_leaf", np.arange(10, 100, dtype=int)) ,
'criterion' : hp.choice('criterion', ["gini", "entropy"]),
'class_weight' : hp.choice('class_weight', ['balanced_subsample', None]),
'n_jobs' : -1,
'oob_score' : True,
'random_state' : hp.randint('random_state',2000000)
 }
y_pred2 = xg_reg_model2.predict(X_test)
<data_type_conversions>
def objective_RF(space):
global obj_call_count, cur_best_score, X_train, y_train, test, X_test, y_test, test_id
obj_call_count += 1
print('
LightGBM objective call
sorted_params = sorted(space.items() , key=lambda z: z[0])
 print('Params:', str.join(' ', ['{}={}'.format(k, v)for k, v in sorted_params if not k.startswith('column:')]))
params = sample(space)
 mdl = RandomForestClassifier(**params)
 cv_score = cross_val_score(mdl, X_train, y_train ).mean()
print('CV finished ;
cv_score={:7.5f}'.format(cv_score))
_model = mdl.fit(X_train, y_train)
 predictions = _model.predict_proba(X_test)[:,1]
score = roc_auc_score(y_test, predictions)
 print('valid score={}'.format(score))
do_submit = score > 0.64
if score > cur_best_score:
cur_best_score = score
print('NEW BEST SCORE={}'.format(cur_best_score))
do_submit = True
if do_submit:
submit_guid = uuid.uuid4()
print('Compute submissions guid={}'.format(submit_guid))
y_submission = _model.predict_proba(test_ext)[:,1]
submission_filename = 'rf_score_{:13.11f}_submission_guid_{}.csv'.format(score,submit_guid)
 pd.DataFrame(
 {'ids':test_id, 'prob':y_submission}
).to_csv(submission_filename, index=False)
 loss = 1 - score
return {'loss': loss, 'status': STATUS_OK}
y_pred1 , y_pred2 = np.round(y_pred1),np.round(y_pred2)
 y_pred1[y_pred1 < 0] = 0
y_pred2[y_pred2 < 0] = 0
df_submission['ConfirmedCases'] = y_pred1.astype(int)
 df_submission['Fatalities'] = y_pred2.astype(int )<save_to_csv>
trials = Trials()
best = fmin(fn=objective_RF,
space=space_RF,
algo=HYPEROPT_ALGO,
max_evals=N_HYPEROPT_PROBES,
trials=trials,
verbose=1)
 print('-'*50)
 print('The best params for RF:')
 print(best)
 print('
' )
df_submission.to_csv("submission.csv",index = False )<load_from_csv>
space_XGB ={
'max_depth' : hp.choice("max_depth", np.arange(5, 15,dtype=int)) ,
'learning_rate' : hp.loguniform('learning_rate', -4.9, -3.0),
'n_estimators' : hp.choice('n_estimators', np.arange(10, 100,dtype=int)) ,
'objective' : 'binary:logistic',
'booster' : 'gbtree',
'reg_alpha' : hp.uniform('reg_alpha', 1e-5, 1e-1),
'reg_lambda' : hp.uniform('reg_lambda', 1e-5, 1e-1),
'colsample_bytree' : hp.uniform('colsample_bytree', 0.5, 0.8),
'min_child_weight ': hp.uniform('min_child_weight', 0.5, 0.8),
'random_state' : hp.randint('random_state',2000000)
 }
PATH ='/kaggle/input/covid19-global-forecasting-week-4'
trainingdata = pd.read_csv(f'{PATH}/train.csv')
 testdata = pd.read_csv(f'{PATH}/test.csv' )<create_dataframe>
def objective_XGB(space):
global obj_call_count, cur_best_score, X_train, y_train, test, X_test, y_test, test_id
obj_call_count += 1
print('
LightGBM objective call
sorted_params = sorted(space.items() , key=lambda z: z[0])
 print('Params:', str.join(' ', ['{}={}'.format(k, v)for k, v in sorted_params if not k.startswith('column:')]))
params = sample(space)
 mdl = XGBClassifier(**params)
 cv_score = cross_val_score(mdl, X_train, y_train ).mean()
print('CV finished ;
cv_score={:7.5f}'.format(cv_score))
_model = mdl.fit(X_train, y_train, eval_set=[(X_train, y_train),(X_test, y_test)], eval_metric='auc',verbose=True,early_stopping_rounds =30)
 params.update({'n_estimators': _model.best_iteration})
 predictions = _model.predict_proba(X_test)[:,1]
score = roc_auc_score(y_test, predictions)
 print('valid score={}'.format(score))
do_submit = score > 0.64
if score > cur_best_score:
cur_best_score = score
print('NEW BEST SCORE={}'.format(cur_best_score))
do_submit = True
if do_submit:
submit_guid = uuid.uuid4()
print('Compute submissions guid={}'.format(submit_guid))
y_submission = _model.predict_proba(test_ext)[:,1]
submission_filename = 'xgb_score_{:13.11f}_submission_guid_{}.csv'.format(score,submit_guid)
 pd.DataFrame(
 {'ids':test_id, 'prob':y_submission}
).to_csv(submission_filename, index=False)
 loss = 1 - score
return {'loss': loss, 'status': STATUS_OK}
train = trainingdata
test = testdata<data_type_conversions>
trials = Trials()
best = fmin(fn=objective_XGB,
space=space_XGB,
algo=HYPEROPT_ALGO,
max_evals=N_HYPEROPT_PROBES,
trials=trials,
verbose=1)
 print('-'*50)
 print('The best params for XGB:')
 print(best)
 print('
' )
trainingdate = pd.to_datetime(trainingdata["Date"])
 testdate = pd.to_datetime(testdata["Date"])
 print(trainingdate )<categorify>
train_stack = train_backup.copy()
test_stack = test_backup.copy()
m = []
d = []
for i in range(0,ltrainingdate):
dx =(trainingdate[i].strftime("%d"))
mx =(trainingdate[i].strftime("%m"))
m.append(int(mx))
d.append(int(dx))
mt = []
dt = []
for i in range(0,ltestdate):
dtx =(testdate[i].strftime("%d"))
mtx =(testdate[i].strftime("%m"))
mt.append(int(mtx))
dt.append(int(dtx)) 
<categorify>
meta_data = get_meta(train_stack)
 col_ordinal = meta_data[(meta_data.level == 'ordinal')&(meta_data.keep)&(meta_data.role != 'target')].index
col_nominal = meta_data[(meta_data.level == 'nominal')&(meta_data.keep)&(meta_data.role != 'target')].index
col_interval = meta_data[(meta_data.level == 'interval')&(meta_data.keep)&(meta_data.role != 'target')].index
col_binary = meta_data[(meta_data.level == 'binary')&(meta_data.keep)&(meta_data.role != 'target')].index
m = []
d = []
for i in range(0,ltrainingdate):
dx =(trainingdate[i].strftime("%d"))
mx =(trainingdate[i].strftime("%m"))
m.append(int(mx))
d.append(int(dx))
mt = []
dt = []
for i in range(0,ltestdate):
dtx =(testdate[i].strftime("%d"))
mtx =(testdate[i].strftime("%m"))
mt.append(int(mtx))
dt.append(int(dtx)) 
<feature_engineering>
train_stack = train_stack.replace(-1, np.NaN)
 d_median = train_stack.median(axis=0)
 d_mean = train_stack.mean(axis=0)
 train_stack = train_stack.fillna(-1 )
train.insert(6,"Month",m,False)
 train.insert(7,"Day",d,False)
 test.insert(4,"Month",mt,False)
 test.insert(5,"Day",dt,False )<count_unique_values>
from sklearn import preprocessing
print("Training Data")
 traindays = trainingdata['Date'].nunique()
print("Number of Country_Region: ", trainingdata['Country_Region'].nunique())
 print("Number of Province_State: ", trainingdata['Province_State'].nunique())
 print("Number of Days: ", traindays)
 notrain = trainingdata['Id'].nunique()
print("Number of datapoints in train:", notrain)
 lotrain = int(notrain/traindays)
 print("L Trains:", lotrain)
<count_unique_values>
def transform(df, ohe, d_median, d_mean):
dcol = [c for c in d_median.index if c in d_mean.index and c !='target']
for c in dcol:
if 'bin_' not in c:
df[c+'_median_range'] =(df[c].values > d_median[c])
 df[c+'_mean_range'] =(df[c].values > d_mean[c])
 for c in one_hot:
if len(one_hot[c])>2 and len(one_hot[c])< 7:
for val in one_hot[c]:
df[c+'_oh_' + str(val)] =(df[c].values == val)
 return df
print("Test Data")
 testdays = testdata['Date'].nunique()
print("Number of Days: ", testdays)
 notest = testdata['ForecastId'].nunique()
print("Number of datapoints in test:", notest)
 lotest = int(notest/testdays)
 print("L Test:", lotest)
<define_variables>
train_stack = transform(train_stack, one_hot,d_median, d_mean )
zt = testdate[0]
daycount = []
for i in range(0,lotrain):
for j in range(1,traindays+1):
daycount.append(j)
<define_variables>
test_stack = transform(test_stack, one_hot, d_median, d_mean )
for i in range(traindays):
if(zt == trainingdate[i]):
zx = i
print(zx)
 daytest = []
for i in range(0,lotest):
for j in range(1,testdays+1):
jr = zx + j
daytest.append(jr)
<prepare_output>
train_stack = create_extra_features(train_stack)
 test_stack = create_extra_features(test_stack)
 train_stack['bin_sum'] = train_stack[col_binary].sum(axis=1)
 test_stack['bin_sum'] = test_stack[col_binary].sum(axis=1 )
train.insert(8,"DayCount",daycount,False)
 test.insert(6,"DayCount",daytest,False )<define_variables>
col = [c for c in train_stack.columns if c not in ['id','target']]
col = [c for c in col if not c.startswith('ps_calc_')]
traincount = int(len(train["Date"]))
testcount = int(len(test["Date"])) 
<categorify>
dups = train_stack[train_stack.duplicated(subset=col, keep=False)]
train.Province_State = train.Province_State.fillna(0)
 empty = 0
for i in range(0,traincount):
if(train.Province_State[i] == empty):
train.Province_State[i] = train.Country_Region[i]<categorify>
train_stack = train_stack[~(train_stack.index.isin(dups.index)) ]
test.Province_State = test.Province_State.fillna(0)
 empty = 0
for i in range(0,testcount):
if(test.Province_State[i] == empty):
test.Province_State[i] = test.Country_Region[i]<categorify>
target_stack = train_stack['target']
label = preprocessing.LabelEncoder()
train.Country_Region = label.fit_transform(train.Country_Region)
 train.Province_State = label.fit_transform(train.Province_State )<categorify>
train_stack = train_stack[col]
test.Country_Region = label.fit_transform(test.Country_Region)
 test.Province_State = label.fit_transform(test.Province_State)
<prepare_x_and_y>
test_stack = test_stack[col]
X = np.c_[train["Province_State"], train["Country_Region"], train["DayCount"], train["Month"], train["Day"]]
Xt = np.c_[test["Province_State"], test["Country_Region"], test["DayCount"], test["Month"], test["Day"]]<prepare_x_and_y>
meta_data = get_meta(train_stack)
 col_ordinal = meta_data[(meta_data.level == 'ordinal')&(meta_data.keep)&(meta_data.role != 'target')].index
col_nominal = meta_data[(meta_data.level == 'nominal')&(meta_data.keep)&(meta_data.role != 'target')].index
col_interval = meta_data[(meta_data.level == 'interval')&(meta_data.keep)&(meta_data.role != 'target')].index
col_binary = meta_data[(meta_data.level == 'binary')&(meta_data.keep)&(meta_data.role != 'target')].index
Y1 = train["ConfirmedCases"]
Y2 = train["Fatalities"]<choose_model_class>
X_stack = pd.concat([train_stack[col_interval],train_stack[col_ordinal], pd.get_dummies(train_stack[col_binary])], axis=1)
 test_stack_val = pd.concat([test_stack[col_interval],test_stack[col_ordinal], pd.get_dummies(test_stack[col_binary])], axis=1)
 y_stack = target_stack
regr = XGBRegressor(n_estimators = 1500, gamma = 0, learning_rate = 0.8, random_state = 42, max_depth = 50, subsample = 1, reg_lambda = 0, reg_alpha = 0.5)
 regr1 = XGBRegressor(n_estimators = 1500, gamma = 0, learning_rate = 0.8, random_state = 42, max_depth = 50, subsample = 1, reg_lambda = 0, reg_alpha = 0.5 )<compute_test_metric>
X_stack = X_stack.drop(columns=['gender_-1','facebook_profile_-1'], axis = 1)
def rmsle(y_true, y_pred):
return mean_squared_log_error(y_true, y_pred)**(1/2);<train_model>
cross_val_model(X_stack, y_stack, rf_model )
regr.fit(X,Y1.ravel())
 yscore = regr.predict(X)
 a = abs(yscore)
 b=abs(Y1)
 ascore =(rmsle(a,b))
print("RMSLE of Confirmed Cases is ",ascore )<predict_on_test>
cross_val_model(X_stack, y_stack, XGB_model )
ypred = regr.predict(Xt)
 ypred = pd.DataFrame({'ConfirmedCases' : ypred})
 ypred = round(ypred)
 ypred.head(20 )<compute_test_metric>
class Ensemble(object):
def __init__(self, n_splits, stacker, base_models):
self.n_splits = n_splits
self.stacker = stacker
self.base_models = base_models
def fit_predict(self, X, y, T):
X = np.array(X)
 y = np.array(y)
 T = np.array(T)
 folds = list(StratifiedKFold(n_splits=self.n_splits, shuffle=True, random_state=2017 ).split(X, y))
S_train = np.zeros(( X.shape[0], len(self.base_models)))
 S_test = np.zeros(( T.shape[0], len(self.base_models)))
 for i, clf in enumerate(self.base_models):
S_test_i = np.zeros(( T.shape[0], self.n_splits))
for j,(train_idx, test_idx)in enumerate(folds):
X_train = X[train_idx]
y_train = y[train_idx]
X_holdout = X[test_idx]
print("Fit %s fold %d" %(str(clf ).split('(')[0], j+1))
clf.fit(X_train, y_train)
 y_pred = clf.predict_proba(X_holdout)[:,1]
S_train[test_idx, i] = y_pred
S_test_i[:, j] = clf.predict_proba(T)[:,1]
S_test[:, i] = S_test_i.mean(axis=1)
 results = cross_val_score(self.stacker, S_train, y, cv=3, scoring='roc_auc')
 print("Stacker score: %.5f" %(results.mean()))
self.stacker.fit(S_train, y)
 res = self.stacker.predict_proba(S_test)[:,1]
return res
regr1.fit(X,Y2.ravel())
 ypred2 = regr1.predict(Xt)
 yptest = regr1.predict(X)
 yptest = np.round(yptest)
 c = abs(yptest)
 d = abs(Y2)
 ascore = rmsle(c,d)
 print("RMSLE of Fatalities is", ascore)
<prepare_output>
rf_params = {}
rf_params['n_estimators'] = 80
rf_params['max_depth'] = 12
rf_params['min_samples_split'] = 50
rf_params['min_samples_leaf'] = 23
ypred2 = pd.DataFrame({'Fatalities' : ypred2})
 ypred2 = round(ypred2)
 ypred2.head(20 )<create_dataframe>
xgb_params = {}
xgb_params['learning_rate'] =0.03660642032718193
xgb_params['n_estimators'] = 70
xgb_params['max_depth'] = 7
xgb_params['reg_alpha'] = 0.1
xgb_params['reg_lambda'] = 0.1
xgb_params['colsample_bytree'] = 0.6162725690461764
xgb_params['min_child_weight'] = 0.751826989118936
ypc = pd.DataFrame()
forecast = test["ForecastId"]<prepare_output>
rf_model = RandomForestClassifier(**rf_params, random_state=584867 )
ypc.insert(0,"ForecastId",forecast,False)
 ypc.insert(1,"ConfirmedCases",ypred,False)
 ypc.insert(2, "Fatalities",ypred2,False )<save_to_csv>
xgb_model = XGBClassifier(**xgb_params, random_state=2943 )
ypc.to_csv('submission.csv', index=False )<import_modules>
log_model = LogisticRegression(random_state=29 )
import numpy as np
import pandas as pd
import datetime as datetime
from sklearn import preprocessing<set_options>
stack = Ensemble(n_splits=3,
stacker = log_model,
base_models =(rf_model, xgb_model))
register_matplotlib_converters() 
<import_modules>
X_stack.fillna(-1, inplace = True)
 test_stack_val.fillna(-1,inplace=True)
 y_pred = stack.fit_predict(X_stack, target_stack, test_stack_val )
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestRegressor
from xgboost import XGBRegressor<load_from_csv>
sub = pd.DataFrame()
sub['ids'] = test_id
sub['prob'] = y_pred
sub.to_csv('stacked_main.csv', index=False )