Kaggle competition: COVID19 Global Forecasting (Week 1)
<SOS> metric: MCRMSLE Kaggle data source: covid19-global-forecasting-(week-1)<choose_model_class>
!pip install pyramid-arima
class BertClassifier(tf.keras.Model):
def __init__(self, bert: TFBertModel, num_classes: int):
super().__init__()
self.bert = bert
self.classifier = Dense(num_classes, activation='sigmoid')
 @tf.function
def call(self, input_ids, attention_mask=None, token_type_ids=None, position_ids=None, head_mask=None):
outputs = self.bert(input_ids,
attention_mask=attention_mask,
token_type_ids=token_type_ids,
position_ids=position_ids,
head_mask=head_mask)
 cls_output = outputs[1]
cls_output = self.classifier(cls_output)
 return cls_output
model = BertClassifier(TFBertModel.from_pretrained(bert_model_name), len(label_cols))<choose_model_class>
warnings.filterwarnings("ignore")
 test = pd.read_csv(".. /input/covid19-global-forecasting-week-1/test.csv")
 train = pd.read_csv(".. /input/covid19-global-forecasting-week-1/train.csv")
 train.head()
steps_per_epoch = train_size // BATCH_SIZE
validation_steps = validation_size // BATCH_SIZE
loss_object = tf.keras.losses.BinaryCrossentropy(from_logits=False)
 train_loss = tf.keras.metrics.Mean(name='train_loss')
 validation_loss = tf.keras.metrics.Mean(name='test_loss')
 warmup_steps = steps_per_epoch // 3
total_steps = steps_per_epoch * NR_EPOCHS - warmup_steps
optimizer = create_optimizer(init_lr=2e-5, num_train_steps=total_steps, num_warmup_steps=warmup_steps)
 train_auc_metrics = [tf.keras.metrics.AUC() for i in range(len(label_cols)) ]
validation_auc_metrics = [tf.keras.metrics.AUC() for i in range(len(label_cols)) ]
@tf.function
def train_step(model, token_ids, masks, labels):
labels = tf.dtypes.cast(labels, tf.float32)
 with tf.GradientTape() as tape:
predictions = model(token_ids, attention_mask=masks)
 loss = loss_object(labels, predictions)
 gradients = tape.gradient(loss, model.trainable_variables)
 optimizer.apply_gradients(zip(gradients, model.trainable_variables), 1.0)
 train_loss(loss)
 for i, auc in enumerate(train_auc_metrics):
auc.update_state(labels[:,i], predictions[:,i])
 @tf.function
def validation_step(model, token_ids, masks, labels):
labels = tf.dtypes.cast(labels, tf.float32)
 predictions = model(token_ids, attention_mask=masks, training=False)
 v_loss = loss_object(labels, predictions)
 validation_loss(v_loss)
 for i, auc in enumerate(validation_auc_metrics):
auc.update_state(labels[:,i], predictions[:,i])
 def train(model, train_dataset, val_dataset, train_steps_per_epoch, val_steps_per_epoch, epochs):
for epoch in range(epochs):
print('=' * 50, f"EPOCH {epoch}", '=' * 50)
 start = time.time()
for i,(token_ids, masks, labels)in enumerate(tqdm(train_dataset, total=train_steps_per_epoch)) :
train_step(model, token_ids, masks, labels)
 if i % 1000 == 0:
