Kaggle competition: Titanic - Machine Learning from Disaster
features['Title'] = df_title['Name']
features['Child'] =(full['Age'] <= 14 ).astype(int)
 print(features )<feature_engineering>
data_raw = pd.read_csv('.. /input/titanic/train.csv')
 data_val = pd.read_csv('.. /input/titanic/test.csv')
 data1 = data_raw.copy(deep=True)
 data_cleaner = [data1, data_val]
Target = ['Survived']
def parse_surname(name):
return name.split(',')[0]
family = pd.DataFrame(full[['Parch','SibSp','Ticket']])
 family['Family_size'] = 1 + family.Parch + family.SibSp
family['Surname'] = full.Name.map(parse_surname)
 dict_scount = dict(family.groupby('Surname' ).Family_size.count())
 dict_scode = dict(zip(dict_scount.keys() , range(len(dict_scount))))
family['Surname_code'] = family['Surname'].map(dict_scode)
 family['Surname_count'] = family['Surname'].map(dict_scount)
 display(full[family.Surname == 'Smith'])
 print(family )<feature_engineering>
for dataset in data_cleaner:
dataset.Age.fillna(dataset.Age.median() , inplace=True)
 dataset.Embarked.fillna('S', inplace=True)
 dataset.Fare.fillna(dataset.Fare.median() , inplace=True)
 dataset['Family_members'] = dataset.Parch + dataset.SibSp
surname2chk = family[family['Family_size'] < family['Surname_count']].Surname.unique()
family['Surname_adj'] = family['Surname']
for s in surname2chk:
family_regroup = family[family['Surname'] == s]
fam_code_dict = tick2fam_gen(family_regroup)
 for idx in family_regroup.index:
curr_ticket = full.loc[idx].Ticket
fam_code = fam_code_dict[curr_ticket]
if family_regroup.loc[idx, 'Family_size'] == 1:
if family_regroup.Ticket.value_counts() [curr_ticket] > 1:
family.loc[idx, 'Surname_adj'] = s + '-hidfam' + fam_code
else:
family.loc[idx, 'Surname_adj'] = s + '-single' + fam_code
else:
family.loc[idx, 'Surname_adj'] = s + '-fam' + fam_code
display(family[family.Surname == 'Smith'] )<count_unique_values>
data1.drop(['Name', 'PassengerId', 'Ticket', 'SibSp', 'Parch'], axis=1, inplace=True )
dict_fcount = dict(family.groupby('Surname_adj' ).Family_size.count())
 dict_fcode = dict(zip(dict_fcount.keys() , range(len(dict_fcount))))
family['Family_code'] = family['Surname_adj'].map(dict_fcode)
 family['Family_count'] = family['Surname_adj'].map(dict_fcount)
 display(family[family.Surname == 'Smith'])
 print(f"No.of Family Before Regrouping: {len(family.Surname_code.unique())}")
 print(f"No.of Family After Regrouping: {len(family.Family_code.unique())}" )<categorify>
for dataset in data_cleaner:
dataset['Cabin_Allotted'] = np.where(dataset.Cabin.isnull() , 0, 1)
 dataset.drop('Cabin', axis=1, inplace=True )
group = pd.DataFrame(family[['Surname_code','Surname_count','Family_code','Family_count']])
 dict_tcount = dict(full.groupby('Ticket' ).PassengerId.count())
 dict_tcode = dict(zip(dict_tcount.keys() ,range(len(dict_tcount))))
group['Ticket_code'] = full.Ticket.map(dict_tcode)
 group['Ticket_count'] = full.Ticket.map(dict_tcount)
 print(f"No.of Tickets Identified: {len(group['Ticket_code'].unique())}")
 display(full[(full.Ticket == 'A/4 48871')|(full.Ticket == 'A/4 48873')] )<categorify>
lb = LabelEncoder()
for dataset in data_cleaner:
dataset['Sex_labeled'] = lb.fit_transform(dataset.Sex)
 dataset['AgeBin'] = pd.qcut(dataset.Age, 3)
 dataset['Age_labeled'] = lb.fit_transform(dataset['AgeBin'])
 dataset['FareBin'] = pd.qcut(dataset.Fare, 4)
 dataset['Fare_labeled'] = lb.fit_transform(dataset['FareBin'])
 dataset['Embarked_labeled'] = lb.fit_transform(dataset.Embarked )
def ChainCombineGroups(df, colA, colB):

 data = df.copy()
