Kaggle competition: COVID19 Global Forecasting (Week 4)
def MCRMSE(y_true, y_pred):
colwise_mse = tf.reduce_mean(tf.square(y_true - y_pred), axis=1)
 return tf.reduce_mean(tf.sqrt(colwise_mse), axis=1)
 def gru_layer(hidden_dim, dropout):
return tf.keras.layers.Bidirectional(
 tf.keras.layers.GRU(hidden_dim,
dropout=dropout,
return_sequences=True,
kernel_initializer = 'orthogonal'))
def lstm_layer(hidden_dim, dropout):
return tf.keras.layers.Bidirectional(
 tf.keras.layers.LSTM(hidden_dim,
dropout=dropout,
return_sequences=True,
kernel_initializer = 'orthogonal'))
def build_model(model_type=1, seq_len=107, pred_len=68, embed_dim=100,
dropout=dropout_model, hidden_dim_first = hidden_dim_first,
hidden_dim_second = hidden_dim_second, hidden_dim_third = hidden_dim_third):
inputs = tf.keras.layers.Input(shape=(seq_len, 3))
embed = tf.keras.layers.Embedding(input_dim=len(token2int), output_dim=embed_dim )(inputs)
 reshaped = tf.reshape(
 embed, shape=(-1, embed.shape[1], embed.shape[2] * embed.shape[3]))
reshaped = tf.keras.layers.SpatialDropout1D (.2 )(reshaped)
 if model_type == 0:
hidden = gru_layer(hidden_dim_first, dropout )(reshaped)
 hidden = gru_layer(hidden_dim_second, dropout )(hidden)
 hidden = gru_layer(hidden_dim_third, dropout )(hidden)
 elif model_type == 1:
hidden = lstm_layer(hidden_dim_first, dropout )(reshaped)
 hidden = lstm_layer(hidden_dim_second, dropout )(hidden)
 hidden = lstm_layer(hidden_dim_third, dropout )(hidden)
 elif model_type == 2:
hidden = gru_layer(hidden_dim_first, dropout )(reshaped)
 hidden = lstm_layer(hidden_dim_second, dropout )(hidden)
 hidden = lstm_layer(hidden_dim_third, dropout )(hidden)
 elif model_type == 3:
hidden = lstm_layer(hidden_dim_first, dropout )(reshaped)
 hidden = gru_layer(hidden_dim_second, dropout )(hidden)
 hidden = gru_layer(hidden_dim_third, dropout )(hidden)
 elif model_type == 4:
hidden = lstm_layer(hidden_dim_first, dropout )(reshaped)
 hidden = gru_layer(hidden_dim_second, dropout )(hidden)
 hidden = lstm_layer(hidden_dim_third, dropout )(hidden)
 truncated = hidden[:, :pred_len]
out = tf.keras.layers.Dense(5, activation='linear' )(truncated)
 model = tf.keras.Model(inputs=inputs, outputs=out)
 adam = tf.optimizers.Adam()
model.compile(optimizer=adam, loss=MCRMSE)
 return model<choose_model_class>
inputData = pd.read_csv(filepath["train"])
 inputData = inputData.rename(columns = {"Country_Region" : "Place"})
 for i in range(inputData.shape[0]):
if str(inputData['Province_State'][i])!= "nan":
inputData['Place'][i] = inputData['Province_State'][i] + ", " + inputData["Place"][i]
places = inputData.Place.unique()
inDates = inputData.Date.unique()
nplaces = len(places)
 nInDates = len(inDates)
 caseData = {}
fatalityData = {}
for date in inDates:
caseData[date] = []
fatalityData[date] = []
a = 0
b = nInDates
for i in range(313):
for j in range(a, b):
caseData[inputData['Date'][j]].append(inputData['ConfirmedCases'][j])
 fatalityData[inputData['Date'][j]].append(inputData['Fatalities'][j])
 a+=nInDates
b+=nInDates
casesdf = pd.DataFrame(caseData, index = [i for i in range(nplaces)])
 fataldf = pd.DataFrame(fatalityData, index = [i for i in range(nplaces)] )
def train_and_predict(n_folds=5, model_name="model", model_type=0, epochs=90, debug=False,
dropout_model=dropout_model, hidden_dim_first = hidden_dim_first,
hidden_dim_second = hidden_dim_second, hidden_dim_third = hidden_dim_third,
seed=seed):
print("Model:", model_name)
 ensemble_preds = pd.DataFrame(index=sample_sub.index, columns=target_cols ).fillna(0)
 kf = KFold(n_folds, shuffle=True, random_state=seed)
 skf = StratifiedKFold(n_folds, shuffle=True, random_state=seed)
 val_losses = []
