Kaggle competition: COVID19 Global Forecasting (Week 4)
EMBEDDING_FILE = '.. /input/embeddings/paragram_300_sl999/paragram_300_sl999.txt'
def get_coefs(word,*arr): return word, np.asarray(arr, dtype='float32')
 embeddings_index = dict(get_coefs(*o.split(" ")) for o in open(EMBEDDING_FILE, encoding="utf8", errors='ignore')if len(o)>100)
 all_embs = np.stack(embeddings_index.values())
 emb_mean,emb_std = all_embs.mean() , all_embs.std()
embed_size = all_embs.shape[1]
word_index = tokenizer.word_index
nb_words = min(max_features, len(word_index))
embedding_matrix = np.random.normal(emb_mean, emb_std,(nb_words, embed_size))
for word, i in word_index.items() :
if i >= max_features: continue
embedding_vector = embeddings_index.get(word)
 if embedding_vector is not None: embedding_matrix[i] = embedding_vector
inp = Input(shape=(maxlen,))
x = Embedding(max_features, embed_size, weights=[embedding_matrix], trainable=False )(inp)
 x = Bidirectional(CuDNNLSTM(128, return_sequences=True))(x)
 x = Bidirectional(CuDNNLSTM(64, return_sequences=True))(x)
 x = Attention(maxlen )(x)
 x = Dense(64, activation="relu" )(x)
 x = Dense(1, activation="sigmoid" )(x)
 model = Model(inputs=inp, outputs=x)
 model.compile(loss='binary_crossentropy', optimizer=Adam(lr=1e-3), metrics=['accuracy'] )<train_model>
plt.style.use('ggplot')
 font = {'family' : 'meiryo'}
plt.rc('font', **font )
model.fit(train_X, train_y, batch_size=512, epochs=3, validation_data=(val_X, val_y))<predict_on_test>
os.environ['PYTHONHASHSEED'] = '0'
np.random.seed(42)
 rn.seed(12345)
 session_conf = tf.compat.v1.ConfigProto(intra_op_parallelism_threads=1, inter_op_parallelism_threads=1)
 tf.random.set_seed(1234)
 sess = tf.compat.v1.Session(graph=tf.compat.v1.get_default_graph() , config=session_conf)
 tf.compat.v1.keras.backend.set_session(sess )
pred_paragram_val_y = model.predict([val_X], batch_size=1024, verbose=1)
 for thresh in np.arange(0.1, 0.501, 0.01):
thresh = np.round(thresh, 2)
 print("F1 score at threshold {0} is {1}".format(thresh, metrics.f1_score(val_y,(pred_paragram_val_y>thresh ).astype(int))))<predict_on_test>
train_df = pd.read_csv(".. /input/covid19-global-forecasting-week-4/train.csv")
 train_df = train_df.fillna("No State")
 train_df
pred_paragram_test_y = model.predict([test_X], batch_size=1024, verbose=1 )<set_options>
test_rate = 0.1
time_series_len = 18
train_date_count = len(set(train_df["Date"]))
X, Y = [],[]
scaler = StandardScaler()
train_df["ConfirmedCases_std"] = scaler.fit_transform(train_df["ConfirmedCases"].values.reshape(len(train_df["ConfirmedCases"].values),1))
for state,country in train_df.groupby(["Province_State","Country_Region"] ).sum().index:
df = train_df[(train_df["Country_Region"] == country)&(train_df["Province_State"] == state)]
if df["ConfirmedCases"].sum() != 0:
for i in range(len(df)- time_series_len):
X.append(df[['ConfirmedCases_std']].iloc[i:(i+time_series_len)].values)
 Y.append(df[['ConfirmedCases_std']].iloc[i+time_series_len].values)
 X=np.array(X)
 Y=np.array(Y)
 X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=test_rate, shuffle = True ,random_state = 0 )
del word_index, embeddings_index, all_embs, embedding_matrix, model, inp, x
time.sleep(10 )<compute_test_metric>
def huber_loss(y_true, y_pred, clip_delta=1.0):
error = y_true - y_pred
cond = tf.keras.backend.abs(error)< clip_delta
squared_loss = 0.5 * tf.keras.backend.square(error)
 linear_loss = clip_delta *(tf.keras.backend.abs(error)- 0.5 * clip_delta)
 return tf.where(cond, squared_loss, linear_loss)
 def huber_loss_mean(y_true, y_pred, clip_delta=1.0):
