Kaggle competition: Titanic - Machine Learning from Disaster
from sklearn.model_selection import StratifiedKFold,KFold
import lightgbm as lgb<choose_model_class>
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.utils.data as tud
n_fold = 5
folds = StratifiedKFold(n_splits=n_fold, shuffle=True, random_state=42 )<init_hyperparams>
trainRaw = pd.read_csv('/kaggle/input/titanic/train.csv')
 testRaw = pd.read_csv('/kaggle/input/titanic/test.csv')
 answer = pd.read_csv('/kaggle/input/titanic-answer/answers.csv' )
params = {'num_leaves': 8,
'min_data_in_leaf': 80,
'min_sum_hessian_in_leaf': 10.0,
'objective': 'binary',
'max_depth': 16,
'num_leaves': 13,
'learning_rate': 0.0085,
'boosting': 'gbdt',
'bagging_freq': 5,
'bagging_fraction': 0.38,
'feature_fraction': 0.04,
'bagging_seed': 11,
'reg_alpha': 0.1302650970728192,
'reg_lambda': 0.3603427518866501,
'random_state': 42,
'metric': 'auc',
'verbosity': -1,
'subsample': 0.81,
'min_gain_to_split': 0.01077313523861969,
'min_child_weight': 19.428902804238373,
'num_threads': 8}<train_model>
Pclass_dic = ['Pclass_1', 'Pclass_2', 'Pclass_3']
all_data[Pclass_dic] = all_data[Pclass_dic]/3
Sex_dic = ['Sex_female', 'Sex_male']
all_data[Sex_dic] = all_data[Sex_dic]/2
Embarked_dic = ['Embarked_C', 'Embarked_Q', 'Embarked_S']
all_data[Embarked_dic] = all_data[Embarked_dic]/3
Title_dic = ['Title_Master', 'Title_Miss', 'Title_Mr', 'Title_Mrs', 'Title_Officer', 'Title_Royalty']
all_data[Title_dic] = all_data[Title_dic]/6
FamilyLabel_dic = ['FamilyLabel_0', 'FamilyLabel_1', 'FamilyLabel_2']
all_data[FamilyLabel_dic] = all_data[FamilyLabel_dic]/3
Deck_dic = ['Deck_A', 'Deck_B', 'Deck_C', 'Deck_D', 'Deck_E', 'Deck_F', 'Deck_G', 'Deck_T','Deck_U' ]
all_data[Deck_dic] = all_data[Deck_dic]/9
TicketGroup_dic = ['TicketGroup_0', 'TicketGroup_1', 'TicketGroup_2']
all_data[TicketGroup_dic] = all_data[TicketGroup_dic]/3
prediction = np.zeros(len(test))
for fold_n,(train_index, valid_index)in enumerate(folds.split(train,y)) :
print('Fold', fold_n)
 X_train, X_valid = train.iloc[train_index], train.iloc[valid_index]
y_train, y_valid = y.iloc[train_index], y.iloc[valid_index]
train_data = lgb.Dataset(X_train, label=y_train)
 valid_data = lgb.Dataset(X_valid, label=y_valid)
 model = lgb.train(params,train_data,num_boost_round=20000,
valid_sets = [train_data, valid_data],verbose_eval=300,early_stopping_rounds = 200)
 prediction += model.predict(test, num_iteration=model.best_iteration)/5<load_from_csv>
def minmaxscaler(data):
min = np.amin(data)
 max = np.amax(data)
 return(data - min)/(max-min)
 def feature_normalize(data):
mu = np.mean(data,axis=0)
 std = np.std(data,axis=0)
 return(data - mu)/std
def unnormalized_show(img):
img = img * std + mu
npimg = img.numpy()
plt.figure()
plt.imshow(np.transpose(npimg,(1, 2, 0)) )
sub1=pd.read_csv(".. /input/sample_submission.csv" )<feature_engineering>
all_data.Age = minmaxscaler(all_data.Age)
 all_data.Fare = minmaxscaler(all_data.Fare )
sub1["target"]=prediction<save_to_csv>
train=all_data[all_data['Survived'].notnull() ]
test=all_data[all_data['Survived'].isnull() ].drop('Survived',axis=1)
 test_submssion=testRaw[['PassengerId']]