historys = []
for i,(train_index, val_index)in enumerate(skf.split(train_inputs_all, train['SN_filter'])) :
print("Fold:", str(i+1))
model_train = build_model(model_type=model_type,
dropout=dropout_model,
hidden_dim_first = hidden_dim_first,
hidden_dim_second = hidden_dim_second,
hidden_dim_third = hidden_dim_third)
 model_short = build_model(model_type=model_type, seq_len=107, pred_len=107,
dropout=dropout_model,
hidden_dim_first = hidden_dim_first,
hidden_dim_second = hidden_dim_second,
hidden_dim_third = hidden_dim_third)
 model_long = build_model(model_type=model_type, seq_len=130, pred_len=130,
dropout=dropout_model,
hidden_dim_first = hidden_dim_first,
hidden_dim_second = hidden_dim_second,
hidden_dim_third = hidden_dim_third)
 train_inputs, train_labels = train_inputs_all[train_index], train_labels_all[train_index]
val_inputs, val_labels = train_inputs_all[val_index], train_labels_all[val_index]
checkpoint = tf.keras.callbacks.ModelCheckpoint(f'{model_name}.h5')
 history = model_train.fit(
 train_inputs , train_labels,
validation_data=(val_inputs,val_labels),
batch_size=64,
epochs=epochs,
callbacks=[tf.keras.callbacks.ReduceLROnPlateau() , checkpoint],
verbose=2 if debug else 0 
)
 print(f"{model_name} Min training loss={min(history.history['loss'])}, min validation loss={min(history.history['val_loss'])}")
 val_losses.append(min(history.history['val_loss']))
historys.append(history)
 model_short.load_weights(f'{model_name}.h5')
 model_long.load_weights(f'{model_name}.h5')
 public_preds = model_short.predict(public_inputs)
 private_preds = model_long.predict(private_inputs)
 preds_model = []
for df, preds in [(public_df, public_preds),(private_df, private_preds)]:
for i, uid in enumerate(df.id):
single_pred = preds[i]
single_df = pd.DataFrame(single_pred, columns=target_cols)
 single_df['id_seqpos'] = [f'{uid}_{x}' for x in range(single_df.shape[0])]
preds_model.append(single_df)
 preds_model_df = pd.concat(preds_model)
 ensemble_preds[target_cols] += preds_model_df[target_cols].values / n_folds
if debug:
print("Intermediate ensemble result")
 print(ensemble_preds[target_cols].head())
 ensemble_preds["id_seqpos"] = preds_model_df["id_seqpos"].values
ensemble_preds = pd.merge(sample_sub["id_seqpos"], ensemble_preds, on="id_seqpos", how="left")
 print("Mean Validation loss:", str(np.mean(val_losses)))
 if debug:
fig, ax = plt.subplots(1, 1, figsize =(10, 5))
for i, history in enumerate(historys):
ax.plot(history.history['loss'])
 ax.plot(history.history['val_loss'])
 ax.set_title('model_'+str(i+1))
ax.set_ylabel('Loss')
 ax.set_xlabel('Epoch')
 plt.show()
return ensemble_preds
public_df = test.query("seq_length == 107" ).copy()
private_df = test.query("seq_length == 130" ).copy()
public_inputs = preprocess_inputs(public_df)
 private_inputs = preprocess_inputs(private_df)
 ensembles = []
for i in range(5):
model_name = "model_"+str(i+1)
 ensemble = train_and_predict(n_folds=5, model_name=model_name, model_type=i, epochs=100,
dropout_model=dropout_model, hidden_dim_first = hidden_dim_first,
hidden_dim_second = hidden_dim_second, hidden_dim_third = hidden_dim_third,
seed=seed)
 ensembles.append(ensemble )<prepare_output>
outputData = pd.read_csv(filepath['test'])
 outputDates = outputData.Date.unique()
beginOutDate = outputDates[0]
endOutDate = outputDates[-1]
endInDate = inDates[-1]
outputDates = outputDates[np.where(outputDates == endInDate)[0][0]+1:]
outputDates
ensemble_final = ensembles[0].copy()
ensemble_final[target_cols] = 0
for ensemble in ensembles:
ensemble_final[target_cols] += ensemble[target_cols].values / len(ensembles)
 ensemble_final<save_to_csv>