return tf.keras.backend.mean(huber_loss(y_true, y_pred, clip_delta))
pred_val_y = 0.39*pred_glove_val_y + 0.41*pred_fasttext_val_y + 0.39*pred_paragram_val_y
for thresh in np.arange(0.1, 0.501, 0.01):
thresh = np.round(thresh, 2)
 print("F1 score at threshold {0} is {1}".format(thresh, metrics.f1_score(val_y,(pred_val_y>thresh ).astype(int))))<save_to_csv>
epochs_num = 20
n_in = 1
model = Sequential()
model.add(GRU(100,
batch_input_shape=(None, time_series_len, n_in),
kernel_initializer=random_uniform(seed=1),
return_sequences=False 
))
model.add(Dense(50))
model.add(Dropout(0.15))
model.add(Dense(n_in, kernel_initializer=random_uniform(seed=1)))
 model.add(Activation("linear"))
opt = Adagrad(lr=0.01, epsilon=1e-08, decay=1e-4)
 model.compile(loss = huber_loss_mean, optimizer=opt )
pred_test_y = 0.35*pred_glove_test_y + 0.42*pred_fasttext_test_y + 0.35*pred_paragram_test_y
pred_test_y =(pred_test_y>0.42 ).astype(int)
 out_df = pd.DataFrame({"qid":test_df["qid"].values})
 out_df['prediction'] = pred_test_y
out_df.to_csv("submission.csv", index=False )<load_from_csv>
callbacks = [ReduceLROnPlateau(monitor='loss', patience=4, verbose=1, factor=0.6),
EarlyStopping(monitor='loss', patience=10)]
hist = model.fit(X_train, Y_train, batch_size=20, epochs=epochs_num,
callbacks=callbacks,shuffle=False )
train_df = pd.read_csv('.. /input/train.csv')
 X_train = train_df["question_text"].fillna("dieter" ).values
test_df = pd.read_csv('.. /input/test.csv')
 X_test = test_df["question_text"].fillna("dieter" ).values
y = train_df["target"]<import_modules>
predicted_std = model.predict(X_test)
 result_std= pd.DataFrame(predicted_std)
 result_std.columns = ['predict']
result_std['actual'] = Y_test
result_std.plot(figsize=(25,6))
plt.show()
from keras.models import Model
from keras.layers import Input, Dense, Embedding, concatenate
from keras.layers import CuDNNGRU, Bidirectional, GlobalAveragePooling1D, GlobalMaxPooling1D, Conv1D
from keras.layers import Add, BatchNormalization, Activation, CuDNNLSTM, Dropout
from keras.layers import *
from keras.models import *
from keras.preprocessing import text, sequence
from keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau
import gc
from sklearn import metrics<string_transform>
predicted = scaler.inverse_transform(predicted_std)
 Y_test2 = scaler.inverse_transform(Y_test )
maxlen = 70
max_features = 50000
embed_size = 300
tokenizer = text.Tokenizer(num_words=max_features)
 tokenizer.fit_on_texts(list(X_train)+ list(X_test))
X_train = tokenizer.texts_to_sequences(X_train)
 X_test = tokenizer.texts_to_sequences(X_test)
 x_train = sequence.pad_sequences(X_train, maxlen=maxlen)
 x_test = sequence.pad_sequences(X_test, maxlen=maxlen )<categorify>
np.sqrt(mean_squared_log_error(predicted, Y_test2))
def attention_3d_block(inputs):
TIME_STEPS = inputs.shape[1].value
SINGLE_ATTENTION_VECTOR = False
input_dim = int(inputs.shape[2])
 a = Permute(( 2, 1))(inputs)
 a = Reshape(( input_dim, TIME_STEPS))(a)
 a = Dense(TIME_STEPS, activation='softmax' )(a)
 if SINGLE_ATTENTION_VECTOR:
a = Lambda(lambda x: K.mean(x, axis=1))(a)
 a = RepeatVector(input_dim )(a)
 a_probs = Permute(( 2, 1))(a)
 output_attention_mul = Multiply()([inputs, a_probs])
 return output_attention_mul<feature_engineering>
result= pd.DataFrame(predicted)
 result.columns = ['predict']
result['actual'] = Y_test2
result.plot(figsize=(25,6))
plt.show()
class AttLayer(Layer):
def __init__(self, attention_dim):
self.init = initializers.get('normal')
 self.supports_masking = True
self.attention_dim = attention_dim
super(AttLayer, self ).__init__()
def build(self, input_shape):
assert len(input_shape)== 3
self.W = K.variable(self.init(( input_shape[-1], self.attention_dim)))
 self.b = K.variable(self.init(( self.attention_dim,)))
 self.u = K.variable(self.init(( self.attention_dim, 1)))
 self.trainable_weights = [self.W, self.b, self.u]
