Kaggle competition: COVID19 Global Forecasting (Week 2)
def ensemble_structure(number_models, num_features, MODEL_ROOT):
if number_models == 1:
hidden_dims = [150]
number_of_dims = [num_features]
input_dims = [np.arange(num_features)]
elif os.path.isfile(MODEL_ROOT + "/" + 'hidden_dims.pkl'):
with open(MODEL_ROOT + "/" + 'hidden_dims.pkl', 'rb')as handle:
hidden_dims = pickle.load(handle)
 with open(MODEL_ROOT + "/" + 'number_of_dims.pkl', 'rb')as handle:
number_of_dims = pickle.load(handle)
 with open(MODEL_ROOT + "/" + 'input_dims.pkl', 'rb')as handle:
input_dims = pickle.load(handle)
 else:
hidden_dims = np.random.randint(96, 160, number_models)
 number_of_dims = np.random.randint(int(num_features*0.7), num_features, number_models)
 input_dims = []
for i in range(number_models):
input_dims.append(np.random.randint(0, num_features, number_of_dims[i]))
with open(MODEL_ROOT + "/" + 'hidden_dims.pkl', 'wb')as handle:
pickle.dump(hidden_dims, handle, protocol=pickle.HIGHEST_PROTOCOL)
 with open(MODEL_ROOT + "/" + 'number_of_dims.pkl', 'wb')as handle:
pickle.dump(number_of_dims, handle, protocol=pickle.HIGHEST_PROTOCOL)
 with open(MODEL_ROOT + "/" + 'input_dims.pkl', 'wb')as handle:
pickle.dump(input_dims, handle, protocol=pickle.HIGHEST_PROTOCOL)
 return hidden_dims, number_of_dims, input_dims 
<feature_engineering>
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import os
def fold_selection(folds, kf=0, random = False,
date=450, Debug=False):
folds['fold']=int(0)
 if Debug==True:
val = folds.sample(n=int(folds.shape[0]/10))
folds.loc[val.index, 'fold'] = int(1)
 else:
if random==True:
val = folds.sample(n=int(folds.shape[0] / 10))
folds.loc[val.index, 'fold'] = int(1)
 elif kf>0:
group_kfold = GroupKFold(n_splits=kf)
 group = folds['date']
f = 1
for train_index, test_index in group_kfold.split(folds, folds['resp'], group):
folds.loc[test_index, 'fold'] = int(f)
 f = f+1
else:
folds.loc[folds['date']>date, 'fold'] = int(1)
 folds['fold'] = folds['fold'].astype(int)
 return folds<choose_model_class>
data = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-2/train.csv',index_col='Date',parse_dates=True )
class simple_linear_layer(nn.Module):
def __init__(self, input_dim, out_dim):
super(simple_linear_layer, self ).__init__()
self.dense = nn.Linear(input_dim, out_dim)
 self.batch_norm = nn.BatchNorm1d(out_dim)
 self.dropout = nn.Dropout(0.2)
 def forward(self, x):
x = self.dense(x)
 x = self.batch_norm(x)
 x = self.dropout(x)
 return x
class MLP_Model(nn.Module):
def __init__(self, input_dim, hidden_dim, out_dim=4):
super(MLP_Model, self ).__init__()
self.block1 = simple_linear_layer(input_dim, hidden_dim)
 self.block2 = simple_linear_layer(hidden_dim, hidden_dim)
 self.block3 = nn.Linear(int(hidden_dim), out_dim)
 def forward(self, x):
v = F.relu(self.block1(x))
v = F.relu(self.block2(v))
out = self.block3(v)
 return out
class ResidualBlock(nn.Module):
def __init__(self, input_dim, hidden_dim, output_dim, downsample=None):
super(ResidualBlock, self ).__init__()
self.fc1 = nn.Linear(input_dim, hidden_dim)
 self.bn1 = nn.BatchNorm1d(hidden_dim)
 self.relu1 = nn.ReLU()
self.dropout1 = nn.Dropout(0.2)
 self.fc2 = nn.Linear(hidden_dim, output_dim)
 self.bn2 = nn.BatchNorm1d(output_dim)
 self.relu2 = nn.ReLU()
self.dropout2 = nn.Dropout(0.2)
 self.downsample = downsample