mins = []
maxes = []
def norm(series):
if np.min(series.values)!= np.max(series.values):
return np.min(series.values), np.max(series.values),(series-np.min(series.values)) /(np.max(series.values)-np.min(series.values))
return 0,1,series
def invnorm(x, mini, maxi):
return x*(maxi-mini)+ mini
ensemble_final.to_csv('ensemble_final.csv', index=False )<set_options>
def custom_loss(actual, predicted):
return kb.maximum(0.0, kb.abs(actual-predicted)+kb.log(actual/predicted))
def get_model() :
model = keras.Sequential([
keras.layers.Dense(64, activation='relu', input_shape=[nInDates-1]),
keras.layers.Dense(32, activation='relu'),
keras.layers.Dense(1)
 ])
 optimizer = tf.keras.optimizers.RMSprop(0.001)
 model.compile(loss=custom_loss, optimizer = optimizer)
 return model
warnings.filterwarnings('ignore')
<define_variables>
normed_casesdf = casesdf.copy()
for i in range(313):
mini, maxi, normed = norm(casesdf.loc[i,:])
 mins.append(mini)
 maxes.append(maxi)
 for j in range(nInDates):
normed_casesdf[inDates[j]][i] = normed[j]
normed_casesdf
SEED = 731
def seed_everything(seed):
random.seed(seed)
 np.random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 tf.random.set_seed(seed)
 seed_everything(SEED )<define_variables>
epochs = [(i+1)*10 for i in range(10)]
cvscores = []
for epochsize in epochs:
totalloss = 0
print(epochsize)
 for i in range(10):
model = get_model()
validation_df = normed_casesdf.iloc[(31*i):(31*(i+1)) ]
training_df = normed_casesdf.copy()
training_df.drop(training_df.index[(31*i):(31*(i+1)) ])
 model.fit(training_df[training_df.columns[:-1]], training_df[training_df.columns[-1]], epochs = epochsize, verbose=0)
 loss = model.evaluate(validation_df[validation_df.columns[:-1]], validation_df[validation_df.columns[-1]])
 totalloss+=loss
avg = totalloss/10
cvscores.append(avg )
FOLDS = 6
epoch = 200
VERBOSE = 0<load_from_csv>
optimalepochsize = epochs[cvscores.index(min(cvscores)) ]
train = pd.read_json('/kaggle/input/stanford-covid-vaccine/train.json', lines=True)
 test = pd.read_json('/kaggle/input/stanford-covid-vaccine/test.json', lines=True)
 sample_sub = pd.read_csv('/kaggle/input/stanford-covid-vaccine/sample_submission.csv')
 target_cols = ['reactivity', 'deg_Mg_pH10', 'deg_pH10', 'deg_Mg_50C', 'deg_50C']
token2int = {x:i for i, x in enumerate('().ACGUBEHIMSX')}
def preprocess_inputs(df, cols=['sequence', 'structure', 'predicted_loop_type']):
return np.transpose(
 np.array(df[cols].applymap(lambda seq: [token2int[x] for x in seq] ).values.tolist()), 
(0, 2, 1))
train_inputs = preprocess_inputs(train[train.signal_to_noise > 1])
 train_labels = np.array(train[train.signal_to_noise > 1][target_cols].values.tolist() ).transpose(( 0, 2, 1))<define_variables>
model = get_model()
normed_train_X = normed_casesdf[normed_casesdf.columns[:-1]]
normed_train_y = normed_casesdf[normed_casesdf.columns[-1]]
model.fit(normed_train_X, normed_train_y, epochs = optimalepochsize)
 for i in range(len(outputDates)) :
normed_X = normed_casesdf[normed_casesdf.columns[i+1:]]
normed_predictions = model.predict(normed_X ).reshape(nplaces)
 for j in range(nplaces):
normed_predictions[j] = max(normed_predictions[j], normed_casesdf[normed_casesdf.columns[-1]][j])
 unnormed_predictions = [invnorm(normed_predictions[j], mins[j], maxes[j])for j in range(nplaces)]
casesdf[outputDates[i]] = unnormed_predictions
normed_casesdf[outputDates[i]] = normed_predictions
REG=0<compute_test_metric>
mins = []
maxes = []
normed_fataldf = fataldf.copy()
for i in range(313):
mini, maxi, normed = norm(fataldf.loc[i,:])
 mins.append(mini)
 maxes.append(maxi)
 for j in range(nInDates):
