Kaggle competition: Titanic - Machine Learning from Disaster
no_decay = ['bias', 'LayerNorm.weight']
optimizer_grouped_parameters = [
{'params': [p for n, p in model.named_parameters() if not any(nd in n for nd in no_decay)], 'weight_decay': 0.01},
{'params': [p for n, p in model.named_parameters() if any(nd in n for nd in no_decay)], 'weight_decay': 0.0}
]
EPOCH_NUM = 2
warmup_steps = 10 ** 3
total_steps = len(train_iterator)* EPOCH_NUM - warmup_steps
optimizer = AdamW(optimizer_grouped_parameters, lr=2e-5, eps=1e-8)
 scheduler = get_linear_schedule_with_warmup(optimizer, warmup_steps, total_steps)
<train_model>
train = pd.read_csv("/kaggle/input/titanic/train.csv")
 train.head()
for i in range(EPOCH_NUM):
print('=' * 50, f"EPOCH {i}", '=' * 50)
 train(model, train_iterator, optimizer, scheduler)
 evaluate(model, dev_iterator )<load_from_csv>
test = pd.read_csv("/kaggle/input/titanic/test.csv")
 test.head()
model.eval()
test_df = pd.read_csv(os.path.join(path, 'test.csv'))
submission = pd.read_csv(os.path.join(path, 'sample_submission.csv'))
columns = ['toxic', 'severe_toxic', 'obscene', 'threat', 'insult', 'identity_hate']
for i in tqdm(range(len(test_df)// BATCH_SIZE + 1)) :
batch_df = test_df.iloc[i * BATCH_SIZE:(i + 1)* BATCH_SIZE]
assert(batch_df["id"] == submission["id"][i * BATCH_SIZE:(i + 1)* BATCH_SIZE] ).all() , f"Id mismatch"
texts = []
for text in batch_df["comment_text"].tolist() :
text = tokenizer.encode(text, add_special_tokens=True)
 if len(text)> 120:
text = text[:119] + [tokenizer.sep_token_id]
texts.append(torch.LongTensor(text))
x = pad_sequence(texts, batch_first=True, padding_value=tokenizer.pad_token_id ).to(device)
 mask =(x != tokenizer.pad_token_id ).float().to(device)
 with torch.no_grad() :
_, outputs = model(x, attention_mask=mask)
 outputs = outputs.cpu().numpy()
submission.iloc[i * BATCH_SIZE:(i + 1)* BATCH_SIZE][columns] = outputs<save_to_csv>
print('Train columns with null values:',train.isnull().sum() , sep = '
')
 print("-"*42)
 print('Test/Validation columns with null values:', test.isnull().sum() ,sep = '
')
 print("-"*42 )
submission.to_csv("submission.csv", index=False )<save_to_csv>
test[test['Fare'].isnull() ]
test['Fare'] = test['Fare'].fillna(test['Fare'].mean() )
submission.to_csv("submission.csv", index=False )<define_variables>
print('Number of missing values in Fare column for test set:', test.Fare.isnull().sum() )
f_caps_gru = '.. /input/capsule-net-with-gru/submission.csv'
f_dual_embed_pl = '.. /input/submission-dual-embed-pl/submission_dual_embed(2 ).csv'
f_dual_embed_mish = '.. /input/bi-gru-lstm-dual-embedding-with-mish/submission.csv'
f_lstm_glove_tta = '.. /input/improved-lstm-baseline-glove-dropout-trainta/submission.csv'
f_dual_embed_dehyp = '.. /input/improved-lstm-baseline-bi-lstm-dual-embed-dehyp/submission.csv'
f_lstm_fast = '.. /input/improved-lstm-baseline-fasttext-dropout/submission.csv'
f_nbsvm = '.. /input/nb-svm-strong-linear-baseline/submission.csv'
f_bi_post = '.. /input/bi-post/10fold_lstmpp_am.csv'
f_dpcnn = '.. /input/dpcnn-wordcloud/10fold_dpcnn_test.csv'
f_dmcnn = '.. /input/dmcnn-demoji/10fold_dmcnn_am.csv'
f_rcn = '.. /input/rcn-capsule/10fold_capsule_am.csv'<load_from_csv>
train[train['Embarked'].isnull() ]
train['Embarked'] = train['Embarked'].fillna('S' )
p_caps_gru = pd.read_csv(f_caps_gru)
 p_dual_embed_pl = pd.read_csv(f_dual_embed_pl)
 p_dual_embed_mish = pd.read_csv(f_dual_embed_mish)
 p_lstm_glove_tta = pd.read_csv(f_lstm_glove_tta)
 p_dual_embed_dehyp = pd.read_csv(f_dual_embed_dehyp)
 p_lstm_fast = pd.read_csv(f_lstm_fast)
 p_nbsvm = pd.read_csv(f_nbsvm)
 p_bi_post = pd.read_csv(f_bi_post)
 p_dpcnn = pd.read_csv(f_dpcnn)
 p_dmcnn = pd.read_csv(f_dmcnn)
 p_rcn = pd.read_csv(f_rcn )<define_variables>
print('Number of missing values in Embarked column for train set:', test.Embarked.isnull().sum() )
label_cols = ['toxic', 'severe_toxic', 'obscene', 'threat', 'insult', 'identity_hate']
p_res = p_caps_gru.copy()
p_res[label_cols] =(p_caps_gru[label_cols] + p_dual_embed_pl[label_cols] + p_dual_embed_mish[label_cols] + p_lstm_glove_tta[label_cols] + p_dual_embed_dehyp[label_cols] + p_lstm_fast[label_cols] + p_nbsvm[label_cols] + p_bi_post[label_cols] + p_dpcnn[label_cols] + p_dmcnn[label_cols] + p_rcn[label_cols])/ 11<save_to_csv>
y = train["Survived"]
features = ["Sex", "Embarked", "Pclass", "Fare"]
X = pd.get_dummies(train[features])
 X_test = pd.get_dummies(test[features])
 model = RandomForestClassifier(n_estimators=125, max_depth=5, random_state=1)
 model.fit(X, y)
 predictions = model.predict(X_test)
 output = pd.DataFrame({'PassengerId': test.PassengerId, 'Survived': predictions})
 output.to_csv('contract_submission.csv', index=False)
 print("Your submission was successfully saved!" )