def forward(self, x):
residual = x
out = self.fc1(x)
 out = self.bn1(out)
 out = self.relu1(out)
 out = self.dropout1(out)
 out1 = self.fc2(out)
 out1 = self.bn2(out1)
 out1 = self.relu2(out1)
 out1 = self.dropout2(out1)
 if self.downsample:
residual = self.downsample(residual)
 out1 = torch.cat([out1, residual], dim=1)
 return out1
class ResNet(nn.Module):
def __init__(self, input_dim, hidden_dim, block, num_classes=4):
super(ResNet, self ).__init__()
self.layer1 = self.make_layer(block, input_dim, hidden_dim, hidden_dim)
 self.layer2 = self.make_layer(block, 2*hidden_dim, hidden_dim, hidden_dim)
 self.out = nn.Linear(2*hidden_dim, num_classes)
 def make_layer(self, block, input_dim, hidden_dim, output_dim):
downsample = None
if(input_dim != output_dim):
downsample = nn.Sequential(
 nn.Linear(input_dim, output_dim))
layer=block(input_dim, hidden_dim, output_dim, downsample)
 return layer
def forward(self, x):
out = self.layer1(x)
 out = self.layer2(out)
 out = self.out(out)
 return out
class Ensemble_MoaModel(nn.Module):
def __init__(self, input_dims, number_of_dims, hidden_dims,
model_name, out_dim=4):
super(Ensemble_MoaModel, self ).__init__()
self.models = torch.nn.ModuleList()
self.input_dims = input_dims
self.model_name = model_name
for i in range(len(hidden_dims)) :
if self.model_name=='Simple_MLP':
self.models.append(MLP_Model(input_dim=number_of_dims[i],
hidden_dim=hidden_dims[i], out_dim=out_dim))
elif self.model_name=="ResNet":
self.models.append(ResNet(input_dim=number_of_dims[i],
hidden_dim=hidden_dims[i],
block=ResidualBlock,
num_classes=out_dim))
else:
print("Please check model name.There is no this model!!!")
 def forward(self, x):
out = []
for i in range(len(self.input_dims)) :
temp = self.models[i](x[:, self.input_dims[i]])
 out.append(temp.unsqueeze(0))
out = torch.cat(out, dim=0)
 out = out.permute(1, 0, 2)
 return out<categorify>
data = data.fillna(value='empty' )
class JaneDataset:
def __init__(self, features, targets):
self.features = features
self.targets = targets
def __len__(self):
return(self.features.shape[0])
 def __getitem__(self, idx):
dct = {
'x': torch.tensor(self.features[idx, :], dtype=torch.float),
'y': torch.tensor(self.targets[idx], dtype=torch.float),
}
return dct
class TestDataset:
def __init__(self, features):
self.features = features
def __len__(self):
return(self.features.shape[0])
 def __getitem__(self, idx):
dct = {
'x': torch.tensor(self.features[idx, :], dtype=torch.float)
 }
return dct<train_model>
data['state_with_country'] = data['Province_State'] +'_'+ data['Country_Region']
def train_fn(model, optimizer, scheduler,
loss_fn, dataloader, device):
model.train()
final_loss = 0
train_preds = []
train_y = []
for data in dataloader:
optimizer.zero_grad()
inputs, targets = data['x'].to(device), data['y'].to(device)
 train_y.append(targets.detach().cpu().numpy())
 outputs = model(inputs)
 loss = 0
for i in range(outputs.shape[1]):
loss = loss + loss_fn(outputs[:, i],
targets.to(device))
loss.backward()
optimizer.step()
final_loss += loss.item()
train_preds.append(np.mean(outputs.sigmoid().detach().cpu().numpy() , axis=1))
final_loss /= len(dataloader)
 train_preds = np.concatenate(train_preds)
 train_y = np.concatenate(train_y)
 auc = roc_auc_score(train_y, train_preds)
 return auc<compute_train_metric>
data= data.drop(labels=['Province_State','Country_Region','Id'],axis=1 )
def valid_fn(model, df, dataloader, device,
scheduler=None, loss_fn=None):
model.eval()
final_loss = 0
valid_preds = []
val_y = []
for data in dataloader:
inputs, targets = data['x'].to(device), data['y'].to(device)
 val_y.append(targets.detach().cpu().numpy())
 outputs = model(inputs)
 if loss_fn==None:
valid_preds.append(np.mean(outputs.sigmoid().detach().cpu().numpy() , axis=1))
final_loss = 0
else:
loss = loss_fn(torch.mean(outputs, dim=1), targets.to(device))
final_loss += loss.item()
valid_preds.append(np.mean(outputs.sigmoid().detach().cpu().numpy() , axis=1))
final_loss /= len(dataloader)
 valid_preds = np.concatenate(valid_preds)
 val_y = np.concatenate(val_y)
 actual = np.where(valid_preds>=0.5, 1, 0)
 score = utility_score_numba(df['date'].values, df['weight'].values,
df['resp'].values, actual[:, 0])
 auc = roc_auc_score(val_y, valid_preds)
 if scheduler!=None:
scheduler.step(auc)
 return auc, score, valid_preds<prepare_x_and_y>
state_i_data =[]
for i in state_with_country_name:
state_i_data.append(i)
def run_training(folds, target, feature_cols, target_cols,
input_dims, number_of_dims, hidden_dims,
model_name, BATCH_SIZE, LEARNING_RATE, WEIGHT_DECAY,
EPOCHS, EARLY_STOP, EARLY_STOPPING_STEPS, fold, seed, MODEL_ROOT):
seed_everything(seed)
 train_df = folds.reset_index(drop=True)
 valid_df = folds[folds['fold'] == fold].reset_index(drop=True)
 x_train, y_train = train_df[feature_cols].values, train_df[target].values
x_valid, y_valid = valid_df[feature_cols].values, valid_df[target].values
train_dataset = JaneDataset(x_train, y_train)
 valid_dataset = JaneDataset(x_valid, y_valid)
 trainloader = DataLoader(train_dataset, batch_size=BATCH_SIZE, shuffle=True)
 validloader = DataLoader(valid_dataset, batch_size=BATCH_SIZE, shuffle=False)
 if os.path.isfile(MODEL_ROOT + "/"+ f"FOLD_{fold}_{seed}.pth"):
model_new = torch.load(MODEL_ROOT + "/"+ f"FOLD_{fold}_{seed}.pth")
 model_new.to(DEVICE)
 model_new.eval()
valid_tpr, valid_score, valid_preds = valid_fn(model_new, valid_df, validloader, DEVICE, scheduler=None, loss_fn=None)
 oof = valid_preds
else:
model_new = Ensemble_MoaModel(input_dims, number_of_dims, hidden_dims, model_name, out_dim=len(target_cols))
print(model_new)
 model_new.to(DEVICE)
 optimizer = torch.optim.Adam(model_new.parameters() , lr=LEARNING_RATE, weight_decay=WEIGHT_DECAY)
 scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer=optimizer, mode='max', factor=0.1, patience=3, verbose=True)
 loss_fn = nn.BCEWithLogitsLoss()
early_stopping_steps = EARLY_STOPPING_STEPS
early_step = 0
best_tpr = -np.inf
for epoch in range(EPOCHS):
train_loss = train_fn(model_new, optimizer, scheduler
, loss_fn, trainloader, DEVICE)
 valid_tpr, valid_score, valid_preds = valid_fn(model_new, valid_df, validloader, DEVICE, scheduler=scheduler, loss_fn=loss_fn)
 print(f"FOLD: {fold}, EPOCH: {epoch}, train_auc: {train_loss}, valid_auc: {valid_tpr}, valid_score: {valid_score}")
 if valid_tpr > best_tpr:
best_tpr = valid_tpr
oof = valid_preds
early_step = 0
torch.save(model_new, MODEL_ROOT + "/"+ f"FOLD_{fold}_{seed}.pth")
 elif(EARLY_STOP == True):
early_step += 1
if(early_step >= early_stopping_steps):
break
del train_loss, valid_tpr, valid_score
gc.collect()
model_new = torch.load(MODEL_ROOT + "/"+ f"FOLD_{fold}_{seed}.pth")
 model_new.to(DEVICE)
 return oof, valid_df<define_variables>
for i,j in enumerate(state_with_country_name):
state_i_data[i] = data[data['state_with_country'] == j ]
state_i_data[i] = state_i_data[i].drop(['state_with_country'],axis=1)
EPOCHS = 50
BATCH_SIZE = 5000
LEARNING_RATE = 1e-3
WEIGHT_DECAY = 1e-5
EARLY_STOPPING_STEPS = 10
EARLY_STOP = True
Date = 475
Debug = False
Random = True
kf=0<define_variables>
prev_sub = pd.read_csv('/kaggle/input/covid19-forecasting-using-rnn/submission.csv')
 test_whole = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-2/test.csv',index_col='Date',parse_dates=True)
 test_whole = test_whole.fillna(value='empty')