normed_fataldf[inDates[j]][i] = normed[j]
normed_fataldf
def MCRMSE(y_true, y_pred):
colwise_mse = tf.reduce_mean(tf.square(y_true - y_pred), axis=1)
 return tf.reduce_mean(tf.sqrt(colwise_mse), axis=1 )<choose_model_class>
model2 = get_model()
normed_train_X = normed_fataldf[inDates[:-1]]
normed_train_y = normed_fataldf[inDates[-1]]
model2.fit(normed_train_X, normed_train_y, epochs = optimalepochsize)
 for i in range(len(outputDates)) :
normed_train_X = normed_fataldf[normed_fataldf.columns[i+1:]]
normed_predictions = model.predict(normed_train_X ).reshape(nplaces)
 unnormed_predictions = []
for j in range(nplaces):
normed_predictions[j] = max(normed_predictions[j], normed_fataldf[normed_fataldf.columns[-1]][j])
 unnormed_prediction = invnorm(normed_predictions[j], mins[j], maxes[j])
 if unnormed_prediction == 0:
unnormed_prediction = fataldf[fataldf.columns[-14]][j]*.05
unnormed_predictions.append(unnormed_prediction)
 fataldf[outputDates[i]] = unnormed_predictions
normed_fataldf[outputDates[i]] = normed_predictions
def gru_layer(hidden_dim, dropout):
return tf.keras.layers.Bidirectional(
 tf.keras.layers.GRU(hidden_dim,
dropout=dropout,
return_sequences=True,
kernel_initializer = 'orthogonal'))
def lstm_layer(hidden_dim, dropout):
return tf.keras.layers.Bidirectional(
 tf.keras.layers.LSTM(hidden_dim,
dropout=dropout,
return_sequences=True,
kernel_initializer = 'orthogonal'))
def build_model(gru=1,seq_len=107, pred_len=68, dropout=0.5,
embed_dim=160, hidden_dim=384):
inputs = tf.keras.layers.Input(shape=(seq_len, 3))
embed = tf.keras.layers.Embedding(input_dim=len(token2int), output_dim=embed_dim )(inputs)
 reshaped = tf.reshape(
 embed, shape=(-1, embed.shape[1], embed.shape[2] * embed.shape[3]))
reshaped = tf.keras.layers.SpatialDropout1D (.2 )(reshaped)
 if gru==0:
hidden = gru_layer(hidden_dim, dropout )(reshaped)
 hidden = gru_layer(hidden_dim, dropout )(hidden)
 hidden = gru_layer(hidden_dim, dropout )(hidden)
 elif gru==1:
hidden = gru_layer(hidden_dim, dropout )(reshaped)
 hidden = gru_layer(hidden_dim, dropout )(hidden)
 hidden = lstm_layer(hidden_dim, dropout )(hidden)
 elif gru==2:
hidden = gru_layer(hidden_dim, dropout )(reshaped)
 hidden = lstm_layer(hidden_dim, dropout )(hidden)
 hidden = gru_layer(hidden_dim, dropout )(hidden)
 elif gru==3:
hidden = gru_layer(hidden_dim, dropout )(reshaped)
 hidden = lstm_layer(hidden_dim, dropout )(hidden)
 hidden = lstm_layer(hidden_dim, dropout )(hidden)
 elif gru==4:
hidden = lstm_layer(hidden_dim, dropout )(reshaped)
 hidden = gru_layer(hidden_dim, dropout )(hidden)
 hidden = gru_layer(hidden_dim, dropout )(hidden)
 elif gru==5:
hidden = lstm_layer(hidden_dim, dropout )(reshaped)
 hidden = gru_layer(hidden_dim, dropout )(hidden)
 hidden = lstm_layer(hidden_dim, dropout )(hidden)
 elif gru==6:
hidden = lstm_layer(hidden_dim, dropout )(reshaped)
 hidden = lstm_layer(hidden_dim, dropout )(hidden)
 hidden = gru_layer(hidden_dim, dropout )(hidden)
 elif gru==7:
hidden = lstm_layer(hidden_dim, dropout )(reshaped)
 hidden = lstm_layer(hidden_dim, dropout )(hidden)
 hidden = lstm_layer(hidden_dim, dropout )(hidden)
 truncated = hidden[:, :pred_len]