super(AttLayer, self ).build(input_shape)
 def compute_mask(self, inputs, mask=None):
return mask
def call(self, x, mask=None):
uit = K.tanh(K.bias_add(K.dot(x, self.W), self.b))
ait = K.dot(uit, self.u)
 ait = K.squeeze(ait, -1)
 ait = K.exp(ait)
 if mask is not None:
ait *= K.cast(mask, K.floatx())
 ait /= K.cast(K.sum(ait, axis=1, keepdims=True)+ K.epsilon() , K.floatx())
 ait = K.expand_dims(ait)
 weighted_input = x * ait
output = K.sum(weighted_input, axis=1)
 return output
def compute_output_shape(self, input_shape):
return(input_shape[0], input_shape[-1] )<statistical_test>
test_df = pd.read_csv(".. /input/covid19-global-forecasting-week-4/test.csv" )
EMBEDDING_FILE = '.. /input/embeddings/glove.840B.300d/glove.840B.300d.txt'
def get_coefs(word,*arr): return word, np.asarray(arr, dtype='float32')
 embeddings_index = dict(get_coefs(*o.split(" ")) for o in open(EMBEDDING_FILE))
all_embs = np.stack(embeddings_index.values())
 emb_mean,emb_std = all_embs.mean() , all_embs.std()
embed_size = all_embs.shape[1]
word_index = tokenizer.word_index
nb_words = min(max_features, len(word_index))
embedding_matrix_1 = np.random.normal(emb_mean, emb_std,(nb_words, embed_size))
for word, i in word_index.items() :
if i >= max_features: continue
embedding_vector = embeddings_index.get(word)
 if embedding_vector is not None: embedding_matrix_1[i] = embedding_vector
del embeddings_index; gc.collect()<statistical_test>
submission = pd.read_csv(".. /input/covid19-global-forecasting-week-4/submission.csv" )
EMBEDDING_FILE = '.. /input/embeddings/wiki-news-300d-1M/wiki-news-300d-1M.vec'
def get_coefs(word,*arr): return word, np.asarray(arr, dtype='float32')
 embeddings_index = dict(get_coefs(*o.split(" ")) for o in open(EMBEDDING_FILE)if len(o)>100)
 all_embs = np.stack(embeddings_index.values())
 emb_mean,emb_std = all_embs.mean() , all_embs.std()
embed_size = all_embs.shape[1]
word_index = tokenizer.word_index
nb_words = min(max_features, len(word_index))
embedding_matrix_2 = np.random.normal(emb_mean, emb_std,(nb_words, embed_size))
for word, i in word_index.items() :
if i >= max_features: continue
embedding_vector = embeddings_index.get(word)
 if embedding_vector is not None: embedding_matrix_2[i] = embedding_vector
del embeddings_index; gc.collect()<statistical_test>
temp =(datetime.datetime.strptime("2020-04-01", '%Y-%m-%d')- datetime.timedelta(days=time_series_len)).strftime('%Y-%m-%d')
 test_df = train_df[train_df["Date"] > temp]
EMBEDDING_FILE = '.. /input/embeddings/paragram_300_sl999/paragram_300_sl999.txt'
def get_coefs(word,*arr): return word, np.asarray(arr, dtype='float32')
 embeddings_index = dict(get_coefs(*o.split(" ")) for o in open(EMBEDDING_FILE, encoding="utf8", errors='ignore')if len(o)>100)
 all_embs = np.stack(embeddings_index.values())
 emb_mean,emb_std = all_embs.mean() , all_embs.std()
embed_size = all_embs.shape[1]
word_index = tokenizer.word_index
nb_words = min(max_features, len(word_index))
embedding_matrix_3 = np.random.normal(emb_mean, emb_std,(nb_words, embed_size))
for word, i in word_index.items() :
if i >= max_features: continue
embedding_vector = embeddings_index.get(word)
 if embedding_vector is not None: embedding_matrix_3[i] = embedding_vector
del embeddings_index; gc.collect()<features_selection>
check_df = pd.read_csv(".. /input/covid19-global-forecasting-week-4/train.csv" ).query("Date>'2020-04-01'and Date<='2020-04-14'")
 check_df["ConfirmedCases_std"] = scaler.transform(check_df["ConfirmedCases"].values.reshape(len(check_df["ConfirmedCases"].values),1))
