Kaggle competition: COVID19 Global Forecasting (Week 1)
<SOS> metric: MCRMSLE Kaggle data source: covid19-global-forecasting-(week-1)<load_from_csv>
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename))
train=pd.read_csv('/kaggle/input/covid19-global-forecasting-week-1/train.csv',sep=',')
 test=pd.read_csv('/kaggle/input/covid19-global-forecasting-week-1/test.csv',sep=',')
 submission=pd.read_csv('/kaggle/input/covid19-global-forecasting-week-1/submission.csv',sep=',')
 print(train.head(2))
print(test.head(2))
sample_submission = pd.read_csv('/kaggle/input/jigsaw-toxic-comment-classification-challenge/sample_submission.csv')
 test_labels = pd.read_csv('//kaggle/input/jigsaw-toxic-comment-classification-challenge/test_labels.csv')
 train = pd.read_csv('//kaggle/input/jigsaw-toxic-comment-classification-challenge/train.csv')
 test = pd.read_csv('//kaggle/input/jigsaw-toxic-comment-classification-challenge/test.csv' )<split>
print("Training data:
", train.count() , "
Test data:
", test.count())
 print("
Training Missing data:
", train.isnull().sum() , "
Test Missing data:
", test.isnull().sum() )
Y = train[['toxic','severe_toxic','obscene','threat','insult','identity_hate']].astype(float)
 X = train['comment_text']
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.33, random_state=42 )<split>
train = train.drop(['Province/State'],axis=1)
 test = test.drop(['Province/State'],axis=1)
 train.dtypes
X_train = X_train.values
X_test = X_test.values
y_train = y_train.values
y_test = y_test.values<compute_test_metric>
def create_time_features(df):

 df['date'] = df.index
df['dayofweek'] = df['date'].dt.dayofweek
df['quarter'] = df['date'].dt.quarter
df['month'] = df['date'].dt.month
df['year'] = df['date'].dt.year
df['dayofyear'] = df['date'].dt.dayofyear
df['dayofmonth'] = df['date'].dt.day
df['weekofyear'] = df['date'].dt.weekofyear
X = df[['dayofweek','quarter','month','year',
'dayofyear','dayofmonth','weekofyear']]
return X
def accuracy_thresh(y_pred, y_true, thresh:float=0.4, sigmoid:bool=True):
"Compute accuracy when `y_pred` and `y_true` are the same size."
if sigmoid: y_pred = y_pred.sigmoid()
return np.mean(((y_pred>thresh ).float() ==y_true.float() ).float().cpu().numpy() , axis=1 ).sum() 
<choose_model_class>
train["iDate"] = train["Date"].apply(lambda x: x.replace("-",""))
train["iDate"] = train["iDate"].astype(int)
 test["iDate"] = test["Date"].apply(lambda x: x.replace("-",""))
test["iDate"] = test["iDate"].astype(int)
 train['Date'] = pd.to_datetime(train['Date'])
 test['Date']= pd.to_datetime(test['Date'])
 train = train.set_index(['Date'])
 test = test.set_index(['Date'] )
config = DistilBertConfig(vocab_size_or_config_json_file=32000, hidden_size=768,dropout=0.1,num_labels=6,
num_hidden_layers=12, num_attention_heads=12, intermediate_size=3072 )<choose_model_class>
create_time_features(train)
 create_time_features(test )
class DistilBertForSequenceClassification(nn.Module):
def __init__(self, config):
super().__init__()
self.num_labels = config.num_labels
self.distilbert = DistilBertModel.from_pretrained('distilbert-base-uncased')
 self.pre_classifier = nn.Linear(config.hidden_size, config.hidden_size)
 self.classifier = nn.Linear(config.hidden_size, config.num_labels)
 self.dropout = nn.Dropout(config.seq_classif_dropout)
 nn.init.xavier_normal_(self.classifier.weight)
 def forward(self, input_ids=None, attention_mask=None, head_mask=None, labels=None):
distilbert_output = self.distilbert(input_ids=input_ids,
attention_mask=attention_mask,
head_mask=head_mask)
 hidden_state = distilbert_output[0]
pooled_output = hidden_state[:, 0]
pooled_output = self.pre_classifier(pooled_output)
 pooled_output = nn.ReLU()(pooled_output)
 pooled_output = self.dropout(pooled_output)
 logits = self.classifier(pooled_output)
 return logits<string_transform>
train['ConfirmedCases_today'] = train.groupby(['Country/Region'])['ConfirmedCases'].diff(1)
 train['Fatalities_today'] = train.groupby(['Country/Region'])['Fatalities'].diff(1 )
max_seq_length = 256
tokenizer = DistilBertTokenizer.from_pretrained('distilbert-base-uncased')
 class text_dataset(Dataset):
def __init__(self,x,y, transform=None):
self.x = x
self.y = y
self.transform = transform
def __getitem__(self,index):
tokenized_comment = tokenizer.tokenize(self.x[index])
 if len(tokenized_comment)> max_seq_length:
tokenized_comment = tokenized_comment[:max_seq_length]
ids_review = tokenizer.convert_tokens_to_ids(tokenized_comment)
 padding = [0] *(max_seq_length - len(ids_review))
ids_review += padding
assert len(ids_review)== max_seq_length
ids_review = torch.tensor(ids_review)
 hcc = self.y[index]
list_of_labels = [torch.from_numpy(hcc)]
return ids_review, list_of_labels[0]
def __len__(self):
return len(self.x)
<compute_test_metric>
train['ConfirmedCases_today'] = train['ConfirmedCases_today'].fillna(0)
 train['Fatalities_today'] = train['Fatalities_today'].fillna(0)
 print(train.groupby(['Country/Region'])['ConfirmedCases_today'].sum())
 print(train.groupby(['Country/Region'])['Fatalities_today'].sum())
text_dataset(X_train, y_train ).__getitem__(6)[1]<create_dataframe>
train.isnull().sum()
batch_size = 32
training_dataset = text_dataset(X_train,y_train)
 test_dataset = text_dataset(X_test,y_test)
 dataloaders_dict = {'train': torch.utils.data.DataLoader(training_dataset, batch_size=batch_size, shuffle=False),
'val':torch.utils.data.DataLoader(test_dataset, batch_size=batch_size, shuffle=False)
 }
dataset_sizes = {'train':len(X_train),
'val':len(X_test)}
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 model = DistilBertForSequenceClassification(config)
 model.to(device)
 print(device)
<train_model>
train.isnull().sum()
def train_model(model, criterion, optimizer, scheduler, num_epochs=2):
model.train()
since = time.time()
print('starting')
 best_model_wts = copy.deepcopy(model.state_dict())
 best_loss = 100
for epoch in range(num_epochs):
print('Epoch {}/{}'.format(epoch+1, num_epochs))
print('-' * 10)
 for phase in ['train', 'val']:
if phase == 'train':
scheduler.step()
model.train()
else:
model.eval()
running_loss = 0.0
beta_score_accuracy = 0.0
micro_roc_auc_acc = 0.0
for inputs, hcc in dataloaders_dict[phase]:
inputs = inputs.to(device)
 hcc = hcc.to(device)
 optimizer.zero_grad()
with torch.set_grad_enabled(phase == 'train'):
outputs = model(inputs)
 loss = criterion(outputs,hcc.float())
 if phase == 'train':
loss.backward()
optimizer.step()
running_loss += loss.item() * inputs.size(0)
 micro_roc_auc_acc += accuracy_thresh(outputs.view(-1,6),hcc.view(-1,6))
epoch_loss = running_loss / dataset_sizes[phase]
epoch_micro_roc_acc = micro_roc_auc_acc / dataset_sizes[phase]
print('{} total loss: {:.4f} '.format(phase,epoch_loss))
print('{} micro_roc_auc_acc: {:.4f}'.format(phase, epoch_micro_roc_acc))
if phase == 'val' and epoch_loss < best_loss:
print('saving with loss of {}'.format(epoch_loss),
'improved over previous {}'.format(best_loss))
best_loss = epoch_loss
best_model_wts = copy.deepcopy(model.state_dict())
 torch.save(model.state_dict() , 'distilbert_model_weights.pth')
 print()
time_elapsed = time.time() - since
print('Training complete in {:.0f}m {:.0f}s'.format(
 time_elapsed // 60, time_elapsed % 60))
print('Best val Acc: {:4f}'.format(float(best_loss)))
 model.load_state_dict(best_model_wts)
 return model
print('done' )<choose_model_class>
input_cols = ["Lat","Long", "iDate"]
output_cols = ["ConfirmedCases","Fatalities"]
ids = submission["ForecastId"]
extra_feat = ["ConfirmedCases_today", "Fatalities_today"]
lrlast =.001
lrmain = 3e-5
optim1 = torch.optim.Adam(model.parameters() ,lrmain)
 optimizer_ft = optim1
criterion = nn.BCEWithLogitsLoss()
exp_lr_scheduler = lr_scheduler.StepLR(optimizer_ft, step_size=3, gamma=0.1 )<train_model>
X = train[input_cols]
Y1 = train[output_cols[0]]
Y2 = train[output_cols[1]]
X_test = test[input_cols]
E1 = train[extra_feat[0]]
E2 = train[extra_feat[1]]
XE = train[input_cols+extra_feat]
model_ft1 = train_model(model, criterion, optimizer_ft, exp_lr_scheduler,num_epochs=8 )<categorify>
model= xgb.XGBRegressor(n_estimators=1000)
 model.fit(X,E1)
 ep1 = model.predict(X_test)
 model.fit(X,E2)
 ep2 = model.predict(X_test)
x_test = test['comment_text']
y_test = np.zeros(x_test.shape[0]*6 ).reshape(x_test.shape[0],6)
 test_dataset = text_dataset(x_test,y_test)
 prediction_dataloader = torch.utils.data.DataLoader(test_dataset, batch_size=1, shuffle=False)
 def preds(model,test_loader):
predictions = []
for inputs, sentiment in test_loader:
inputs = inputs.to(device)
 sentiment = sentiment.to(device)
 with torch.no_grad() :
outputs = model(inputs)
 outputs = torch.sigmoid(outputs)
 predictions.append(outputs.cpu().detach().numpy().tolist())
 return predictions<prepare_output>
model= xgb.XGBRegressor(n_estimators=1000)
 model.fit(X,E1)
 ep1 = model.predict(X_test)
 preds = np.array(ep1)
 preds[preds < 0] = 0
preds = np.round(preds, 0)
 model.fit(X,E2)
 ep2 = model.predict(X_test)
 prds = np.array(ep2)
 prds[prds < 0] = 0
prds = np.round(prds, 0 )
predictions = preds(model=model_ft1,test_loader=prediction_dataloader)
 predictions = np.array(predictions)[:,0]<prepare_output>
test['ConfirmedCases_today'] = preds
test['Fatalities_today'] = prds
submission = pd.DataFrame(predictions,columns=['toxic','severe_toxic','obscene','threat','insult','identity_hate'])
 test[['toxic','severe_toxic','obscene','threat','insult','identity_hate']]=submission
final_sub = test[['id','toxic','severe_toxic','obscene','threat','insult','identity_hate']]
final_sub.head()<save_to_csv>
XE_test = test[input_cols+extra_feat]
final_sub.to_csv('submissions.csv',index=False)
 final_sub.head()<import_modules>
tree_reg= xgb.XGBRegressor(n_estimators=1000)
 tree_reg.fit(XE,Y1)
 prd = tree_reg.predict(XE_test)
 tree_reg.fit(XE,Y2)
 prd_2 = tree_reg.predict(XE_test)
 output_tree_reg = pd.DataFrame({ 'ForecastId' : ids, 'ConfirmedCases': prd,'Fatalities':prd_2 })
 output_tree_reg.to_csv('submission.csv', index=False)