out = tf.keras.layers.Dense(5, activation='linear' )(truncated)
 model = tf.keras.Model(inputs=inputs, outputs=out)
 adam = tf.optimizers.Adam()
radam = tfa.optimizers.RectifiedAdam()
lookahead = tfa.optimizers.Lookahead(adam, sync_period=6)
 ranger = tfa.optimizers.Lookahead(radam, sync_period=6)
 model.compile(optimizer=adam, loss=MCRMSE)
 return model<choose_model_class>
inDatesTail = inDates[np.where(inDates == beginOutDate)[0][0]:]
submissionDates = [i for i in inDatesTail] + [i for i in outputDates]
es =tf.keras.callbacks.EarlyStopping(
 monitor='val_loss', min_delta=0, patience=20, verbose=1, mode='auto',
baseline=None, restore_best_weights=False)
 lr_callback = tf.keras.callbacks.ReduceLROnPlateau(verbose=0 )<split>
caseSub = casesdf[submissionDates]
fatalSub = fataldf[submissionDates]
caseSub
kf = KFold(n_splits=FOLDS, shuffle=True,random_state=SEED)
 rskf = RepeatedStratifiedKFold(FOLDS, n_repeats = 1, random_state = SEED)
 train_inputs_ = train_inputs
train_labels_ = train_labels
print(train_inputs_.shape,train_labels_.shape)
 for fold,(train_index, val_index)in enumerate(kf.split(train_inputs_,train_labels_)) :
print(f"fold {fold} has train:{len(train_index)}, val:{len(val_index)}")
 train_inputs=train_inputs_[train_index]
train_labels=train_labels_[train_index]
val_inputs=train_inputs_[val_index]
val_labels=train_labels_[val_index]
tf.keras.backend.clear_session()

 gru = build_model(gru=0)
 sv_gru = tf.keras.callbacks.ModelCheckpoint('model_gru.h5', save_best_only=True)
 history_gru = gru.fit(
 train_inputs, train_labels,
validation_data=(val_inputs,val_labels),
batch_size=64,
epochs=epoch,
callbacks=[lr_callback,sv_gru,es],
verbose = VERBOSE 
)
 print(f"Min training loss={min(history_gru.history['loss'])}, min validation loss={min(history_gru.history['val_loss'])}")
 lstm = build_model(gru=7)
 sv_lstm = tf.keras.callbacks.ModelCheckpoint('model_lstm.h5', save_best_only=True)
 history_lstm = lstm.fit(
 train_inputs, train_labels,
validation_data=(val_inputs,val_labels),
batch_size=64,
epochs=epoch,
callbacks=[lr_callback,sv_lstm,es],
verbose = VERBOSE 
)
 print(f"Min training loss={min(history_lstm.history['loss'])}, min validation loss={min(history_lstm.history['val_loss'])}")
 lstm = build_model(gru=1)
 sv_lstm = tf.keras.callbacks.ModelCheckpoint('model_hyb1.h5', save_best_only=True)
 history_lstm = lstm.fit(
 train_inputs, train_labels,
validation_data=(val_inputs,val_labels),
batch_size=64,
epochs=epoch,
callbacks=[lr_callback,sv_lstm,es],
verbose = VERBOSE 
)
 print(f"Min training loss={min(history_lstm.history['loss'])}, min validation loss={min(history_lstm.history['val_loss'])}")
 lstm = build_model(gru=2)
 sv_lstm = tf.keras.callbacks.ModelCheckpoint('model_hyb2.h5', save_best_only=True)
 history_lstm = lstm.fit(
 train_inputs, train_labels,
validation_data=(val_inputs,val_labels),
batch_size=64,
epochs=epoch,
callbacks=[lr_callback,sv_lstm,es],
verbose = VERBOSE 
)
 print(f"Min training loss={min(history_lstm.history['loss'])}, min validation loss={min(history_lstm.history['val_loss'])}")
 lstm = build_model(gru=3)
 sv_lstm = tf.keras.callbacks.ModelCheckpoint('model_hyb3.h5', save_best_only=True)
 history_lstm = lstm.fit(
 train_inputs, train_labels,
validation_data=(val_inputs,val_labels),
batch_size=64,
epochs=epoch,
callbacks=[lr_callback,sv_lstm,es],
verbose = VERBOSE 
)
 print(f"Min training loss={min(history_lstm.history['loss'])}, min validation loss={min(history_lstm.history['val_loss'])}")
 lstm = build_model(gru=4)
 sv_lstm = tf.keras.callbacks.ModelCheckpoint('model_hyb4.h5', save_best_only=True)
 history_lstm = lstm.fit(
 train_inputs, train_labels,
validation_data=(val_inputs,val_labels),
batch_size=64,
epochs=epoch,
callbacks=[lr_callback,sv_lstm,es],
verbose = VERBOSE 
)
 print(f"Min training loss={min(history_lstm.history['loss'])}, min validation loss={min(history_lstm.history['val_loss'])}")
 lstm = build_model(gru=5)
 sv_lstm = tf.keras.callbacks.ModelCheckpoint('model_hyb5.h5', save_best_only=True)
 history_lstm = lstm.fit(
 train_inputs, train_labels,
validation_data=(val_inputs,val_labels),
batch_size=64,
epochs=epoch,
callbacks=[lr_callback,sv_lstm,es],
verbose = VERBOSE 
)
 print(f"Min training loss={min(history_lstm.history['loss'])}, min validation loss={min(history_lstm.history['val_loss'])}")
 lstm = build_model(gru=6)
 sv_lstm = tf.keras.callbacks.ModelCheckpoint('model_hyb6.h5', save_best_only=True)
 history_lstm = lstm.fit(
 train_inputs, train_labels,
validation_data=(val_inputs,val_labels),
batch_size=64,
epochs=epoch,
callbacks=[lr_callback,sv_lstm,es],
verbose = VERBOSE 
)
 print(f"Min training loss={min(history_lstm.history['loss'])}, min validation loss={min(history_lstm.history['val_loss'])}")
 public_df = test.query("seq_length == 107" ).copy()