search_df = data.copy()
group_count = 0
while not search_df.empty:
pool = search_df.iloc[:1]
idx = pool.index
search_df.drop(index = idx, inplace = True)
 flag_init = 1
update = pd.DataFrame()
while(flag_init or not update.empty):
flag_init = 0
pool_A_uniq = np.unique(pool[colA])
 pool_B_uniq = np.unique(pool[colB])
 for col in [colA,colB]:
idx = []
for num in np.unique(pool[col]):
idx.extend(search_df[search_df[col] == num].index)
 update = search_df.loc[idx]
pool = pd.concat([pool, update], axis = 0)
 search_df = search_df.drop(index = idx)
 data.loc[pool.index, 'Group_'] = group_count
group_count += 1
return np.array(data['Group_'].astype(int))<groupby>
print(data1['Age_labeled'].value_counts())
 print(data1['Fare_labeled'].value_counts() )
group['Group_code'] = ChainCombineGroups(group, 'Family_code', 'Ticket_code')
 dict_gcount = dict(group.groupby('Group_code' ).Family_code.count())
 group['Group_count'] = group.Group_code.map(dict_gcount)
 print(f"Family: {len(family['Family_code'].unique())}")
 print(f"Group: {len(group['Ticket_code'].unique())}")
 print(f"Combined: {len(group['Group_code'].unique())}
")
 print('An example of grouping the both friends and family under a same group:')
 display(pd.concat([full['Ticket'],family[['Surname','Family_code']],group[['Ticket_code','Group_code']]], axis = 1)[group['Group_code'] == 458] )<concatenate>
MLA = [
ensemble.AdaBoostClassifier() ,
ensemble.BaggingClassifier() ,
ensemble.ExtraTreesClassifier() ,
ensemble.GradientBoostingClassifier() ,
ensemble.RandomForestClassifier() ,
neighbors.KNeighborsClassifier() ,
tree.DecisionTreeClassifier() ,
tree.ExtraTreeClassifier() ,
XGBClassifier(objective='binary:logistic', eval_metric='logloss')
 ]
cv_split = model_selection.ShuffleSplit(n_splits=10, test_size=.2, train_size=.8, random_state=1)
 MLA_columns = ['MLA Name', 'MLA Parameters', 'MLA Train Accuracy Mean', 'MLA Test Accuracy Mean', 'MLA Test Accuracy 3*STD', 'MLA Time']
MLA_compare = pd.DataFrame(columns = MLA_columns)
 row_index = 0
for alg in MLA:
MLA_name = alg.__class__.__name__
MLA_compare.loc[row_index, 'MLA Name'] = MLA_name
MLA_compare.loc[row_index, 'MLA Parameters'] = str(alg.get_params())
 cv_results = model_selection.cross_validate(alg, data1[data1_X], data1[Target].values.reshape(-1,), cv=cv_split, return_train_score=True)
 MLA_compare.loc[row_index, 'MLA Time'] = cv_results['fit_time'].mean()
MLA_compare.loc[row_index, 'MLA Train Accuracy Mean'] = cv_results['train_score'].mean()
MLA_compare.loc[row_index, 'MLA Test Accuracy Mean'] = cv_results['test_score'].mean()
MLA_compare.loc[row_index, 'MLA Test Accuracy 3*STD'] = cv_results['test_score'].std() *3
row_index += 1
MLA_compare
group_final = pd.concat([family[['Surname_code','Surname_count','Family_code','Family_count']],
group[['Ticket_code','Ticket_count','Group_code','Group_count']],
full['Survived']], axis = 1 )<groupby>
grid_n_estimator = [10, 50, 100, 300]
grid_ratio = [.1,.25,.5,.75, 1.0]
grid_learn = [.01,.03,.05,.1,.25]
grid_max_depth = [1, 2, 3, 4, 5, 6, 7, 8, 10, None]
grid_min_samples = [5, 10,.03,.05,.10]
grid_criterion = ['gini', 'entropy']
grid_seed = [1]
grid_param = [
[{
'learning_rate': grid_learn,
'n_estimators': grid_n_estimator,
'random_state': grid_seed
}],
[{
'max_samples': grid_ratio,
'n_estimators': grid_n_estimator,
'random_state': grid_seed
}],
[{
'n_estimators': grid_n_estimator,
'criterion': grid_criterion,
'max_depth': grid_max_depth,
'random_state': grid_seed
}],
[{
'learning_rate': grid_learn,
'n_estimators': grid_n_estimator,
'max_depth': grid_max_depth,
'random_state': grid_seed
}],
[{
'n_estimators': grid_n_estimator,
'criterion': grid_criterion,
'max_depth': grid_max_depth[:-1],
'random_state': grid_seed,
}],
[{
'n_neighbors': grid_max_depth[:-1],
'weights': ['uniform', 'distance'],
'algorithm': ['auto', 'ball_tree', 'kd_tree', 'brute']
}],
[{
'criterion': grid_criterion,
'max_depth': grid_max_depth,
'random_state': grid_seed,
}],
[{
'criterion': grid_criterion,
'max_depth': grid_max_depth,
'random_state': grid_seed,
}],
[{
'learning_rate': grid_learn,
'max_depth': grid_max_depth[:-1],
'n_estimators': grid_n_estimator,
'seed': grid_seed,
'objective': ['binary:logistic'],
'eval_metric': ['logloss']
}]
]
row_index = 0
start_total = time.perf_counter()
for clf, param in zip(MLA, grid_param):
start = time.perf_counter()
best_search = model_selection.GridSearchCV(estimator=clf, param_grid=param, cv=cv_split, scoring='roc_auc')
 best_search.fit(data1[data1_X], data1[Target].values.reshape(-1,))
run = time.perf_counter() - start
best_param = best_search.best_params_
print(f'The best parameter for {clf.__class__.__name__} is {best_param} with a runtime of {run:.2f} seconds.')
 print('-'*60)
 clf.set_params(**best_param)
 MLA_name = clf.__class__.__name__
MLA_compare.loc[row_index, 'MLA Name'] = MLA_name
MLA_compare.loc[row_index, 'MLA Parameters'] = str(clf.get_params())
 cv_results = model_selection.cross_validate(clf, data1[data1_X], data1[Target].values.reshape(-1,), cv=cv_split, return_train_score=True)
 MLA_compare.loc[row_index, 'MLA Time'] = cv_results['fit_time'].mean()
MLA_compare.loc[row_index, 'MLA Train Accuracy Mean'] = cv_results['train_score'].mean()
MLA_compare.loc[row_index, 'MLA Test Accuracy Mean'] = cv_results['test_score'].mean()
MLA_compare.loc[row_index, 'MLA Test Accuracy 3*STD'] = cv_results['test_score'].std() *3
row_index += 1
run_total = time.perf_counter() - start_total
print('-'*60)
 print(f'Total optimization time was {run_total / 60:.2f} minutes.')
 print('-'*60)
 MLA_compare.sort_values(by=['MLA Test Accuracy Mean'], ascending=False, inplace=True)
 MLA_compare
for param in [('Surname_code','Surname_count'), 
('Family_code','Family_count'), 
('Ticket_code','Ticket_count'), 
('Group_code','Group_count')]:
n_member_survived_by_gp = group_final.groupby(param[0] ).Survived.sum()
n_mem_survived = group_final[param[0]].map(n_member_survived_by_gp)
 n_mem_survived_adj = n_mem_survived - group_final.Survived.apply(lambda x: 1 if x == 1 else 0)
 n_member_dead_by_gp = group_final.groupby(param[0] ).Survived.count() - group_final.groupby(param[0] ).Survived.sum()
n_mem_dead = group_final[param[0]].map(n_member_dead_by_gp)
 n_mem_dead_adj = n_mem_dead - group_final.Survived.apply(lambda x: 1 if x == 0 else 0)
 unknown_factor =(group_final[param[1]] - n_mem_survived_adj - n_mem_dead_adj)/group_final[param[1]]
confidence = 1 - unknown_factor
key = 'Confidence_member_survived'+'_'+param[0]
ratio =(1/group_final[param[1]])*(n_mem_survived_adj - n_mem_dead_adj)
 group_final[key] = confidence * ratio
plt.barh(group_final.corr().Survived[-4:].index, group_final.corr().Survived[-4:])
 plt.xlabel('Correlation with Survived');
features['Cf_mem_survived'] = group_final['Confidence_member_survived_Group_code']<feature_engineering>
model = ensemble.RandomForestClassifier(**{'criterion': 'entropy', 'max_depth': 5, 'n_estimators': 50, 'random_state': 1})
 model.fit(data1[data1_X], data1[Target].values.reshape(-1,))
predictions = model.predict(data_val[data1_X])
 output = pd.DataFrame({'PassengerId': data_val.PassengerId, 'Survived': predictions})
 output.to_csv('./my_submission_RandomForestClassifier_tunned_F4.csv', index=False)
 print("Your submission was successfully saved!" )