target_cols = ['resp', 'resp_1', 'resp_2', 'resp_3', 'resp_4']
useless_cols = ['date', 'weight', 'ts_id']
target = ['action', 'action_1', 'action_2', 'action_3', 'action_4']<load_pretrained>
test_whole['state_with_country'] = test_whole['Province_State'] +'_'+ test_whole['Country_Region']
test_whole.head()
inference = True
if inference==True:
model = torch.load('.. /input/prerained-model-with-50-epoch/ResNet_5/FOLD_1_10.pth')
 f_mean = np.load('.. /input/prerained-model-with-50-epoch/fmean.npy')
 with open('.. /input/prerained-model-with-50-epoch/feature_cols.pkl', 'rb')as handle:
feature_cols = pickle.load(handle)
 env = janestreet.make_env()
for(test_df, pred_df)in tqdm(env.iter_test()):
if test_df['weight'].item() > 0:
x_tst = test_df.loc[:, feature_cols].values
if np.isnan(x_tst[:, :].sum()):
x_tst[:, 1:] = np.nan_to_num(x_tst[:, 1:])+ np.isnan(x_tst[:, 1:])* f_mean
pred = np.zeros(( 1, len(target_cols)))
 pred = np.mean(model(torch.tensor(x_tst, dtype=torch.float ).to(DEVICE)).sigmoid().detach().cpu().numpy() , axis=1)
 pred = np.mean(pred, axis=1)
 pred_df.action = np.where(pred >= 0.5, 1, 0 ).astype(int)
 else:
pred_df.action = 0
env.predict(pred_df)
 else:
train = dt.fread('.. /input/jane-street-market-prediction/train.csv' ).to_pandas()
feature_cols = list(train.drop(useless_cols + target_cols, axis=1))
for action, col in zip(target, target_cols):
train[action] = np.where(train[col]>0, 1, 0)
 for col in feature_cols:
if train[col].isnull().sum() /train.shape[0]>0.05:
feature_cols.remove(col)
 with open('feature_cols.pkl', 'wb')as handle:
pickle.dump(feature_cols, handle, protocol=pickle.HIGHEST_PROTOCOL)
 train = train[train['weight'] != 0]
train = train.loc[train['date']>85]
train = train.reset_index(drop=True)
 train.fillna(train.mean() ,inplace=True)
 f_mean = np.mean(train[feature_cols[1:]].values,axis=0)
 np.save('fmean.npy', f_mean)
 list_number_of_models=[5]
model_names = ["ResNet"]
SEED = 10
num_features=len(feature_cols)
 num_targets=len(target_cols)
 weights_mod = [0.5, 0.5]
oof_ensemble = np.zeros(( int(train.shape[0]/5), len(target_cols)))
 for m, model_name in enumerate(model_names):
for w, number_of_models in enumerate(list_number_of_models):
MODEL_ROOT = "%s_%s" %(model_name, number_of_models)
 fold_name = 'fold.pkl'
if not os.path.exists(MODEL_ROOT):
os.makedirs(MODEL_ROOT)
 train = fold_selection(train, kf=kf, random=Random, date=Date, Debug=Debug)
 hidden_dims, number_of_dims, input_dims = ensemble_structure(number_of_models, num_features, MODEL_ROOT)
 if kf==0:
oof, val_df = run_training(folds=train, target=target, feature_cols=feature_cols,
target_cols=target_cols, input_dims=input_dims,
number_of_dims=number_of_dims, hidden_dims=hidden_dims,
model_name=model_name, BATCH_SIZE=BATCH_SIZE, LEARNING_RATE=LEARNING_RATE,
WEIGHT_DECAY=WEIGHT_DECAY, EPOCHS=EPOCHS, EARLY_STOP=EARLY_STOP,
EARLY_STOPPING_STEPS=EARLY_STOPPING_STEPS, fold=1, seed=SEED,
MODEL_ROOT=MODEL_ROOT)
 for th in range(490, 520):
