Kaggle competition: Titanic - Machine Learning from Disaster
%%time
oof_preds = np.zeros(train_df.shape[0])
 sub_preds = np.zeros(test_df.shape[0])
 feature_importance_df = pd.DataFrame()
for n_fold,(trn_idx, val_idx)in enumerate(folds.split(train_df, y_train)) :
trn_x, trn_y = train_df[train_cols].iloc[trn_idx], y_train.iloc[trn_idx]
val_x, val_y = train_df[train_cols].iloc[val_idx], y_train.iloc[val_idx]
dtrain = xgb.DMatrix(trn_x, trn_y, feature_names=trn_x.columns)
 dval = xgb.DMatrix(val_x, val_y, feature_names=val_x.columns)
 clf = xgb.train(params=params, dtrain=dtrain, num_boost_round=4000, evals=[(dtrain, "Train"),(dval, "Val")],
verbose_eval= 100, early_stopping_rounds=50)
 oof_preds[val_idx] = clf.predict(xgb.DMatrix(val_x))
sub_preds += clf.predict(xgb.DMatrix(test_df[train_cols])) / folds.n_splits
fold_importance_df = pd.DataFrame()
fold_importance_df["feature"] = pd.DataFrame.from_dict(data=clf.get_fscore() , orient="index", columns=["FScore"])["FScore"].index
fold_importance_df["fscore"] = pd.DataFrame.from_dict(data=clf.get_fscore() , orient="index", columns=["FScore"])["FScore"].values
fold_importance_df["fold"] = n_fold + 1
feature_importance_df = pd.concat([feature_importance_df, fold_importance_df], axis=0)
 print('
Fold %1d AUC %.6f & std %.6f' %(n_fold + 1, roc_auc_score(val_y, oof_preds[val_idx]), np.std([oof_preds[val_idx]])))
 print('Fold %1d Precision %.6f' %(n_fold + 1, precision_score(val_y, np.round(oof_preds[val_idx]))))
print('Fold %1d Recall %.6f' %(n_fold + 1, recall_score(val_y, np.round(oof_preds[val_idx]))))
print('Fold %1d F1 score %.6f' %(n_fold + 1,f1_score(val_y, np.round(oof_preds[val_idx]))))
print('Fold %1d Kappa score %.6f
' %(n_fold + 1,cohen_kappa_score(val_y, np.round(oof_preds[val_idx]))))
gc.collect()
print('
CV AUC score %.6f & std %.6f' %(roc_auc_score(y_train, oof_preds), np.std(( oof_preds))))
print('CV Precision score %.6f' %(precision_score(y_train, np.round(oof_preds))))
print('CV Recall score %.6f' %(recall_score(y_train, np.round(oof_preds))))
print('CV F1 score %.6f' %(f1_score(y_train, np.round(oof_preds))))
print('CV Kappa score %.6f' %(cohen_kappa_score(y_train, np.round(oof_preds))))<compute_test_metric>
mainData = pd.read_csv('/kaggle/input/titanic/train.csv')
 testData = pd.read_csv('/kaggle/input/titanic/test.csv' )
print(confusion_matrix(y_train, np.round(oof_preds)) )<sort_values>
mainData = mainData[mainData.Embarked.notna() ]
mainData.drop(['Cabin'], axis='columns', inplace=True)
 testData.drop(['Cabin'], axis='columns', inplace=True )
feature_importance_df.groupby(["feature"])["fscore",].mean().sort_values("fscore", ascending=False )<feature_engineering>
def rank(row):
a = ['Mrs.','Mr.','Miss','Master']
ret = []
for b in a:
if b in row.Name:
ret.append(b)
 if len(ret)==0:
if row.Sex == 'male':
ret = ['Mr.']
else:
ret = ['Miss']
return ' '.join(ret)
 mainData['rank'] = mainData.apply(rank, axis = 1)
 testData['rank'] = testData.apply(rank, axis = 1 )
test_df['target'] = sub_preds<compute_test_metric>
mainData = mainData.drop(['Name','Ticket','Sex'], axis = 1)
 testData = testData.drop(['Name','Ticket','Sex'], axis = 1 )
oof_roc = roc_auc_score(y_train, oof_preds)
 oof_roc<save_to_csv>
mainData["Embarked"] = mainData["Embarked"].map({'S': 1,'C':2,'Q':3})
 testData["Embarked"] = testData["Embarked"].map({'S': 1,'C':2,'Q':3})
 mainData["rank_num"] = mainData["rank"].map({'Mrs.': 1,'Mr.':2,'Miss':3,'Master':4})
 testData["rank_num"] = testData["rank"].map({'Mrs.': 1,'Mr.':2,'Miss':3,'Master':4})
 def grouping(row):
a = row.Pclass
a = a * 10 + row.rank_num
a = a * 10 + row.Embarked
return a
mainData['grouping'] = mainData.apply(grouping, axis = 1 ).astype(int)
 testData['grouping'] = testData.apply(grouping, axis = 1 ).astype(int )
ss = pd.DataFrame({"ID_code":test_df["ID_code"], "target":test_df["target"]})
 ss.to_csv("sant_xgb_%sFold_%.6f.csv"%(folds.n_splits, oof_roc), index=None)
 ss.head()<set_options>
mainData['predict'] = mainData.groupby('grouping')['Survived'].transform(lambda x: x.mode() [0])
 groupDict = dict(zip(mainData.grouping, mainData.predict))
print(f'Train accuracy is {1 - abs(mainData.Survived - mainData.predict ).sum() /mainData.shape[0]:.2%}')
 groupValTrain = set(mainData.grouping)
 groupValTest = set(testData.grouping)
 groupDiff = groupValTest - groupValTrain
groupDiff
testData.query('grouping in @groupDiff' )
sns.set_style('whitegrid')
 print(os.listdir(".. /input")) 
<load_from_csv>
for v in groupDiff:
groupDict[v] = 1
%%time
train = pd.read_csv('.. /input/train.csv' )<load_from_csv>
testData['Survived'] = testData.grouping.apply(lambda x: groupDict[x])
 testData[['PassengerId','Survived']].to_csv("final_submission.csv",index = False)
 print("That's all!" )