y = train.Survived
X = train.drop(['Survived'],axis=1 )
sub1.to_csv("submissionlgb.csv",index=False )<set_options>
class DATASET(tud.Dataset):
def __init__(self, train, test):
self.X = torch.from_numpy(np.asarray(train)).float()
self.y= torch.from_numpy(np.asarray(test)).float()
def __getitem__(self, index):
return self.X[index], self.y[index]
def __len__(self):
return len(self.y)
 D_IN, H1, H2, H3, D_OUT = 31, 100, 100, 10, 1
DROP_PROB1, DROP_PROB2 = 0.5, 0.5
class Base_net(nn.Module):
def __init__(self):
super(Base_net, self ).__init__()
self.linear1 = nn.Linear(D_IN, H1)
 self.linear2 = nn.Linear(H1, H2)
 self.linear3 = nn.Linear(H2, H3)
 self.linear4 = nn.Linear(H3, D_OUT)
 self.dp1 = nn.Dropout(DROP_PROB1)
 self.dp2 = nn.Dropout(DROP_PROB2)
 def forward(self, x):
x = F.relu(self.linear1(x))
x = F.leaky_relu(self.linear2(x))
x = self.dp1(x)
 x = F.leaky_relu(self.linear3(x))
x = self.dp2(x)
 x = F.leaky_relu(self.linear4(x))
x = torch.sigmoid(x)
 return x
pd.set_option('display.max_columns', 200)
 InteractiveShell.ast_node_interactivity = "all"
warnings.filterwarnings('ignore' )<load_from_csv>
full_dataset = DATASET(X, y)
 answer_dataset = DATASET(test, answer.Survived)
 full_dataloador = tud.DataLoader(full_dataset, batch_size=64, shuffle=False, drop_last=False)
 LEN_FULL_TRAIN = len(full_dataset.y)
 LEN_ANSWER = len(answer_dataset.y)
 LEARNING_RATE = 1e-3
WEIGHT_DECAY = 2e-4
model = Base_net()
optimizer = torch.optim.Adam(model.parameters() , weight_decay=WEIGHT_DECAY, lr=LEARNING_RATE)
 loss_func = nn.BCELoss()
for epoch in range(1000):
model.train()
for batch_x, batch_y in full_dataloador:
barch_y_pred = model(batch_x)
 loss = loss_func(barch_y_pred, batch_y.unsqueeze(dim=1))
optimizer.zero_grad()
loss.backward()
optimizer.step()
model.eval()
train_pred = model(full_dataset.X)
 train_pred = train_pred.detach().apply_(lambda x : 1 if x > 0.6 else 0)
 train_acc = np.sum(train_pred.numpy().flatten() == full_dataset.y.numpy().flatten())/ LEN_FULL_TRAIN
answer_pred = model(answer_dataset.X)
 answer_pred = answer_pred.detach().apply_(lambda x : 1 if x > 0.6 else 0)
 answer_acc = np.sum(answer_pred.numpy().flatten() == answer_dataset.y.numpy().flatten())/ LEN_ANSWER
if epoch % 20 == 0 :
print("epoch: {} | loss: {} | train acc: {} | answer acc: {}".format(epoch, loss.item() , train_acc, answer_acc))
if answer_acc > 0.81 :
print("epoch: {} | loss: {} | train acc: {} | answer acc: {}".format(epoch, loss.item() , train_acc, answer_acc))
break
train_df = pd.read_csv('.. /input/train.csv')
 test_df = pd.read_csv('.. /input/test.csv' )<count_missing_values>
test = torch.from_numpy(np.asarray(test)).float()
model.eval()
test_submssion.loc[:,'Survived'] = model(test ).detach().numpy().flatten()
test_submssion.loc[:,'Survived'] = test_submssion.loc[:,'Survived'].apply(lambda x : 1 if x > 0.5 else 0 )
train_df.isnull().sum().sum()
test_df.isnull().sum().sum()<feature_engineering>
test_submssion.to_csv('/kaggle/working/submssion.csv',index=False )