action = np.where(oof>th/1000, 1, 0)
 print(model_name, th, 'score:', utility_score_numba(date=val_df['date'].values, weight=val_df['weight'].values,
resp=val_df['resp'].values, action=action[:, 0]))
else:
for fold in range(kf):
oof, val_df = run_training(folds=train, target=target, feature_cols=feature_cols,
target_cols=target_cols, input_dims=input_dims,
number_of_dims=number_of_dims, hidden_dims=hidden_dims,
model_name=model_name, BATCH_SIZE=BATCH_SIZE, LEARNING_RATE=LEARNING_RATE,
WEIGHT_DECAY=WEIGHT_DECAY, EPOCHS=EPOCHS, EARLY_STOP=EARLY_STOP,
EARLY_STOPPING_STEPS=EARLY_STOPPING_STEPS, fold=fold+1, seed=SEED,
MODEL_ROOT=MODEL_ROOT)
 for th in range(490, 520):
action = np.where(oof>th/1000, 1, 0)
 print(model_name, th, 'score:', utility_score_numba(date=val_df['date'].values, weight=val_df['weight'].values,
resp=val_df['resp'].values, action=action[:, 0]))<load_from_csv>
prev_sub.index = test_whole.index
df = dtable.fread('/kaggle/input/jane-street-market-prediction/train.csv' ).to_pandas()
df = df.query('date > 85' ).reset_index(drop = True)
 df = df[df.weight > 0]
df.reset_index(inplace=True, drop=True)
 df = df.astype({c: np.float32 for c in df.select_dtypes(include='float64' ).columns})
 df_labels = df[['date', 'weight', 'resp_1', 'resp_2', 'resp_3', 'resp_4', 'resp']]
features = [c for c in df.columns if 'feature' in c]
df = df.drop(df_labels.columns, axis=1)
 df.fillna(df.mean() , inplace=True)
 resp_cols = ['resp_1', 'resp_2', 'resp_3', 'resp_4', 'resp']
X = df[features]
y = np.stack([(df_labels[c] > 0 ).astype('int')for c in resp_cols] ).T
f_mean = np.mean(df[features[1:]].values, axis=0)
 device = 'cuda' if t.cuda.is_available() else 'cpu'<choose_model_class>
prev_sub['state_with_country'] = test_whole['state_with_country']
class Model(nn.Module):
def __init__(self, input_size):
super(Model, self ).__init__()
self.batch_norm0 = nn.BatchNorm1d(input_size)
 self.layer1 = LinBnDrop(input_size, 256, bn=True, p=0, act=Mish() , lin_first=False)
 a = LinBnDrop(256, 256, bn=True, p=.12, act=Mish() , lin_first=False)
 b = LinBnDrop(256, 256, bn=True, p=.12, act=Mish() , lin_first=False)
 self.res1 = SequentialEx(a, b, MergeLayer())
 self.layer2 = LinBnDrop(256, 512, bn=True, p=0.12, act=Mish() , lin_first=False)
 a = LinBnDrop(512, 512, bn=True, p=.12, act=Mish() , lin_first=False)
 b = LinBnDrop(512, 512, bn=True, p=.12, act=Mish() , lin_first=False)
 self.res2 = SequentialEx(a, b, MergeLayer())
 self.layer3 = LinBnDrop(512, 640, bn=True, p=0.12, act=Mish() , lin_first=False)
 a = LinBnDrop(640, 640, bn=True, p=.12, act=Mish() , lin_first=False)
 b = LinBnDrop(640, 640, bn=True, p=.12, act=Mish() , lin_first=False)
 self.res3 = SequentialEx(a, b, MergeLayer())
 self.layer4 = LinBnDrop(640, 256, bn=True, p=0.12, act=Mish() , lin_first=False)
 a = LinBnDrop(256, 256, bn=True, p=.12, act=Mish() , lin_first=False)
 b = LinBnDrop(256, 256, bn=True, p=.12, act=Mish() , lin_first=False)
 self.res4 = SequentialEx(a, b, MergeLayer())
 self.layer5 = LinBnDrop(256, 128, bn=True, p=0.12, act=Mish() , lin_first=False)
 a = LinBnDrop(128, 128, bn=True, p=.12, act=Mish() , lin_first=False)
 b = LinBnDrop(128, 128, bn=True, p=.12, act=Mish() , lin_first=False)
 self.res5 = SequentialEx(a, b, MergeLayer())
 self.layer6 = nn.Linear(128, 5)
 def forward(self, x):
x = self.batch_norm0(x)
 x = self.layer1(x)
 x = self.res1(x)
 x = self.layer2(x)
 x = self.res2(x)
 x = self.layer3(x)
 x = self.res3(x)
 x = self.layer4(x)
 x = self.res4(x)
 x = self.layer5(x)
 x = self.res5(x)
 x = self.layer6(x)
 return x 