EMBEDDING_FILE = '.. /input/embeddings/GoogleNews-vectors-negative300/GoogleNews-vectors-negative300.bin'
embeddings_index = KeyedVectors.load_word2vec_format(EMBEDDING_FILE, binary=True)
 word_index = tokenizer.word_index
nb_words = min(max_features, len(word_index))
embedding_matrix_4 =(np.random.rand(nb_words, embed_size)- 0.5)/ 5.0
for word, i in word_index.items() :
if i >= max_features: continue
if word in embeddings_index:
embedding_vector = embeddings_index.get_vector(word)
 embedding_matrix_4[i] = embedding_vector
del embeddings_index; gc.collect()<concatenate>
confirmedCases_pred = []
for i in range(0,313*time_series_len,time_series_len):
temp_array = np.array(test_df["ConfirmedCases_std"][i:i+time_series_len])
 for j in range(43):
if j<13:
temp_array = np.append(temp_array,np.array(check_df["ConfirmedCases_std"])[int(i*13/time_series_len)+j])
 elif np.array(test_df["ConfirmedCases"][i:i+time_series_len] ).sum() == 0:
temp_array = np.append(temp_array,temp_array[-1])
 else:
temp_array = np.append(temp_array,model.predict(temp_array[-time_series_len:].reshape(1,time_series_len,1)))
 confirmedCases_pred.append(temp_array[-43:] )
embedding_matrix = np.concatenate(( embedding_matrix_1, embedding_matrix_2, embedding_matrix_3, embedding_matrix_4), axis=1)
 del embedding_matrix_1, embedding_matrix_2, embedding_matrix_3, embedding_matrix_4
gc.collect()
np.shape(embedding_matrix )<choose_model_class>
submission["ConfirmedCases"] = np.abs(scaler.inverse_transform(np.array(confirmedCases_pred ).reshape(313*43)))
 submission["ConfirmedCases_std"] = np.array(confirmedCases_pred ).reshape(313*43)
 submission
def model1(init):
x = Reshape(( maxlen, embed_size * 4, 1))(init)
 filter_sizes = [1,2,3]
num_filters = 32
conv_0 = Conv2D(num_filters, kernel_size=(filter_sizes[0], embed_size), padding='valid', kernel_initializer='normal', activation='relu' )(x)
 conv_1 = Conv2D(num_filters, kernel_size=(filter_sizes[1], embed_size), padding='valid', kernel_initializer='normal', activation='relu' )(x)
 conv_2 = Conv2D(num_filters, kernel_size=(filter_sizes[2], embed_size), padding='valid', kernel_initializer='normal', activation='relu' )(x)
 maxpool_0 = MaxPool2D(pool_size=(maxlen - filter_sizes[0] + 1, 1), strides=(1,1), padding='valid' )(conv_0)
 maxpool_1 = MaxPool2D(pool_size=(maxlen - filter_sizes[1] + 1, 1), strides=(1,1), padding='valid' )(conv_1)
 maxpool_2 = MaxPool2D(pool_size=(maxlen - filter_sizes[2] + 1, 1), strides=(1,1), padding='valid' )(conv_2)
 concatenated_tensor = Concatenate(axis=1 )([maxpool_0, maxpool_1, maxpool_2])
 gmp = GlobalMaxPooling2D()(concatenated_tensor)
 gap = GlobalAveragePooling2D()(concatenated_tensor)
 conc = Concatenate(axis=1 )([gmp, gap])
 return conc
def model2(init):
x = init
x = Bidirectional(CuDNNLSTM(128, return_sequences=True))(x)
 x = attention_3d_block(x)
 x = Bidirectional(CuDNNLSTM(64, return_sequences=True))(x)
 x = AttLayer(64 )(x)
 return x
def model3(init):
filters = 64
x = init
pool = []
x = Conv1D(filters, 1, activation='relu' )(init)
 x = MaxPooling1D(2 )(x)
 x = Dropout(0.1 )(x)
 x = Conv1D(filters, 2, activation='relu' )(x)
 x = MaxPooling1D(2 )(x)
 x = Dropout(0.1 )(x)
 x = Conv1D(filters, 3, activation='relu' )(x)
 x = MaxPooling1D(2 )(x)
 x = Dropout(0.1 )(x)
 x = Conv1D(filters, 5, activation='relu' )(x)
 x = MaxPooling1D(2 )(x)
 x = Dropout(0.1 )(x)
 x = Flatten()(x)
 return x
