Kaggle competition: 2019  ML competition with KISTI
<SOS> metric: accuracy Kaggle data source: 2019-ml-competition-with-kisti<save_to_csv>
print(os.listdir(".. /input"))
warnings.filterwarnings('ignore')
 plt.rcParams['figure.figsize'] =(16,9)
 sns.set_palette('gist_earth' )
results = pd.Series(results,name="Label")
 submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("keras_cnn_mnist_aug_ensemble2.csv",index=False )<load_from_csv>
df_train = pd.read_csv('.. /input/train.csv')
 df_test = pd.read_csv('.. /input/test.csv')
 full = pd.concat([df_train, df_test], axis = 0, sort=True)
 full.set_index('PassengerId', drop = False, inplace=True)
 train = full[:891]
display(full.head() )
train = pd.read_csv(".. /input/titanic/train.csv")
 train.head()<load_from_csv>
def parse_Cabin(cabin):
if type(cabin)== str:
m = re.search(r'([A-Z])+', cabin)
 return m.group(1)
 else:
return 'X'
full['Cabin_short'] = full['Cabin'].map(parse_Cabin )
test = pd.read_csv(".. /input/titanic/test.csv")
 test.head()<concatenate>
dict_fare_by_Pclass = dict(full.groupby('Pclass' ).Fare.mean())
 print(dict_fare_by_Pclass)
 missing_fare = full.loc[full.Fare == 0,'Pclass'].map(dict_fare_by_Pclass)
 full.loc[full.Fare == 0,'Fare'] = missing_fare
missing_fare = full.loc[full.Fare.isnull() ,'Pclass'].map(dict_fare_by_Pclass)
 full.loc[full.Fare.isnull() ,'Fare'] = missing_fare
display(full.head(180))
all = pd.concat([train, test], sort = False)
 all.info()<feature_engineering>
features = pd.DataFrame()
features['Pclass'] = full['Pclass']
features['Fare'] = full['Fare']
features['Sex'] = full['Sex']
all['Age'] = all['Age'].fillna(value=all['Age'].median())
 all['Fare'] = all['Fare'].fillna(value=all['Fare'].median() )<feature_engineering>
features['A5'] =(full['Ticket_short'] == 'A5' ).astype(int)
 features['PC'] =(full['Ticket_short'] == 'PC' ).astype(int)
 print(features )
all.loc[ all['Age'] <= 16, 'Age'] = 0
all.loc[(all['Age'] > 16)&(all['Age'] <= 32), 'Age'] = 1
all.loc[(all['Age'] > 32)&(all['Age'] <= 48), 'Age'] = 2
all.loc[(all['Age'] > 48)&(all['Age'] <= 64), 'Age'] = 3
all.loc[ all['Age'] > 64, 'Age'] = 4<count_values>
dict_Title = {"Capt": "Officer",
"Col": "Officer",
"Major": "Officer",
"Jonkheer": "Royalty",
"Don": "Royalty",
"Sir" : "Royalty",
"Dr": "Officer",
"Rev": "Officer",
"the Countess":"Royalty",
"Dona": "Royalty",
"Mme": "Mrs",
"Mlle": "Miss",
"Ms": "Mrs",
"Mr" : "Mr",
"Mrs" : "Mrs",
"Miss" : "Miss",
"Master" : "Master",
"Lady" : "Royalty"
}
title = title.map(dict_Title)
 plt.figure(figsize =(14,6))
sns.violinplot(x = title, y = full['Age']);
all['Title'] = all['Name'].apply(get_title)
 all['Title'].value_counts()<count_values>
df_title = pd.DataFrame(title ).join(full[['Age','Survived']])
 dict_age = df_title.groupby('Name' ).Age.mean()
idx = full.Age.isnull()
full.loc[idx,'Age'] = df_title.loc[idx, 'Name'].map(dict_age)
 display(full.head(6))
all['Title'] = all['Title'].replace(['Capt.', 'Dr.', 'Major.', 'Rev.'], 'Officer.')
 all['Title'] = all['Title'].replace(['Lady.', 'Countess.', 'Don.', 'Sir.', 'Jonkheer.', 'Dona.'], 'Royal.')
 all['Title'] = all['Title'].replace(['Mlle.', 'Ms.'], 'Miss.')
 all['Title'] = all['Title'].replace(['Mme.'], 'Mrs.')
 all['Title'].value_counts()<count_values>
features['Title'] = df_title['Name']
features['Child'] =(full['Age'] <= 14 ).astype(int)
 print(features )
all['Cabin'] = all['Cabin'].fillna('Missing')
 all['Cabin'] = all['Cabin'].str[0]