private_df = test.query("seq_length == 130" ).copy()
public_inputs = preprocess_inputs(public_df)
 private_inputs = preprocess_inputs(private_df)
 gru_short = build_model(gru=0, seq_len=107, pred_len=107)
 gru_long = build_model(gru=0, seq_len=130, pred_len=130)
 lstm_short = build_model(gru=7, seq_len=107, pred_len=107)
 lstm_long = build_model(gru=7, seq_len=130, pred_len=130)
 hyb1_short = build_model(gru=1, seq_len=107, pred_len=107)
 hyb1_long = build_model(gru=1, seq_len=130, pred_len=130)
 hyb2_short = build_model(gru=2, seq_len=107, pred_len=107)
 hyb2_long = build_model(gru=2, seq_len=130, pred_len=130)
 hyb3_short = build_model(gru=3, seq_len=107, pred_len=107)
 hyb3_long = build_model(gru=3, seq_len=130, pred_len=130)
 hyb4_short = build_model(gru=4, seq_len=107, pred_len=107)
 hyb4_long = build_model(gru=4, seq_len=130, pred_len=130)
 hyb5_short = build_model(gru=5, seq_len=107, pred_len=107)
 hyb5_long = build_model(gru=5, seq_len=130, pred_len=130)
 hyb6_short = build_model(gru=6, seq_len=107, pred_len=107)
 hyb6_long = build_model(gru=6, seq_len=130, pred_len=130)
 gru_short.load_weights('model_gru.h5')
 gru_long.load_weights('model_gru.h5')
 lstm_short.load_weights('model_lstm.h5')
 lstm_long.load_weights('model_lstm.h5')
 hyb1_short.load_weights('model_hyb1.h5')
 hyb1_long.load_weights('model_hyb1.h5')
 hyb2_short.load_weights('model_hyb2.h5')
 hyb2_long.load_weights('model_hyb2.h5')
 hyb3_short.load_weights('model_hyb3.h5')
 hyb3_long.load_weights('model_hyb3.h5')
 hyb4_short.load_weights('model_hyb4.h5')
 hyb4_long.load_weights('model_hyb4.h5')
 hyb5_short.load_weights('model_hyb5.h5')
 hyb5_long.load_weights('model_hyb5.h5')
 hyb6_short.load_weights('model_hyb6.h5')
 hyb6_long.load_weights('model_hyb6.h5')
 gru_public_preds = gru_short.predict(public_inputs)
 gru_private_preds = gru_long.predict(private_inputs)
 lstm_public_preds = lstm_short.predict(public_inputs)
 lstm_private_preds = lstm_long.predict(private_inputs)
 hyb1_public_preds = hyb1_short.predict(public_inputs)
 hyb1_private_preds = hyb1_long.predict(private_inputs)
 hyb2_public_preds = hyb2_short.predict(public_inputs)
 hyb2_private_preds = hyb2_long.predict(private_inputs)
 hyb3_public_preds = hyb3_short.predict(public_inputs)
 hyb3_private_preds = hyb3_long.predict(private_inputs)
 hyb4_public_preds = hyb4_short.predict(public_inputs)
 hyb4_private_preds = hyb4_long.predict(private_inputs)
 hyb5_public_preds = hyb5_short.predict(public_inputs)
 hyb5_private_preds = hyb5_long.predict(private_inputs)
 hyb6_public_preds = hyb6_short.predict(public_inputs)
 hyb6_private_preds = hyb6_long.predict(private_inputs)
 preds_gru = []