def model4(init):
x = init
x = Bidirectional(CuDNNGRU(128, return_sequences=True))(x)
 x = attention_3d_block(x)
 x = Bidirectional(CuDNNGRU(64, return_sequences=True))(x)
 x = AttLayer(64 )(x)
 return x
def all_embed(init):
x = init
out3 = model3(x)
 return out3
def get_model() :
inp = Input(shape=(maxlen,))
embed1 = Embedding(max_features, embed_size * 4, weights=[embedding_matrix], trainable=False )(inp)
 out = all_embed(embed1)
 x = Dropout(0.3 )(out)
 x = Dense(128, activation='relu' )(x)
 outp = Dense(1, activation="sigmoid" )(x)
 model = Model(inputs=inp, outputs=outp)
 model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'])
 return model<split>
submission.to_csv('./submission_c.csv')
 submission.to_csv('.. \output\kaggle\working\submission_c.csv' )
X_tra, X_val, y_tra, y_val = train_test_split(x_train, y, test_size = 0.1, random_state=42 )<train_model>
test_rate = 0.1
time_series_len = 16
train_date_count = len(set(train_df["Date"]))
X, Y = [],[]
scaler = StandardScaler()
train_df["Fatalities_std"] = scaler.fit_transform(train_df["Fatalities"].values.reshape(len(train_df["Fatalities"].values),1))
ss = StandardScaler()
train_df["ConfirmedCases_std"] = ss.fit_transform(train_df["ConfirmedCases"].values.reshape(len(train_df["ConfirmedCases"].values),1))
for state,country in train_df.groupby(["Province_State","Country_Region"] ).sum().index:
df = train_df[(train_df["Country_Region"] == country)&(train_df["Province_State"] == state)]
if df["Fatalities"].sum() != 0 or df["ConfirmedCases"].sum() != 0:
for i in range(len(df)- time_series_len):
X.append(df[['Fatalities_std','ConfirmedCases_std']].iloc[i:(i+time_series_len)].values)
 Y.append(df[['Fatalities_std']].iloc[i+time_series_len].values)
 X=np.array(X)
 Y=np.array(Y)
 X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=test_rate, shuffle = True ,random_state = 0 )
early_stopping = EarlyStopping(patience=3, verbose=1, monitor='val_loss', mode='min')
 model_checkpoint = ModelCheckpoint('./quora.model', save_best_only=True, verbose=1, monitor='val_loss', mode='min')
 reduce_lr = ReduceLROnPlateau(factor=0.5, patience=3, min_lr=0.0001, verbose=1)
 batch_size = 1536
epochs = 3
hist = model.fit(X_tra, y_tra, batch_size=batch_size, epochs=epochs, validation_data=(X_val, y_val), verbose=True )<find_best_params>
epochs_num = 21
n_in = 2
model = Sequential()
model.add(GRU(100,
batch_input_shape=(None, time_series_len, n_in),
kernel_initializer=random_uniform(seed=1),
return_sequences=False))
model.add(Dense(50))
model.add(Dropout(0.15))
model.add(Dense(1, kernel_initializer=random_uniform(seed=1)))
 model.add(Activation("linear"))
opt = Adagrad(lr=0.01, epsilon=1e-08, decay=1e-4)
 model.compile(loss = huber_loss_mean, optimizer=opt )
pred_val_y = model.predict([X_val], batch_size=1024, verbose=1)
 thresholds = []
for thresh in np.arange(0.1, 0.501, 0.01):
thresh = np.round(thresh, 2)
 res = metrics.f1_score(y_val,(pred_val_y > thresh ).astype(int))
thresholds.append([thresh, res])
 print("F1 score at threshold {0} is {1}".format(thresh, res))
thresholds.sort(key=lambda x: x[1], reverse=True)
 best_thresh = thresholds[0][0]
print("Best threshold: ", best_thresh )<save_to_csv>
callbacks = [ReduceLROnPlateau(monitor='loss', patience=4, verbose=1, factor=0.6),
EarlyStopping(monitor='loss', patience=10)]
hist = model.fit(X_train, Y_train, batch_size=16, epochs=epochs_num,
callbacks=callbacks,shuffle=False )
y_pred = model.predict(x_test, batch_size=1024, verbose=True)
 y_te =(y_pred[:,0] > best_thresh ).astype(np.int)
 submit_df = pd.DataFrame({"qid": test_df["qid"], "prediction": y_te})
 submit_df.to_csv("submission.csv", index=False )<save_to_csv>
predicted_std = model.predict(X_test)
 result_std= pd.DataFrame(predicted_std)
 result_std.columns = ['predict']
