Kaggle competition: COVID19 Global Forecasting (Week 2)
def create_mlp(
 num_columns, num_labels, hidden_units, dropout_rates, label_smoothing, learning_rate 
):
dropout_rate=0.2
hidden_size=256
inp = tf.keras.layers.Input(shape=(num_columns,))
x=tf.keras.layers.BatchNormalization()(inp)
 x=tf.keras.layers.Dropout(dropout_rate )(x)
 x1=tf.keras.layers.Dense(hidden_size )(x)
 x1=tf.keras.layers.BatchNormalization()(x1)
 x1=tf.keras.layers.LeakyReLU()(x1)
 x1=tf.keras.layers.Dropout(dropout_rate )(x1)
 x = tf.keras.layers.concatenate([x, x1], axis=1)
 x2=tf.keras.layers.Dense(hidden_size )(x)
 x2=tf.keras.layers.BatchNormalization(axis=1 )(x2)
 x2=tf.keras.layers.LeakyReLU()(x2)
 x2=tf.keras.layers.Dropout(dropout_rate )(x2)
 x = tf.keras.layers.concatenate([x1, x2], axis=1)
 x3=tf.keras.layers.Dense(hidden_size )(x)
 x3=tf.keras.layers.BatchNormalization(axis=1 )(x3)
 x3=tf.keras.layers.LeakyReLU()(x3)
 x3=tf.keras.layers.Dropout(dropout_rate )(x3)
 x = tf.keras.layers.concatenate([x2, x3], axis=1)
 x4=tf.keras.layers.Dense(hidden_size )(x)
 x4=tf.keras.layers.BatchNormalization(axis=1 )(x4)
 x4=tf.keras.layers.LeakyReLU()(x4)
 x4=tf.keras.layers.Dropout(dropout_rate )(x4)
 x = tf.keras.layers.concatenate([x3, x4], axis=1)
 x = tf.keras.layers.Dense(num_labels )(x)
 out = tf.keras.layers.Activation("sigmoid" )(x)
 model = tf.keras.models.Model(inputs=inp, outputs=out)
 model.compile(
 optimizer=tfa.optimizers.AdamW(learning_rate=learning_rate,weight_decay=1e-5),
loss=tf.keras.losses.BinaryCrossentropy(label_smoothing=label_smoothing),
metrics=tf.keras.metrics.AUC(name="AUC"), 
)
 return model<load_pretrained>
train = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-2/train.csv')
 test = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-2/test.csv')
 submission = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-2/test.csv' )
TRAINING=False
NUM_FOLDS=5
if not TRAINING:
models=[]
for i in range(NUM_FOLDS):
batch_size = 5000
hidden_units = [150, 150, 150]
dropout_rates = [0.2, 0.2, 0.2, 0.2]
label_smoothing = 0.005
learning_rate = 1e-3
clf = create_mlp(
 len(features), 5, hidden_units, dropout_rates, label_smoothing, learning_rate 
)
 clf.load_weights('.. /input/tensorflow-resnet-training/online_model_{}.weights'.format(i))
clf.call = tf.function(clf.call, experimental_relax_shapes=True)
 models.append(clf)
 th = 0.502
f = np.median
env = janestreet.make_env()
for(test_df, pred_df)in tqdm(env.iter_test()):
if test_df['weight'].item() > 0:
test_df=stats_features(test_df)
 x_tt = test_df.loc[:, features].values
if np.isnan(x_tt[:, 1:130].sum()):
x_tt[:, 1:130] = np.nan_to_num(x_tt[:, 1:130])+ np.isnan(x_tt[:, 1:130])* f_mean
pred = np.mean([model(x_tt, training=False ).numpy() for model in models], axis=0)
 pred = f(pred)
 pred_df.action = np.where(pred >= th, 1, 0 ).astype(int)
 else:
pred_df.action = 0
env.predict(pred_df )<import_modules>
print("Number of Country_Region: ", train['Country_Region'].nunique())
 print("Dates go from day", max(train['Date']), "to day", min(train['Date']), ", a total of", train['Date'].nunique() , "days")
 print("Countries with Province/State informed: ", train[train['Province_State'].isna() ==False]['Country_Region'].unique() )
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
import datatable as dt
from sklearn.model_selection import GroupKFold
from tqdm import tqdm
from random import choices
import kerastuner as kt
from sklearn.model_selection import KFold
from sklearn.model_selection._split import _BaseKFold, indexable, _num_samples
from sklearn.utils.validation import _deprecate_positional_args<import_modules>
EMPTY_VAL = "EMPTY_VAL"
def fillState(state, country):
if state == EMPTY_VAL: return country
return state
train['Province_State'].fillna(EMPTY_VAL, inplace=True)
 train['Province_State'] = train.loc[:, ['Province_State', 'Country_Region']].apply(lambda x : fillState(x['Province_State'], x['Country_Region']), axis=1)
 test['Province_State'].fillna(EMPTY_VAL, inplace=True)
 test['Province_State'] = test.loc[:, ['Province_State', 'Country_Region']].apply(lambda x : fillState(x['Province_State'], x['Country_Region']), axis=1)
 test.head()
from janestreet_utils import *<data_type_conversions>
train['row_number'] = train.groupby(['Country_Region', 'Province_State'] ).cumcount()
x = train[train["Country_Region"] == 'China'][train["Province_State"] == 'Hubei']['row_number']
y = train[train["Country_Region"] == 'China'][train["Province_State"] == 'Hubei']['ConfirmedCases']
y_ = train[train["Country_Region"] == 'China'][train["Province_State"] == 'Hubei']['Fatalities']
def f(x, L, b, k, x_0):
return L /(1.+ np.exp(-k *(x - x_0)))+ b
def logistic(xs, L, k, x_0):
result = []
for x in xs:
xp = k*(x-x_0)
 if xp >= 0:
result.append(L /(1.+ np.exp(-xp)))
 else:
result.append(L * np.exp(xp)/(1.+ np.exp(xp)))
 return result
p0 = [max(y), 0.0,max(x)]
p0_ = [max(y_), 0.0,max(x)]
x_ = np.arange(0, 100, 1 ).tolist()
try:
popt, pcov = opt.curve_fit(logistic, x, y,p0)
 yfit = logistic(x_, *popt)
 popt_, pcov_ = opt.curve_fit(logistic, x, y_,p0_)
 yfit_ = logistic(x_, *popt_)
 except:
popt, pcov = opt.curve_fit(f, x, y, method="lm", maxfev=5000)
 yfit = f(x_, *popt)
 popt_, pcov_ = opt.curve_fit(f, x, y_, method="lm", maxfev=5000)
 yfit_ = f(x_, *popt_)
 fig, ax = plt.subplots(1, 1, figsize=(10, 8))
ax.plot(x, y, 'o', label ='Actual Cases')
 ax.plot(x_, yfit, '-', label ='Fitted Cases')
 ax.plot(x, y_, 'o', label ='Actual Fatalities')
 ax.plot(x_, yfit_, '-', label ='Fitted fatalities')
 ax.title.set_text('China - Hubei province')
 plt.legend(loc="center right")
 plt.show()
%%time
TRAINING = False
USE_FINETUNE = True
FOLDS = 5
SEED = 42
train = dt.fread('.. /input/jane-street-market-prediction/train.csv' ).to_pandas()
train = train.query('date > 85' ).reset_index(drop = True)
 train = train.astype({c: np.float32 for c in train.select_dtypes(include='float64' ).columns})
 train.fillna(train.mean() ,inplace=True)
 train = train.query('weight > 0' ).reset_index(drop = True)
 train['action'] =(( train['resp_1'] > 0)&(train['resp_2'] > 0)&(train['resp_3'] > 0)&(train['resp_4'] > 0)&(train['resp'] > 0)).astype('int')
 features = [c for c in train.columns if 'feature' in c]
resp_cols = ['resp_1', 'resp_2', 'resp_3', 'resp', 'resp_4']
X = train[features].values
y = np.stack([(train[c] > 0 ).astype('int')for c in resp_cols] ).T
f_mean = np.mean(train[features[1:]].values,axis=0 )<categorify>
unique = pd.DataFrame(train.groupby(['Country_Region', 'Province_State'],as_index=False ).count())
 unique.head()
%%time
autoencoder, encoder = create_autoencoder(X.shape[-1],y.shape[-1],noise=0.1)
 encoder.load_weights('.. /input/jsmpdata-encoder/encoder.hdf5')
 encoder.trainable = False<load_pretrained>