<categorify>
forecast = []
for i in state_with_country_name:
forecast.append(i )
@njit
def fillna_npwhere_njit(array, values):
if np.isnan(array.sum()):
array = np.where(np.isnan(array), values, array)
 return array
def for_loop(method, matrix, values):
for i in range(matrix.shape[0]):
matrix[i] = method(matrix[i], values)
 return matrix<load_pretrained>
for i,j in enumerate(state_with_country_name):
forecast[i] = prev_sub[prev_sub['state_with_country'] == j ]
forecast[i] = forecast[i].iloc[13:21]
forecast[i] = forecast[i].drop(['ForecastId','state_with_country'],axis=1)
 state_i_data[i] = state_i_data[i].append(forecast[i] )
model_nn = Model(len(features))
model_nn = model_nn.to(device)
 learn = Learner(None, model_nn, loss_func=1)
 learn.load('/kaggle/input/roclossjs5/deep_resnet_model' )<find_best_params>
test_whole = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-2/test.csv',index_col='Date',parse_dates=True)
 test_whole = test_whole.fillna(value='empty' )
%%time
%%capture
env = janestreet.make_env()
learn.model.eval()
preds = []
for(test_df, pred_df)in tqdm(env.iter_test()):
if test_df['weight'].item() > 0:
x_tt = test_df.loc[:, features].values
x_tt[:, 1:] = for_loop(fillna_npwhere_njit, x_tt[:, 1:], f_mean)
 pred = 0.
 pred = learn.model(t.from_numpy(x_tt ).to(device, t.float)).sigmoid() [0][-1].item()
preds.append(pred)
 action = 1 if pred >=.5 else 0
pred_df.action = action
else:
pred_df.action = 0
env.predict(pred_df )<compute_test_metric>
test_whole['state_with_country'] = test_whole['Province_State'] +'_'+ test_whole['Country_Region']
test_whole.head()
preds = np.array(preds)
 preds.mean() , preds.std() , sum(preds >=.5), sum(preds < 5 )<import_modules>
state_i_data_for_test=[]
for i in state_with_country_name_for_test:
state_i_data_for_test.append(i)
import random
from tensorflow.keras.layers import Input, Dense, BatchNormalization, Dropout, Concatenate, Lambda, GaussianNoise, Activation
from tensorflow.keras.models import Model, Sequential
from tensorflow.keras.losses import BinaryCrossentropy
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.callbacks import EarlyStopping
from tensorflow.keras.layers.experimental.preprocessing import Normalization
import tensorflow as tf
import numpy as np
import pandas as pd
from tqdm import tqdm
from random import choices
from sklearn.model_selection import train_test_split
import lightgbm as lgbm
import pickle<load_pretrained>
for i,j in enumerate(state_with_country_name_for_test):
state_i_data_for_test[i] = test_whole[test_whole['state_with_country'] == j ]
state_i_data_for_test[i] = state_i_data_for_test[i].iloc[21:]
state_i_data_for_test[i] = state_i_data_for_test[i].drop(['ForecastId','state_with_country','Province_State','Country_Region'],axis=1)
 state_i_data_for_test.append(state_i_data_for_test[i] )
def load_pickle(fich):
with open(fich,"rb")as f :
contenu = pickle.load(f)
 return contenu<define_variables>
from sklearn.preprocessing import MinMaxScaler
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense,LSTM
from tensorflow.keras.preprocessing.sequence import TimeseriesGenerator
path = ".. /input/eda-missing-values-tsne-clustering-class-imb"<define_variables>
result = []
full_scaler = MinMaxScaler()
SEED = 1111<define_variables>
for i in range(len(state_with_country_name_for_test)) :
scaled_full_data = full_scaler.fit_transform(state_i_data[i])
 length = 1
batch_size = 1
generator = TimeseriesGenerator(scaled_full_data, scaled_full_data, length=length, batch_size=1)
 model = Sequential()
model.add(LSTM(96,input_shape=(length,scaled_full_data.shape[1])))
 model.add(Dense(scaled_full_data.shape[1]))
model.compile(optimizer='adam', loss='mse')
 model.fit_generator(generator,epochs=6)
 n_features = scaled_full_data.shape[1]