result_std['actual'] = Y_test
def create_download_link(df, title = "Download CSV file", filename = "data.csv"):
csv = df.to_csv(index =False)
 b64 = base64.b64encode(csv.encode())
 payload = b64.decode()
html = '<a download="{filename}" href="data:text/csv;base64,{payload}" target="_blank">{title}</a>'
html = html.format(payload=payload,title=title,filename=filename)
 return HTML(html)
 create_download_link(submit_df )<import_modules>
predicted = scaler.inverse_transform(predicted_std)
 Y_test = scaler.inverse_transform(Y_test )
import os
import time
import numpy as np
import pandas as pd
from tqdm import tqdm
import math
from sklearn.model_selection import train_test_split
from sklearn import metrics
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from keras.layers import Dense, Input, LSTM, Embedding, Dropout, Activation, CuDNNGRU, Conv1D
from keras.layers import Bidirectional, GlobalMaxPool1D
from keras.models import Model
from keras import initializers, regularizers, constraints, optimizers, layers
from keras.models import Model
from keras.layers import Input, Embedding, Dense, Conv2D, MaxPool2D
from keras.layers import Reshape, Flatten, Concatenate, Dropout, SpatialDropout1D
from keras.preprocessing import text, sequence
from keras.callbacks import Callback
import warnings
from keras.models import Sequential
from keras.layers import CuDNNLSTM, Dense, Bidirectional, Dropout
from keras.callbacks import Callback
from sklearn.metrics import confusion_matrix, f1_score, precision_score, recall_score<load_from_csv>
X_test_ = scaler.inverse_transform(X_test )
train_df = pd.read_csv(".. /input/train.csv")
 test_df = pd.read_csv(".. /input/test.csv")
 print("Train shape : ",train_df.shape)
 print("Test shape : ",test_df.shape )<split>
np.sqrt(mean_squared_log_error(predicted, Y_test))
train_df, val_df = train_test_split(train_df, test_size=0.1, random_state=2018)
 embed_size = 300
max_features = 50000
maxlen = 100
train_X = train_df["question_text"].fillna("_na_" ).values
val_X = val_df["question_text"].fillna("_na_" ).values
test_X = test_df["question_text"].fillna("_na_" ).values
tokenizer = Tokenizer(num_words=max_features)
 tokenizer.fit_on_texts(list(train_X))
train_X = tokenizer.texts_to_sequences(train_X)
 val_X = tokenizer.texts_to_sequences(val_X)
 test_X = tokenizer.texts_to_sequences(test_X)
 train_X = pad_sequences(train_X, maxlen=maxlen)
 val_X = pad_sequences(val_X, maxlen=maxlen)
 test_X = pad_sequences(test_X, maxlen=maxlen)
 train_y = train_df['target'].values
val_y = val_df['target'].values<compute_test_metric>
temp =(datetime.datetime.strptime("2020-04-01", '%Y-%m-%d')- datetime.timedelta(days=time_series_len)).strftime('%Y-%m-%d')
 test_df = train_df[train_df["Date"] > temp]
def f1(y_true, y_pred):
def recall(y_true, y_pred):

 true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
 possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
 recall = true_positives /(possible_positives + K.epsilon())
 return recall
def precision(y_true, y_pred):

 true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
 predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
 precision = true_positives /(predicted_positives + K.epsilon())
 return precision
precision = precision(y_true, y_pred)
 recall = recall(y_true, y_pred)
 return 2*(( precision*recall)/(precision+recall+K.epsilon()))
class Metrics(Callback):
def on_train_begin(self, logs={}):
self.val_f1s = []
self.val_recalls = []
self.val_precisions = []
def on_epoch_end(self, epoch, logs={}):
val_predict =(np.asarray(self.model.predict(self.model.validation_data[0])) ).round()
val_targ = self.model.validation_data[1]
_val_f1 = f1_score(val_targ, val_predict)
 _val_recall = recall_score(val_targ, val_predict)
 _val_precision = precision_score(val_targ, val_predict)
 self.val_f1s.append(_val_f1)
 self.val_recalls.append(_val_recall)
 self.val_precisions.append(_val_precision)
 print(" — val_f1: %f — val_precision: %f — val_recall %f" %(_val_f1, _val_precision, _val_recall))