for df, preds in [(public_df, gru_public_preds),(private_df, gru_private_preds)]:
for i, uid in enumerate(df.id):
single_pred = preds[i]
single_df = pd.DataFrame(single_pred, columns=target_cols)
 single_df['id_seqpos'] = [f'{uid}_{x}' for x in range(single_df.shape[0])]
preds_gru.append(single_df)
 preds_gru_df = pd.concat(preds_gru)
 preds_lstm = []
for df, preds in [(public_df, lstm_public_preds),(private_df, lstm_private_preds)]:
for i, uid in enumerate(df.id):
single_pred = preds[i]
single_df = pd.DataFrame(single_pred, columns=target_cols)
 single_df['id_seqpos'] = [f'{uid}_{x}' for x in range(single_df.shape[0])]
preds_lstm.append(single_df)
 preds_lstm_df = pd.concat(preds_lstm)
 preds_hyb1 = []
for df, preds in [(public_df, hyb1_public_preds),(private_df, hyb1_private_preds)]:
for i, uid in enumerate(df.id):
single_pred = preds[i]
single_df = pd.DataFrame(single_pred, columns=target_cols)
 single_df['id_seqpos'] = [f'{uid}_{x}' for x in range(single_df.shape[0])]
preds_hyb1.append(single_df)
 preds_hyb1_df = pd.concat(preds_hyb1)
 preds_hyb2 = []
for df, preds in [(public_df, hyb2_public_preds),(private_df, hyb2_private_preds)]:
for i, uid in enumerate(df.id):
single_pred = preds[i]
single_df = pd.DataFrame(single_pred, columns=target_cols)
 single_df['id_seqpos'] = [f'{uid}_{x}' for x in range(single_df.shape[0])]
preds_hyb2.append(single_df)
 preds_hyb2_df = pd.concat(preds_hyb2)
 preds_hyb3 = []
for df, preds in [(public_df, hyb3_public_preds),(private_df, hyb3_private_preds)]:
for i, uid in enumerate(df.id):
single_pred = preds[i]
single_df = pd.DataFrame(single_pred, columns=target_cols)
 single_df['id_seqpos'] = [f'{uid}_{x}' for x in range(single_df.shape[0])]
preds_hyb3.append(single_df)
 preds_hyb3_df = pd.concat(preds_hyb3)
 preds_hyb4 = []
for df, preds in [(public_df, hyb4_public_preds),(private_df, hyb4_private_preds)]:
for i, uid in enumerate(df.id):
single_pred = preds[i]
single_df = pd.DataFrame(single_pred, columns=target_cols)
 single_df['id_seqpos'] = [f'{uid}_{x}' for x in range(single_df.shape[0])]
preds_hyb4.append(single_df)
 preds_hyb4_df = pd.concat(preds_hyb4)
 preds_hyb5 = []
for df, preds in [(public_df, hyb5_public_preds),(private_df, hyb5_private_preds)]:
for i, uid in enumerate(df.id):
single_pred = preds[i]
single_df = pd.DataFrame(single_pred, columns=target_cols)
 single_df['id_seqpos'] = [f'{uid}_{x}' for x in range(single_df.shape[0])]
preds_hyb5.append(single_df)
 preds_hyb5_df = pd.concat(preds_hyb5)
 preds_hyb6 = []
for df, preds in [(public_df, hyb6_public_preds),(private_df, hyb6_private_preds)]:
for i, uid in enumerate(df.id):
single_pred = preds[i]
single_df = pd.DataFrame(single_pred, columns=target_cols)
 single_df['id_seqpos'] = [f'{uid}_{x}' for x in range(single_df.shape[0])]
preds_hyb6.append(single_df)
 preds_hyb6_df = pd.concat(preds_hyb6)
 blend_preds_df = pd.DataFrame()