def date_day_diff(d1, d2):
delta = dt.datetime.strptime(d1, "%Y-%m-%d")- dt.datetime.strptime(d2, "%Y-%m-%d")
 return delta.days
log_regions = []
for index, region in unique.iterrows() :
st = region['Province_State']
co = region['Country_Region']
rdata = train[(train['Province_State']==st)&(train['Country_Region']==co)]
t = rdata['Date'].values
t = [float(date_day_diff(d, t[0])) for d in t]
y = rdata['ConfirmedCases'].values
y_ = rdata['Fatalities'].values
p0 = [max(y), 0.0, max(t)]
p0_ = [max(y_), 0.0, max(t)]
try:
popt, pcov = opt.curve_fit(logistic, t, y, p0, maxfev=10000)
 try:
popt_, pcov_ = opt.curve_fit(logistic, t, y_, p0_, maxfev=10000)
 except:
popt_, pcov_ = opt.curve_fit(f, t, y_,method="trf", maxfev=10000)
 log_regions.append(( co,st,popt,popt_))
except:
popt, pcov = opt.curve_fit(f, t, y,method="trf", maxfev=10000)
 popt_, pcov_ = opt.curve_fit(f, t, y_,method="trf", maxfev=10000)
 log_regions.append(( co,st,popt,popt_))
model_fn = lambda hp: create_model(hp,X.shape[-1],y.shape[-1],encoder)
 tuner = CVTuner(
 hypermodel=model_fn,
oracle=kt.oracles.BayesianOptimization(
 objective= kt.Objective('val_auc', direction='max'),
num_initial_points=4,
max_trials=20))
FOLDS = 5
SEED = 42
models = []
hp = pd.read_pickle(f'.. /input/jsmpdata-tune/best_hp_{SEED}.pkl')
 for f in range(FOLDS):
model = model_fn(hp)
 if USE_FINETUNE:
model.load_weights(f'.. /input/jsmpdata-tune/model_{SEED}_{f}_finetune.hdf5')
 else:
model.load_weights(f'.. /input/jsmpdata-tune/model_{SEED}_{f}.hdf5')
 models.append(model)
<feature_engineering>
log_regions = pd.DataFrame(log_regions)
 log_regions.head()
f = np.median
models = models[-2:]
env = janestreet.make_env()
th = 0.5
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[:, 1:].sum()):
x_tt[:, 1:] = np.nan_to_num(x_tt[:, 1:])+ np.isnan(x_tt[:, 1:])* f_mean
pred = np.mean([model(x_tt, training = False ).numpy() for model in models],axis=0)
 pred = f(pred)
 pred_df.action = np.where(pred >= th, 1, 0 ).astype(int)
 else:
pred_df.action = 0
env.predict(pred_df )<import_modules>
log_regions.columns = ['Country_Region','Province_State','ConfirmedCases','Fatalities']
log_regions.head(1 )
import numpy as np
import pickle
import os
from sklearn.model_selection import GroupKFold
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.autograd import Variable
import random
from torch.utils.data import Dataset, DataLoader
import gc
from sklearn.metrics import precision_score, roc_auc_score
import pickle
import pandas as pd
import datatable as dt
import matplotlib.pylab as plt
import time<set_options>
for index, rt in log_regions.iterrows() :
st = rt['Province_State']
co = rt['Country_Region']
popt = list(['ConfirmedCases'])
 popt_ = list(rt['Fatalities'])
 print(co,st,popt,popt_ )
DEVICE =('cuda' if torch.cuda.is_available() else 'cpu')
 def seed_everything(seed=42):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True<compute_test_metric>
for index, rt in log_regions.iterrows() :
st = rt['Province_State']
co = rt['Country_Region']
popt = list(rt['ConfirmedCases'])
 popt_ = list(rt['Fatalities'])
 if popt_ == [0.0,0.0,69.0]:
popt_ = np.multiply(fp,popt)
 print(co,st,popt,popt_ )
def utility_score_numba(date, weight, resp, action):
Pi = np.bincount(date, weight * resp * action)
 t = np.sum(Pi)/ np.sqrt(np.sum(Pi ** 2)) * np.sqrt(250 / len(Pi))
u = min(max(t, 0), 6)* np.sum(Pi)
 return u<load_pretrained>
submission = pd.DataFrame(submission)
 submission.columns = ['ForecastId','ConfirmedCases','Fatalities']
submission.to_csv('./submission.csv', index = False )