return
my_metrics = Metrics()<import_modules>
check_df["Fatalities_std"] = scaler.transform(check_df["Fatalities"].values.reshape(len(check_df["Fatalities"].values),1))
check_df
from keras.models import Model
from keras.layers import Conv1D, Input, MaxPooling1D, Flatten, Dense, BatchNormalization, concatenate, SpatialDropout1D
from keras.layers.advanced_activations import LeakyReLU 
<categorify>
fatalities_pred = []
for i in range(0,313*time_series_len,time_series_len):
temp_array = np.array(test_df[["Fatalities_std","ConfirmedCases_std"]][i:i+time_series_len])
 for j in range(43):
if j<13:
temp_array = np.append(temp_array,np.append(np.array(check_df["Fatalities_std"])[int(i*13/time_series_len)+j],np.array(check_df["ConfirmedCases_std"])[int(i*13/time_series_len)+j] ).reshape(1,2),axis=0)
 elif np.array(test_df[["Fatalities","ConfirmedCases"]][i:i+time_series_len] ).sum() == 0:
temp_array = np.append(temp_array,np.array(temp_array[-1] ).reshape(1,2),axis=0)
 else:
temp_array = np.append(temp_array,np.append(model.predict(temp_array[-time_series_len:].reshape(1,time_series_len,2)) ,submission["ConfirmedCases_std"][i/time_series_len*43+j] ).reshape(1,2),axis=0)
 fatalities_pred.append(temp_array[-43:] )
def f1_loss(y_true, y_pred):

 true_truth = K.dot(y_true, K.constant([1., 0.], dtype='float32', shape=(2, 1)))
 true_false = K.dot(y_true, K.constant([0., 1.], dtype='float32', shape=(2, 1)))
 y_false = K.constant(1., dtype='float32')- y_true
fake_truth = K.dot(y_false, K.constant([1., 0.], dtype='float32', shape=(2, 1)))
 fake_false = K.dot(y_false, K.constant([0., 1.], dtype='float32', shape=(2, 1)))
 TP_temp = K.sum(true_truth * y_pred)
 FP_temp = K.sum(fake_truth * y_pred)
 FN_temp = K.sum(fake_false * y_pred)
 loss =(FP_temp + FN_temp)/(2 * TP_temp + FP_temp + FN_temp + K.epsilon() )<statistical_test>
submission["Fatalities"] = np.abs(scaler.inverse_transform([i[0] for i in np.array(fatalities_pred ).reshape(313*43,2)]))
submission
EMBEDDING_FILE = '.. /input/embeddings/glove.840B.300d/glove.840B.300d.txt'
def get_coefs(word,*arr): return word, np.asarray(arr, dtype='float32')
 embeddings_index = dict(get_coefs(*o.split(" ")) for o in open(EMBEDDING_FILE))
all_embs = np.stack(embeddings_index.values())
 emb_mean,emb_std = all_embs.mean() , all_embs.std()
embed_size = all_embs.shape[1]
word_index = tokenizer.word_index
nb_words = min(max_features, len(word_index))
embedding_matrix = np.random.normal(emb_mean, emb_std,(nb_words, embed_size))
for word, i in word_index.items() :
if i >= max_features: continue
embedding_vector = embeddings_index.get(word)
 if embedding_vector is not None: embedding_matrix[i] = embedding_vector<choose_model_class>
submission[["ConfirmedCases","Fatalities"]] = submission[["ConfirmedCases","Fatalities"]].round().astype(int)
 submission