test_predictions = []
first_eval_batch = scaled_full_data[-length:]
current_batch = first_eval_batch.reshape(( 1, length, n_features))
for j in range(len(state_i_data_for_test[i])) :
current_pred = model.predict(current_batch)[0]
test_predictions.append(current_pred)
 current_batch = np.append(current_batch[:,1:,:],[[current_pred]],axis=1)
 true_predictions = full_scaler.inverse_transform(test_predictions)
 true_predictions = true_predictions.round()
true_predictions = pd.DataFrame(data=true_predictions,columns=state_i_data[1].columns)
 result.append(true_predictions)
 print('count:-',i)
random.seed(SEED)
 np.random.seed(SEED)
 tf.random.set_seed(SEED )<load_from_csv>
prediction = pd.DataFrame(data= state_i_data[0].iloc[-21:] ,columns=['ConfirmedCases','Fatalities'])
 prediction = prediction.append(result[0] )
print('load train start')
 try:
train = pd.read_parquet('train.parquet')
 except:
train = pd.read_csv('.. /input/jane-street-market-prediction/train.csv', index_col='ts_id')
 train.to_parquet('train.parquet')
 print('load train end' )<feature_engineering>
for i in range(1,len(result)) :
prediction = prediction.append(state_i_data[i].iloc[-21:])
 prediction = prediction.append(result[i] )
train = train.query('date > 85' ).reset_index(drop = True)
 train = train[train['weight'] != 0]
train.fillna(train.mean() ,inplace=True)
 train['action'] =(( train['resp'].values)> 0 ).astype(int)
 features = load_pickle(os.path.join(path,"features_names"))
f_mean = load_pickle(os.path.join(path,"features_imputation"))
with open("imputation","wb")as f :
pickle.dump(f_mean,f)
 resp_cols = ['resp_1', 'resp_2', 'resp_3', 'resp', 'resp_4']
X_train = train.loc[:, features]
y_train = np.stack([(train[c] > 0 ).astype('int')for c in resp_cols] ).T<split>
prediction.index = range(0,len(prediction))
params={"num_leaves":300,
"max_bin":450,
"feature_fraction":0.52,
"bagging_fraction":0.52,
"objective":"binary",
"learning_rate":0.05,
"boosting_type":"gbdt",
"metric":"auc"
}
models = []
for i in tqdm(range(y_train.shape[1])) :
xtr,xval,ytr,yval = train_test_split(X_train ,y_train[:,i],test_size=0.2,stratify=y_train[:,i])
 d_train = lgbm.Dataset(xtr,label=ytr)
 d_eval = lgbm.Dataset(xval,label=yval,reference=d_train)
 clf = lgbm.train(params,d_train,valid_sets=[d_train,d_eval],num_boost_round=1500,\
early_stopping_rounds=50,verbose_eval=50)
 nom_fich = "weights_target_" + resp_cols[i]
clf.save_model(nom_fich)
 models.append(clf )<choose_model_class>
sub_format = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-2/submission.csv' )
params={"num_leaves":300,
"max_bin":450,
"feature_fraction":0.52,
"bagging_fraction":0.52,
"objective":"binary",
"learning_rate":0.05,
"boosting_type":"gbdt",
"metric":"auc"
}
sing_model = []
for i in range(3):
clf = lgbm.Booster(model_file=os.path.join(path,"weights_{}".format(i)))
 sing_model.append(clf )<predict_on_test>
sub_format = sub_format['ForecastId']
f = np.median
th = 0.5000
env = janestreet.make_env()
for(test_df, pred_df)in tqdm(env.iter_test()):
if test_df['weight'].item() > 0:
x_tt = test_df.loc[:, features].values
if np.isnan(x_tt.sum()):
x_tt = np.nan_to_num(x_tt)+ np.isnan(x_tt)* f_mean
pred1 = f(np.stack([model.predict(x_tt)for model in models]),axis=0 ).T
pred2 = np.mean(np.stack([model.predict(x_tt)for model in sing_model]),axis=0)
 pred = 0.5 * pred1 + 0.5 * pred2
pred_df.action = np.where(pred >= th, 1, 0 ).astype(int)
 else:
pred_df.action = 0
env.predict(pred_df )<load_from_disk>
final = pd.concat([sub_format,prediction],axis=1 )
train_df=pd.read_json('.. /input/train.json')
 test_df=pd.read_json('.. /input/test.json' )<feature_engineering>
final.to_csv('submission.csv',index=False )