all['Cabin'].value_counts()<feature_engineering>
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 )
all['Family_Size'] = all['SibSp'] + all['Parch'] + 1
all['IsAlone'] = 0
all.loc[all['Family_Size']==1, 'IsAlone'] = 1
all.head()<drop_column>
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'] )
all_1 = all.drop(['Name', 'Ticket'], axis = 1)
 all_1.head()<categorify>
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())}" )
all_dummies = pd.get_dummies(all_1)
 all_dummies.info()<filter>
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')] )
all_test = all_dummies[all_dummies['Survived'].isna() ]
all_test.info()<split>
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))
X_train, X_test, y_train, y_test = train_test_split(all_train.drop(['PassengerId','Survived'],axis=1),
all_train['Survived'], test_size=0.30,
random_state=101 )<import_modules>
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] )
from sklearn.ensemble import RandomForestClassifier<choose_model_class>
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 )
RF_Model = RandomForestClassifier()<import_modules>
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']
from sklearn.model_selection import GridSearchCV<import_modules>
features['Parch'] = full['Parch']
features['SibSp'] = full['SibSp']
features['Group_size'] = group['Group_count']
features.head()
from sklearn.model_selection import GridSearchCV<define_search_space>
scalar = StandardScaler()
features_z_transformed = features.copy()
continuous = ['Fare']
features_z_transformed[continuous] = scalar.fit_transform(features_z_transformed[continuous])
 features_z_transformed.Sex = features_z_transformed.Sex.apply(lambda x: 1 if x == 'male' else 0)
 features_final = pd.get_dummies(features_z_transformed)
 encoded = list(features_final.columns)
 print("{} total features after one-hot encoding.".format(len(encoded)))
 features_final_train = features_final[:891]
features_final_test = features_final[891:]
parameters = {'n_estimators' :(10,30,50,70,90,100)
 , 'criterion' :('gini', 'entropy')
 , 'max_depth' :(3,5,7,9,10)
 , 'max_features' :('auto', 'sqrt')
 , 'min_samples_split' :(2,4,6)
 }<choose_model_class>
X_train, X_test, y_train, y_test = train_test_split(features_final_train,
train.Survived,
test_size = 0.2,
random_state = 0 )
RF_grid = GridSearchCV(RandomForestClassifier(n_jobs = -1, oob_score= False), param_grid = parameters, cv = 3, verbose = True )<train_model>
clf_A = GradientBoostingClassifier(random_state = 0)
 clf_B = LogisticRegression(random_state= 0)
 clf_C = RandomForestClassifier(random_state= 0)
 samples_100 = len(y_train)
 samples_10 = int(len(y_train)/2)
 samples_1 = int(len(y_train)/10)
 results = {}
for clf in [clf_A, clf_B, clf_C]:
clf_name = clf.__class__.__name__
results[clf_name] = {}
for i, samples in enumerate([samples_1, samples_10, samples_100]):
results[clf_name][i] = \
train_predict(clf, samples, X_train, y_train, X_test, y_test )
RF_grid_model = RF_grid.fit(X_train, y_train )<find_best_params>
warnings.filterwarnings('ignore')
 clf = RandomForestClassifier(random_state = 0, oob_score = True)
 parameters = {'criterion' :['gini'],
'n_estimators' : [350],
'max_depth':[5],
'min_samples_leaf': [4],
'max_leaf_nodes': [10],
'min_impurity_decrease': [0],
'max_features' : [1]
}
scorer = make_scorer(accuracy_score)
 grid_obj = GridSearchCV(clf, parameters, scoring = scorer, cv = 10)
 grid_fit = grid_obj.fit(X_train,y_train)
 best_clf = grid_fit.best_estimator_
predictions =(clf.fit(X_train, y_train)).predict(X_test)
 best_predictions = best_clf.predict(X_test)
 print("Unoptimized model
------")
 print("Accuracy score on testing data: {:.4f}".format(accuracy_score(y_test, predictions)))
 print("Oob score on testing data: {:.4f}".format(clf.oob_score_))
print("
Optimized Model
------")
 print("Final accuracy score on the testing data: {:.4f}".format(accuracy_score(y_test, best_predictions)))
 print("Final oob score on the testing data: {:.4f}".format(best_clf.oob_score_))
print("
Best Parameters
------")
 best_clf
<choose_model_class><EOS> 
final_predict = best_clf.predict(features_final_test)
 prediction = pd.DataFrame(full[891:].PassengerId)
 prediction['Survived'] = final_predict.astype('int')
 prediction.to_csv('./sample_submission.csv',index = False )