STRIDE_1 = 2
STRIDE_2 = 4
STRIDE_3 = 8
FILTER_1 = 64
FILTER_2 = 64
FILTER_3 = 64
inp = Input(shape=(maxlen,))
embed_layer1 = Embedding(max_features, embed_size, weights=[embedding_matrix] )(inp)
 embed_layer1 = SpatialDropout1D(0.4 )(embed_layer1)
 line1 = Conv1D(FILTER_1, STRIDE_1 )(embed_layer1)
 line1 = Activation(LeakyReLU() )(line1)
 line1 = MaxPooling1D(STRIDE_1 )(line1)
 line1 = Conv1D(FILTER_1, STRIDE_1 )(line1)
 line1 = Activation(LeakyReLU() )(line1)
 line1 = MaxPooling1D(STRIDE_1 )(line1)
 line1 = Conv1D(FILTER_1, STRIDE_1 )(line1)
 line1 = Activation(LeakyReLU() )(line1)
 line1 = MaxPooling1D(STRIDE_1*2 )(line1)
 line1 = Flatten()(line1)
 line2 = Conv1D(FILTER_2, STRIDE_1 )(embed_layer1)
 line2 = Activation(LeakyReLU() )(line2)
 line2 = MaxPooling1D(STRIDE_1 )(line2)
 line2 = Conv1D(FILTER_2, STRIDE_1 )(line2)
 line2 = Activation(LeakyReLU() )(line2)
 line2 = MaxPooling1D(STRIDE_1 )(line2)
 line2 = Conv1D(FILTER_2, STRIDE_1 )(line2)
 line2 = Activation(LeakyReLU() )(line2)
 line2 = MaxPooling1D(STRIDE_1*2 )(line2)
 line2 = Flatten()(line2)
 line3 = Conv1D(FILTER_3, STRIDE_1 )(embed_layer1)
 line3 = Activation(LeakyReLU() )(line3)
 line3 = MaxPooling1D(STRIDE_1 )(line3)
 line3 = Conv1D(FILTER_3, STRIDE_1 )(line3)
 line3 = Activation(LeakyReLU() )(line3)
 line3 = MaxPooling1D(STRIDE_1 )(line3)
 line3 = Conv1D(FILTER_3, STRIDE_1 )(line3)
 line3 = Activation(LeakyReLU() )(line3)
 line3 = MaxPooling1D(STRIDE_1*2 )(line3)
 line3 = Flatten()(line3)
 concat_layer = concatenate([line1, line2, line3])
 conv1d_dense = Dense(1024, activation='relu' )(concat_layer)
 embed_layer2 = Embedding(max_features, embed_size, weights=[embedding_matrix] )(inp)
 embed_layer2 = SpatialDropout1D(0.4 )(embed_layer2)
 rnn_line = Bidirectional(CuDNNLSTM(64, return_sequences=True), input_shape=(maxlen, embed_size))(embed_layer2)
 rnn_line = Bidirectional(CuDNNLSTM(64))(rnn_line)
 rnn_dense = Dense(1024, activation='relu' )(rnn_line)
 filter_sizes = [1,2,3,5]
num_filters = 36
embed_layer3 = Embedding(max_features, embed_size, weights=[embedding_matrix] )(inp)
 embed_layer3 = SpatialDropout1D(0.4 )(embed_layer3)
 x = SpatialDropout1D(0.4 )(embed_layer3)
 x = Reshape(( maxlen, embed_size, 1))(x)
 conv_0 = Conv2D(num_filters, kernel_size=(filter_sizes[0], embed_size),
kernel_initializer='he_normal', activation='elu' )(x)
 conv_1 = Conv2D(num_filters, kernel_size=(filter_sizes[1], embed_size),
kernel_initializer='he_normal', activation='elu' )(x)
 conv_2 = Conv2D(num_filters, kernel_size=(filter_sizes[2], embed_size),
kernel_initializer='he_normal', activation='elu' )(x)
 conv_3 = Conv2D(num_filters, kernel_size=(filter_sizes[3], embed_size),
kernel_initializer='he_normal', activation='elu' )(x)
 maxpool_0 = MaxPool2D(pool_size=(maxlen - filter_sizes[0] + 1, 1))(conv_0)
 maxpool_1 = MaxPool2D(pool_size=(maxlen - filter_sizes[1] + 1, 1))(conv_1)
 maxpool_2 = MaxPool2D(pool_size=(maxlen - filter_sizes[2] + 1, 1))(conv_2)
 maxpool_3 = MaxPool2D(pool_size=(maxlen - filter_sizes[3] + 1, 1))(conv_3)
 cnn2d_dense = Concatenate(axis=1 )([maxpool_0, maxpool_1, maxpool_2, maxpool_3])
 cnn2d_dense = Flatten()(cnn2d_dense)
 cnn2d_dense = Dropout(0.1 )(cnn2d_dense)
 total = concatenate([cnn2d_dense, conv1d_dense, rnn_dense])
 preds = Dense(1, activation='sigmoid' )(total)
 model = Model(inputs=inp, outputs=preds )<choose_model_class>
submission = submission.drop("ConfirmedCases_std",axis=1 )
model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy', f1] )<train_model>
submission = submission.set_index('ForecastId' )
model.fit(train_X, train_y, batch_size=512, epochs=2, validation_data=(val_X, val_y))<predict_on_test>
for i in range(313):
for j in range(2,44):
if submission["ConfirmedCases"][i*43+j] < submission["ConfirmedCases"][i*43+j-1]:
submission["ConfirmedCases"][i*43+j] = submission["ConfirmedCases"][i*43+j-1]
if submission["Fatalities"][i*43+j] < submission["Fatalities"][i*43+j-1]:
submission["Fatalities"][i*43+j] = submission["Fatalities"][i*43+j-1]
<predict_on_test><EOS> 
submission.to_csv('submission.csv' )