blend_preds_df['id_seqpos'] = preds_gru_df['id_seqpos']
blend_preds_df['reactivity'] = 0.2*preds_gru_df['reactivity'] + 0.2*preds_lstm_df['reactivity'] + 0.2*preds_hyb1_df['reactivity'] + 0.2*preds_hyb2_df['reactivity'] + 0.2*preds_hyb3_df['reactivity']+ 0.2*preds_hyb4_df['reactivity'] + 0.2*preds_hyb5_df['reactivity'] + 0.2*preds_hyb6_df['reactivity']
blend_preds_df['deg_Mg_pH10'] = 0.2*preds_gru_df['deg_Mg_pH10'] + 0.2*preds_lstm_df['deg_Mg_pH10'] + 0.2*preds_hyb1_df['deg_Mg_pH10'] + 0.2*preds_hyb2_df['deg_Mg_pH10'] + 0.2*preds_hyb3_df['deg_Mg_pH10']+ 0.2*preds_hyb4_df['deg_Mg_pH10'] + 0.2*preds_hyb5_df['deg_Mg_pH10'] + 0.2*preds_hyb6_df['deg_Mg_pH10']
blend_preds_df['deg_pH10'] = 0.2*preds_gru_df['deg_pH10'] + 0.2*preds_lstm_df['deg_pH10'] + 0.2*preds_hyb1_df['deg_pH10'] + 0.2*preds_hyb2_df['deg_pH10'] + 0.2*preds_hyb3_df['deg_pH10']+ 0.2*preds_hyb4_df['deg_pH10'] + 0.2*preds_hyb5_df['deg_pH10'] + 0.2*preds_hyb6_df['deg_pH10']
blend_preds_df['deg_Mg_50C'] = 0.2*preds_gru_df['deg_Mg_50C'] + 0.2*preds_lstm_df['deg_Mg_50C'] + 0.2*preds_hyb1_df['deg_Mg_50C'] + 0.2*preds_hyb2_df['deg_Mg_50C'] + 0.2*preds_hyb3_df['deg_Mg_50C']+ 0.2*preds_hyb4_df['deg_Mg_50C'] + 0.2*preds_hyb5_df['deg_Mg_50C'] + 0.2*preds_hyb6_df['deg_Mg_50C']
blend_preds_df['deg_50C'] = 0.2*preds_gru_df['deg_50C'] + 0.2*preds_lstm_df['deg_50C'] + 0.2*preds_hyb1_df['deg_50C'] + 0.2*preds_hyb2_df['deg_Mg_50C'] + 0.2*preds_hyb3_df['deg_Mg_50C']+ 0.2*preds_hyb4_df['deg_50C'] + 0.2*preds_hyb5_df['deg_Mg_50C'] + 0.2*preds_hyb6_df['deg_Mg_50C']
blend_preds_df['reactivity'] /=1.6
blend_preds_df['deg_Mg_pH10'] /=1.6
blend_preds_df['deg_pH10'] /=1.6
blend_preds_df['deg_Mg_50C']/=1.6
blend_preds_df['deg_50C'] /=1.6
sample_sub = pd.read_csv('/kaggle/input/stanford-covid-vaccine/sample_submission.csv')
 submission = sample_sub[['id_seqpos']].merge(blend_preds_df, on=['id_seqpos'])
 submission.head()
submission.to_csv(f"submission_{fold}.csv", index=False)
<save_to_csv>
submissiondf = pd.read_csv(filepath['submission'] )
files = []
for i in range(FOLDS):
csv = pd.read_csv(f'./submission_{i}.csv')
 files.append(csv)
 total = pd.concat(files, axis=1)
 total.head()
col = list(submission.columns)[1:]
print(col)
 submission1 = submission
for c in col:
submission1[c] = total[c].mean(axis=1)
 submission1.to_csv('submission.csv', index=False)
 submission1.head() 
<set_options>
for place in range(nplaces):
for date in range(len(submissionDates)) :
submissiondf['ConfirmedCases'][place*len(submissionDates)+ date] = caseSub[submissionDates[date]][place]
submissiondf['Fatalities'][place*len(submissionDates)+ date] = fatalSub[submissionDates[date]][place]
submissiondf.set_index(['ForecastId'])
 submissiondf
warnings.filterwarnings('ignore')
<define_variables>
submissiondf.to_csv('submission.csv', index=False )