Kaggle competition: Titanic - Machine Learning from Disaster
total_epoches = 20
step_size = 5
base_lr = 0.01
batch_size = 64
optimizer = optim.Adam(model.parameters() , lr=base_lr)
 criterion = nn.CrossEntropyLoss()
exp_lr_scheduler = lr_scheduler.StepLR(optimizer, step_size=step_size, gamma=0.1)
 if torch.cuda.is_available() :
model = model.cuda()
criterion = criterion.cuda()<create_dataframe>
train_data = pd.read_csv("/kaggle/input/titanic/train.csv")
 train_data.head()
def split_dataframe(dataframe=None, fraction=0.9, rand_seed=1):
df_1 = dataframe.sample(frac=fraction, random_state=rand_seed)
 df_2 = dataframe.drop(df_1.index)
 return df_1, df_2<train_model>
test_data = pd.read_csv("/kaggle/input/titanic/test.csv")
 test_data.head()
for epoch in range(total_epoches):
print("
Train Epoch {}: lr = {}".format(epoch, exp_lr_scheduler.get_lr() [0]))
train_df_new, val_df = split_dataframe(dataframe=train_df, fraction=0.9, rand_seed=epoch)
 train_dataset = MNISTDataset(train_df_new, transform=train_transforms)
 val_dataset = MNISTDataset(val_df, transform=val_test_transforms)
 train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
batch_size=batch_size, shuffle=True)
 val_loader = torch.utils.data.DataLoader(dataset=val_dataset,
batch_size=batch_size, shuffle=False)
 train(train_loader=train_loader, model=model, criterion=criterion, optimizer=optimizer, epoch=epoch)
 validate(val_loader=val_loader, model=model, criterion=criterion)
 exp_lr_scheduler.step() 
<predict_on_test>
def fill_age_random(df, age_column):

 if age_column+"_filled" not in df:
df.insert(( df.columns.get_loc(age_column)+1), age_column+"_filled", df[age_column])
 keys = []
values = []
for key, value in df[age_column].value_counts().items() :
keys.append(key)
 values.append(value)
 indices = [idx for idx, b in enumerate(df[age_column].isna())if b == True]
for idx, series in df.iterrows() :
if idx in indices:
df.loc[idx, age_column+"_filled"] = random.choices(keys, values)
 return df
def prediciton(test_loader, model):
model.eval()
test_pred = torch.LongTensor()
for i, data in enumerate(test_loader):
if torch.cuda.is_available() :
data = data.cuda()
output = model(data)
 pred = output.cpu().data.max(1, keepdim=True)[1]
test_pred = torch.cat(( test_pred, pred), dim=0)
 return test_pred<create_dataframe>
fill_age_random(test_data, "Age")
 test_data.info()
<save_to_csv><EOS> 
y_train = train_data["Survived"]
train_data["Sex_cat"] = train_data["Sex"].astype("category")
 train_data["Sex_code"] = train_data["Sex_cat"].cat.codes
test_data["Sex_cat"] = test_data["Sex"].astype("category")
 test_data["Sex_code"] = test_data["Sex_cat"].cat.codes
features = ["Pclass", "Sex_code", "Age_filled"]
X_test = test_data[features].copy()
X_train = train_data[features].copy()
model = RandomForestClassifier(n_estimators=100, max_depth=5, random_state=1)
 model.fit(X_train, y_train)
 predictions = model.predict(X_test)
 output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions})
 output.to_csv('my_submission.csv', index=False)
 print("Your submission is succesfully saved!" )