print(f'
Train Step: {i}, Loss: {train_loss.result() }')
 for i, label_name in enumerate(label_cols):
print(f"{label_name} roc_auc {train_auc_metrics[i].result() }")
 train_auc_metrics[i].reset_states()
for i,(token_ids, masks, labels)in enumerate(tqdm(val_dataset, total=val_steps_per_epoch)) :
validation_step(model, token_ids, masks, labels)
 print(f'
Epoch {epoch+1}, Validation Loss: {validation_loss.result() }, Time: {time.time() -start}
')
 for i, label_name in enumerate(label_cols):
print(f"{label_name} roc_auc {validation_auc_metrics[i].result() }")
 validation_auc_metrics[i].reset_states()
print('
')
 train(model, train_dataset, validation_dataset, train_steps_per_epoch=steps_per_epoch, val_steps_per_epoch=validation_steps, epochs=NR_EPOCHS )<string_transform>
no_countr = train['Country/Region'].nunique()
no_province = train['Province/State'].nunique()
no_countr_with_prov = len(train[train['Province/State'].isna() ==False]['Country/Region'].unique())
 total_forecasting_number = no_province + no_countr - no_countr_with_prov+2
no_days = train['Date'].nunique()
print('there are ', no_countr, 'unique Countries/Regionions, each with ', no_days, 'days of data, all of them having the same dates.There are also ',no_province, 'Provinces/States which can be found on ', no_countr_with_prov, 'countries/ regions.' )
test_input_ids = tokenize_sentences(df_test['comment_text'], tokenizer, MAX_LEN)
 test_input_ids = pad_sequences(test_input_ids, maxlen=MAX_LEN, dtype="long", value=0, truncating="post", padding="post")
 test_attention_masks = create_attention_masks(test_input_ids )<load_from_csv>
df = confirmed_total_date.copy()
df = pd.DataFrame({'date': [df.index[i] for i in range(len(df)) ] , 'cases': df['ConfirmedCases'].values.reshape(1,-1)[0].tolist() })
 dfog = df.copy()
def l_regr(x,y):
model = LinearRegression().fit(x, y)
 return model
x = df['cases']
x = x.drop(x.index[-1] ).values.reshape(( -1, 1))
y = df['cases']
y = y.drop(y.index[0])
 ex_slope = l_regr(x,y ).coef_
d = 0
for i in range(1,5):
plt.plot(df['cases'])
 plt.show()
plt.close()
df['prev_cases'] = df['cases'].shift(1)
 df['cases'] =(df['cases'] - df['prev_cases'])
 df = df.drop(['prev_cases'],axis=1)
 df = df.drop(df.index[0])
 x = df['cases']
x = x.drop(y.index[-1] ).values.reshape(( -1, 1))
y = df['cases']
y = y.drop(y.index[0])
 model = l_regr(x,y)
 if(abs(model.coef_)> ex_slope):
print('this is it! ', ex_slope)
 break
d += 1
ex_slope = model.coef_
print(model.coef_ )
TEST_BATCH_SIZE = 32
test_steps = len(df_test)// TEST_BATCH_SIZE
test_dataset = create_dataset(( test_input_ids, test_attention_masks), batch_size=TEST_BATCH_SIZE, train=False, epochs=1)
 df_submission = pd.read_csv(subm_path, index_col='id')
 for i,(token_ids, masks)in enumerate(tqdm(test_dataset, total=test_steps)) :
sample_ids = df_test.iloc[i*TEST_BATCH_SIZE:(i+1)*TEST_BATCH_SIZE]['id']
predictions = model(token_ids, attention_mask=masks ).numpy()
df_submission.loc[sample_ids, label_cols] = predictions<save_to_csv>
index = 1
cases_pred= []
fatalities_pred = []
pbar = tqdm(total=total_forecasting_number)
 while index < total_forecasting_number+1:
x = train['ConfirmedCases'].iloc[[i for i in range(no_days*(index-1),no_days*index)]].values
z = train['Fatalities'].iloc[[i for i in range(no_days*(index-1),no_days*index)]].values
index += 1
no_nul_cases = pd.DataFrame(x)
 no_nul_cases = no_nul_cases[no_nul_cases.values != 0]
if(not no_nul_cases.empty):
X = [xi for xi in no_nul_cases.values]
try:
model = pyramid.arima.auto_arima(X,seasonal=True, m=12)
 pred = model.predict(31)
 pred = pred.astype(int)
 pred = pred.tolist()
except:
model = l_regr(np.array([i for i in range(len(X)) ] ).reshape(-1, 1),X)
 pred = [(model.coef_*(len(X)+i)+ model.intercept_ ).astype('int')[0][0] for i in range(1,32)]
else:
pred = [0] * 31
pred = x[-12:].astype(int ).tolist() + pred
cases_pred+=pred
no_nul_fatalities = pd.DataFrame(z)
 no_nul_fatalities = no_nul_fatalities[no_nul_fatalities.values != 0]
if(not no_nul_fatalities.empty):
Z = [zi for zi in no_nul_fatalities.values]
try:
model = pyramid.arima.auto_arima(Z, seasonal=False, m=12)
 pred = model.predict(31)
 pred = pred.astype(int)
 pred = pred.tolist()
except:
model = l_regr(np.array([i for i in range(len(Z)) ] ).reshape(-1, 1),Z)
 pred = [(model.coef_*(len(Z)+i)+ model.intercept_ ).astype('int')[0][0] for i in range(1,32)]
else:
pred = [0] * 31
pred = z[-12:].astype(int ).tolist() + pred
fatalities_pred+=pred
pbar.update(1)
 pbar.close()
<set_options><EOS> 
submission = pd.DataFrame({'ForecastId': [i for i in range(1,len(cases_pred)+1)] ,'ConfirmedCases': cases_pred, 'Fatalities': fatalities_pred})
 filename = 'submission.csv'
submission.to_csv(filename,index=False )