Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<split>
sns.set(context='notebook', style='white', palette='colorblind' )
df_test_per_match_solo_1, df_test_per_match_solo_2, df_test_per_match_solo=filter_small_teams_test(df_test_per_match_solo )<drop_column>
from sklearn.preprocessing import StandardScaler, LabelEncoder
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier, VotingClassifier
from sklearn.neighbors import KNeighborsClassifier
from xgboost import XGBClassifier
from sklearn.model_selection import StratifiedKFold, GridSearchCV, learning_curve, cross_val_score
del df_solo
del df_test_solo<define_variables>
train = pd.read_csv('/kaggle/input/titanic/train.csv')
 test = pd.read_csv('/kaggle/input/titanic/test.csv')
 df = pd.concat([train, test], axis=0, ignore_index=True)
 print(f'Train:{train.shape}
Test:{test.shape}
Df:{df.shape}' )
drop_features_solo=['killPoints','roadKills','teamKills','winPoints']<split>
df.isna().sum() [df.isna().sum() >0]
df_train_in_solo, df_train_groupids_solo, df_test_in_solo, df_test_groupids_solo = prepare_pca_input(df_red_solo, df_test_per_match_solo, dfeatures=drop_features_solo )<concatenate>
df.Sex=df.Sex.map({'male':0, 'female':1} ).astype('int' )
pca_solo=prepare_pca(df_train_in_solo, in_components=None )<prepare_x_and_y>
df['Family_Size']=df.SibSp + df.Parch
df.groupby('Family_Size')['Survived'].mean()
x_train_pca_solo, y_train_pca_solo, x_test_pca_solo=pca_transform_data(df_train_in_solo, df_test_in_solo, pca_solo )<drop_column>
df.Title.value_counts()
del df_red_solo
del df_test_in_solo
del df_train_in_solo<normalization>
df['Family_Size'] = df['Parch'] + df['SibSp']
x_train_scaled_solo, x_test_scaled_solo=scale_data(x_train_pca_solo, x_test_pca_solo )<compute_test_metric>
df['Last_Name'] = df['Name'].apply(lambda x: str.split(x, ",")[0])
 df['Fare'].fillna(df['Fare'].mean() , inplace=True)
 DEFAULT_SURVIVAL_VALUE = 0.5
df['Family_Survival'] = DEFAULT_SURVIVAL_VALUE
for grp, grp_df in df[['Survived','Name', 'Last_Name', 'Fare', 'Ticket', 'PassengerId',
'SibSp', 'Parch', 'Age', 'Cabin']].groupby(['Last_Name', 'Fare']):
if(len(grp_df)!= 1):
for ind, row in grp_df.iterrows() :
smax = grp_df.drop(ind)['Survived'].max()
smin = grp_df.drop(ind)['Survived'].min()
passID = row['PassengerId']
if(smax == 1.0):
df.loc[df['PassengerId'] == passID, 'Family_Survival'] = 1
elif(smin==0.0):
df.loc[df['PassengerId'] == passID, 'Family_Survival'] = 0
for _, grp_df in df.groupby('Ticket'):
if(len(grp_df)!= 1):
for ind, row in grp_df.iterrows() :
if(row['Family_Survival'] == 0)|(row['Family_Survival']== 0.5):
smax = grp_df.drop(ind)['Survived'].max()
smin = grp_df.drop(ind)['Survived'].min()
passID = row['PassengerId']
if(smax == 1.0):
df.loc[df['PassengerId'] == passID, 'Family_Survival'] = 1
elif(smin==0.0):
df.loc[df['PassengerId'] == passID, 'Family_Survival'] = 0
eval_score_solo=mlp_evaluate(x_train_scaled_solo, y_train_pca_solo, df_train_groupids_solo, training_level=10000, verbose=False )<train_model>
df['Family_Survival'].value_counts()
mlp_solo=mlp_train(x_train_scaled_solo, y_train_pca_solo )<predict_on_test>
df['Fare'].fillna(df['Fare'].median() , inplace = True)
 df['FareBin'] = pd.qcut(df['Fare'], 5)
 label = LabelEncoder()
df['FareBin_Code'] = label.fit_transform(df['FareBin'])
 df.drop(['Fare'], 1, inplace=True )
winPlacePerc_one_teams_solo=predict_one_team_matches(df_test_per_match_solo_1)
 winPlacePerc_two_teams_solo=predict_two_team_matches(df_test_per_match_solo_2 )<predict_on_test>
df['FareBin_Code'].value_counts()
winPlacePerc_solo = predict_winPlacePerc(x_test_scaled_solo, df_test_groupids_solo, mlp_solo, verbose=False)
 winPlacePerc_solo = winPlacePerc_solo.append(winPlacePerc_one_teams_solo ).append(winPlacePerc_two_teams_solo )<concatenate>
titles = ['Dr', 'Master', 'Miss', 'Mr', 'Mrs', 'Rev']
for title in titles:
age_to_impute = df.groupby('Title')['Age'].median() [titles.index(title)]
df.loc[(df['Age'].isnull())&(df['Title'] == title), 'Age'] = age_to_impute
df_red_crash=combine_training_teams(df_crash )<feature_engineering>
df['AgeBin'] = pd.qcut(df['Age'], 4)
 label = LabelEncoder()
df['AgeBin_Code'] = label.fit_transform(df['AgeBin'] )
df_red_crash_1, df_red_crash_2, df_red_crash=filter_small_teams(df_red_crash )<concatenate>
df['AgeBin_Code'].value_counts()
df_test_per_match_crash = combine_testing_teams(df_test_crash )<split>
df.drop(['Name', 'PassengerId', 'SibSp', 'Parch', 'Ticket', 'Cabin',
'Embarked', 'Last_Name', 'FareBin', 'AgeBin', 'Survived', 'Title', 'Age'], axis = 1, inplace = True )
df_test_per_match_crash_1, df_test_per_match_crash_2, df_test_per_match_crash=filter_small_teams_test(df_test_per_match_crash )<set_options>
X_train = df[:len(train)]
X_test = df[len(train):]
y_train = train['Survived']
df_crash.drop(['headshotKills','numGroups','swimDistance','teamKills'],axis=1 ).corr().style.format("{:.2}" ).background_gradient(cmap=plt.get_cmap('coolwarm'), axis=1 )<drop_column>
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
 X_test = scaler.transform(X_test )
del df_crash
del df_test_crash<define_variables>
kfold = StratifiedKFold(n_splits=8 )
drop_features_crash=['headshotKills','swimDistance','teamKills']<split>
RFC = RandomForestClassifier()
rf_param_grid = {"max_depth": [None],
"max_features": [3,"sqrt", "log2"],
"min_samples_split": [n for n in range(1, 9)],
"min_samples_leaf": [5, 7],
"bootstrap": [False, True],
"n_estimators" :[200, 500],
"criterion": ["gini", "entropy"]}
rf_param_grid_best = {"max_depth": [None],
"max_features": [3],
"min_samples_split": [4],
"min_samples_leaf": [5],
"bootstrap": [False],
"n_estimators" :[200],
"criterion": ["gini"]}
gs_rf = GridSearchCV(RFC, param_grid = rf_param_grid_best, cv=kfold, scoring="roc_auc", n_jobs= 4, verbose = 1)
 gs_rf.fit(X_train, y_train)
 rf_best = gs_rf.best_estimator_
RFC.fit(X_train, y_train )
df_train_in_crash, df_train_groupids_crash, df_test_in_crash, df_test_groupids_crash = prepare_pca_input(df_red_crash, df_test_per_match_crash, dfeatures=drop_features_crash )<concatenate>
print(f'RandomForest GridSearch best params: {gs_rf.best_params_}
')
 print(f'RandomForest GridSearch best score: {gs_rf.best_score_}')
 print(f'RandomForest score: {RFC.score(X_train,y_train)}' )
pca_crash=prepare_pca(df_train_in_crash, in_components=None )<prepare_x_and_y>
KNN = KNeighborsClassifier()
knn_param_grid = {'algorithm': ['auto'],
'weights': ['uniform', 'distance'],
'leaf_size': [20, 25, 30],
'n_neighbors': [12, 14, 16]}
knn_best_param_grid = {'algorithm': ['auto'],
'weights': ['uniform'],
'leaf_size': [25],
'n_neighbors': [14]}
gs_knn = GridSearchCV(KNN, param_grid = knn_best_param_grid, cv=kfold, scoring = "roc_auc", n_jobs= 4, verbose = 1)
 gs_knn.fit(X_train, y_train)
 KNN.fit(X_train, y_train)
 knn_best = gs_knn.best_estimator_
x_train_pca_crash, y_train_pca_crash, x_test_pca_crash=pca_transform_data(df_train_in_crash, df_test_in_crash, pca_crash )<drop_column>
print(f'KNN GridSearch best params: {gs_knn.best_params_}')
 print()
print(f'KNN GridSearch best score: {gs_knn.best_score_}')
 print(f'KNN score: {KNN.score(X_train,y_train)}' )
del df_red_crash
del df_test_in_crash
del df_train_in_crash<normalization>
knn1 = KNeighborsClassifier(algorithm='auto', leaf_size=26, metric='minkowski',
metric_params=None, n_jobs=1, n_neighbors=6, p=2,
weights='uniform')
 knn1.fit(X_train, y_train )
x_train_scaled_crash, x_test_scaled_crash=scale_data(x_train_pca_crash, x_test_pca_crash )<compute_test_metric>
print(f'KNN score - 2nd model: {knn1.score(X_train, y_train)}' )
eval_score_crash=mlp_evaluate(x_train_scaled_crash, y_train_pca_crash, df_train_groupids_crash, training_level=len(x_train_scaled_crash), verbose=False )<train_model>
GB = GradientBoostingClassifier()
gb_param_grid = {'loss' : ["deviance"],
'n_estimators' : [1000],
'learning_rate': [0.02, 0.05],
'min_samples_split': [15, 20, 25],
'max_depth': [4, 6],
'min_samples_leaf': [50, 60],
'max_features': ["sqrt"]
}
gb_param_grid_best = {'loss' : ["deviance"],
'n_estimators' : [1000],
'learning_rate': [0.02],
'min_samples_split': [25],
'max_depth': [4],
'min_samples_leaf': [60],
'max_features': ["sqrt"]
}
gs_gb = GridSearchCV(GB, param_grid = gb_param_grid_best, cv=kfold, scoring="roc_auc", n_jobs= 4, verbose = 1)
 gs_gb.fit(X_train,y_train)
 GB.fit(X_train, y_train)
 gb_best = gs_gb.best_estimator_
mlp_crash=mlp_train(x_train_scaled_crash, y_train_pca_crash )<predict_on_test>
print(f'GradienBoost GridSearch best params: {gs_gb.best_params_}')
 print()
print(f'GradienBoost GridSearch best score: {gs_gb.best_score_}')
 print(f'GradienBoost score: {GB.score(X_train, y_train)}' )
winPlacePerc_one_teams_crash=predict_one_team_matches(df_test_per_match_crash_1)
 winPlacePerc_two_teams_crash=predict_two_team_matches(df_test_per_match_crash_2 )<predict_on_test>
XGB = XGBClassifier()
xgb_param_grid = {'learning_rate':[0.05, 0.1],
'reg_lambda':[0.3, 0.5],
'gamma': [0.8, 1],
'subsample': [0.8, 1],
'max_depth': [2, 3],
'n_estimators': [200, 300]
}
xgb_param_grid_best = {'learning_rate':[0.1],
'reg_lambda':[0.3],
'gamma': [1],
'subsample': [0.8],
'max_depth': [2],
'n_estimators': [300]
}
gs_xgb = GridSearchCV(XGB, param_grid = xgb_param_grid_best, cv=kfold, scoring="roc_auc", n_jobs= 4, verbose = 1)
 gs_xgb.fit(X_train,y_train)
 XGB.fit(X_train, y_train)
 xgb_best = gs_xgb.best_estimator_
winPlacePerc_crash = predict_winPlacePerc(x_test_scaled_crash, df_test_groupids_crash, mlp_crash, verbose=False )<compute_test_metric>
print(f'XGB GridSearch best params: {gs_xgb.best_params_}')
 print()
print(f'XGB GridSearch best score: {gs_xgb.best_score_}')
 print(f'XGB score: {XGB.score(X_train, y_train)}' )
<concatenate><EOS> 
results=pd.DataFrame({'PassengerId':test['PassengerId'], 'Survived':knn1.predict(X_test)})
 results.to_csv("Titanic_prediction.csv", index=False)
 print('Done!' )