Kaggle competition: COVID19 Global Forecasting (Week 2)
<SOS> metric: MCRMSLE Kaggle data source: covid19-global-forecasting-(week-2)<categorify>
Image(".. /input/covid19-forecast-week-2/COVID-19 forecast - week 2.png" )
<create_dataframe><EOS> 
data = pd.read_csv('.. /input/covid19-global-forecasting-week-2/train.csv')
 data['ConfirmedCases'] = data['ConfirmedCases'].apply(lambda x: np.log(x + 1))
data['Fatalities'] = data['Fatalities'].apply(lambda x: np.log(x + 1))
data["Country & Subdivision"] = [(data['Country_Region'][i], \
data['Province_State'][i])for i in range(len(data)) ]
unique_areas = data['Country & Subdivision'].unique()
unique_areas_dict = {j : i for i, j in enumerate(unique_areas)}
NUM_DATES = 100; TRAIN_SIZE = 57;
train = data[data['Id']%NUM_DATES <= TRAIN_SIZE]
encoded_train = np.array(list(map(lambda x: unique_areas_dict[x],
train['Country & Subdivision'].to_numpy())))\ 
.reshape(( -1, 1))
class MyModel(tf.keras.Model):
def __init__(self,):
super(MyModel, self ).__init__()
self.embedding_size = 10
self.LSTM_size = 30
self.w1 = tf.keras.layers.Embedding(len(unique_areas), self.embedding_size,)
 self.w2 = tf.keras.layers.LSTM(self.LSTM_size,
return_sequences=True,
stateful=False,)
 self.w3 = tf.keras.layers.LSTM(forecast_length,
return_sequences=True,
stateful=False, activation='relu')
 def call(self, inputs):
location, curr_num = tf.split(inputs, [-1, 1], 2)
 x = self.w1(location)
 x = tf.reshape(x, [-1,inputs.shape[1],self.embedding_size])
 x = tf.concat([x, curr_num], axis=2)
 x = self.w2(x,)
 x = self.w3(x,)
 return x
forecast_length = 13
X = np.concatenate(( encoded_train,
train['ConfirmedCases'].to_numpy().reshape(( -1, 1))),
axis=1)
 X = X.reshape(( len(unique_areas), -1, 2))
total_length = X.shape[1]
Y = X[:, :, -1]
Y = [ [y[i:i+forecast_length] for i in range(1, total_length-forecast_length+1)]
for y in Y ]
Y = np.array(Y)
 X = X[:, :-forecast_length, :]
model_1 = MyModel()
model_1.compile(optimizer='adam', loss='mae',)
 while 'history_1' not in vars() or history_1.history['loss'][-1] > 0.1:
history_1 = model_1.fit(X, Y, epochs=10000, batch_size=len(unique_areas),)
 model_1.save_weights('model_1_weights.h5')
 encoded_test = np.array(list(map(lambda x: unique_areas_dict[x],
data['Country & Subdivision'].to_numpy())))\ 
.reshape(( -1, 1))
test_X = np.concatenate(( encoded_test,
data['ConfirmedCases'].to_numpy().reshape(( -1, 1))),
axis=1)
 test_X = test_X.reshape(( len(unique_areas), -1, 2))
test_X = test_X[:, :TRAIN_SIZE, :]
subm = pd.read_csv('.. /input/covid19-global-forecasting-week-2/submission.csv')
 results = np.zeros(( X.shape[0], 0))
for i in range(4):
model_1.reset_states()
test_Y = model_1(test_X ).numpy() [:, -1, :]
results = np.concatenate(( results, np.exp(test_Y)- 1), axis=1)
 test_Y = test_Y.reshape(( -1, forecast_length, 1))
test_Y = np.concatenate(
([[[i]] * forecast_length for i in range(len(test_Y)) ], test_Y,), axis = 2)
 test_X = np.concatenate(( test_X, test_Y), axis=1)
 subm['ConfirmedCases'] = results[:, :NUM_DATES-TRAIN_SIZE].reshape(-1)
 X = np.concatenate(( encoded_train,
train['Fatalities'].to_numpy().reshape(( -1, 1))),
axis=1)
 X = X.reshape(( len(unique_areas), -1, 2))
total_length = X.shape[1]
Y = X[:, :, -1]
Y = [ [y[i:i+forecast_length] for i in range(1, total_length-forecast_length+1)]
for y in Y ]
Y = np.array(Y)
 X = X[:, :-forecast_length, :]
model_2 = MyModel()
model_2.compile(optimizer='adam', loss='mse',)
 while 'history_2' not in vars() or history_2.history['loss'][-1] > 0.01:
history_2 = model_2.fit(X, Y, epochs=10000, batch_size=len(unique_areas),)
 model_2.save_weights('model_2_weights.h5')
 encoded_test = np.array(list(map(lambda x: unique_areas_dict[x],
data['Country & Subdivision'].to_numpy())))\ 
.reshape(( -1, 1))
test_X = np.concatenate(( encoded_test,
data['Fatalities'].to_numpy().reshape(( -1, 1))),
axis=1)
 test_X = test_X.reshape(( len(unique_areas), -1, 2))
test_X = test_X[:, :TRAIN_SIZE, :]
results = np.zeros(( X.shape[0], 0))
for i in range(4):
model_2.reset_states()
test_Y = model_2(test_X ).numpy() [:, -1, :]
print(np.exp(test_Y[2])-1)
 results = np.concatenate(( results, np.exp(test_Y)- 1), axis=1)
 test_Y = test_Y.reshape(( -1, forecast_length, 1))
test_Y = np.concatenate(
([[[i]] * forecast_length for i in range(len(test_Y)) ], test_Y,), axis = 2)
 test_X = np.concatenate(( test_X, test_Y), axis=1)
 subm['Fatalities'] = results[:, :NUM_DATES-TRAIN_SIZE].reshape(-1)
 subm.to_csv('submission.csv', index=False )