Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<sort_values>
import pickle
import warnings
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import xgboost as xgb
from sklearn.metrics import confusion_matrix
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split, KFold, GridSearchCV
for dataset in combine:
dataset['Embarked'] = dataset['Embarked'].fillna(freq_port)
 df[['Embarked', 'Survived']].groupby(['Embarked'], as_index=False ).mean().sort_values(by='Survived', ascending=False )<categorify>
training_set = pd.read_csv("/kaggle/input/titanic/train.csv")
 test_set = pd.read_csv("/kaggle/input/titanic/test.csv" )
for dataset in combine:
dataset["Sex"] = dataset["Sex"].map({'male': 1,'female':0})
 dataset["Embarked"] = dataset["Embarked"].map({'S': 1,'C':2,'Q':3})
 emb = pd.get_dummies(df.Embarked)
 df = df.join(emb)
 df = df.rename(columns={1.0: "S", 2.0: "C", 3.0 : "Q"})
 emb = pd.get_dummies(test_df.Embarked)
 test_df = test_df.join(emb)
 test_df = test_df.rename(columns={1.0: "S", 2.0: "C", 3.0 : "Q"})
 df = df.drop(["Name","Ticket","Embarked","PassengerId"],axis = 1)
 test_df = test_df.drop(["Name","Ticket","Embarked"],axis = 1)
 combine = [df, test_df]<train_model>
sns.set(style="whitegrid")
 pd.options.display.max_rows = 100
pd.options.display.max_columns = 100
warnings.filterwarnings('ignore' )
def status(feature):
print('Processing', feature, ': ok')
<categorify>
def my_xgb(data, target, params):
X = data.drop(target, axis=1)
 y = data[target]
scaler = StandardScaler()
scaler.fit(X)
 X_scaled = scaler.transform(X)
 xgb_clf = xgb.XGBClassifier()
kf = KFold(n_splits=10, random_state=42, shuffle=True)
 gridsearch = GridSearchCV(xgb_clf, param_grid=params, scoring="accuracy", cv=kf, return_train_score=True)
 gridsearch.fit(X_scaled, y)
 return gridsearch, scaler
def process_cabin(combined):
combined.Cabin.fillna('U', inplace=True)
 combined['Cabin'] = combined['Cabin'].map(lambda c: c[0])
 cabin_dummies = pd.get_dummies(combined['Cabin'], prefix='Cabin')
 combined = pd.concat([combined, cabin_dummies], axis=1)
 combined.drop('Cabin', axis=1, inplace=True)
 status('cabin')
 return combined<define_variables>
nanbyfeature = pd.DataFrame(training_set.isna().sum() ).sort_values(by=0, ascending=False)
 nanbyfeature["percent"] = np.round(nanbyfeature[0] / len(training_set)* 100,2)
 nanbyfeature
combine = [df,test_df]<drop_column>
def name_title(title):

 for word in title.split(" ")[1:-1]:
if(word in ["Mme.", "Ms.", "Mrs."]):
return "woman"
elif(word in ["Mr."]):
return "man"
elif(word in ["Master."]):
return "boy"
elif(word in ["Miss.", "Mlle."]):
return "miss"
elif(word in ["Capt.", "Col.", "Major.", "Rev.", "Dr."]):
return "army"
elif(word in ["Jonkheer.", "Don.", "Sir.", "Countess.", "Dona.", "Lady."]):
return "gentry"
else:
return "other"
training_set["title"] = training_set["Name"].apply(name_title)
 cat_analysis(training_set, "title", "title", "total_people", rotation=90, figsize=(16,5))
fig, ax = plt.subplots(figsize=(15,5))
ax = sns.barplot(x="title", y="Survived", data=training_set);
plt.xticks(rotation=0)
 plt.title("Percentage of survived by title")
 plt.show()
training_set.drop("title", axis=1, inplace=True )
df = df.drop('Cabin_T', axis=1)
 df.head()<define_variables>
train_prep = prepare_data(training_set)
 test_prep = prepare_data(test_set )
combine = [df,test_df]<find_best_params>
data_wcg_name = train_prep.copy() [["Sex_female", "boy", "wcg_surname", "Survived"]]
data_wcg_name["wcg_surname_size"] = 1
mask = data_wcg_name["wcg_surname"] != "no_group"
data_wcg_name = data_wcg_name[mask]
columns = {'Survived': 'survived_number'}
data_wcg_name = data_wcg_name.groupby("wcg_surname" ).agg({'Survived':'sum', 'wcg_surname_size':'count'} ).reset_index().rename(columns=columns)
 data_wcg_name["wcg_name_all_died"] = data_wcg_name["survived_number"].apply(lambda x: 1 if x == 0 else 0)
 data_wcg_name["wcg_name_all_survived"] = data_wcg_name["survived_number"] == data_wcg_name["wcg_surname_size"]
data_wcg_name["wcg_name_all_survived"] = data_wcg_name["wcg_name_all_survived"] * 1
wcg_name_test = test_prep.copy() [["wcg_surname"]]
wcg_name_test["wcg_surname_size"] = 1
wcg_name_test["survived_number"] = 0
wcg_name_test["wcg_name_all_died"] = 0
wcg_name_test["wcg_name_all_survived"] = 0
mask = wcg_name_test["wcg_surname"] != "no_group"
wcg_name_test = wcg_name_test[mask]
data_wcg_name = pd.concat([data_wcg_name, wcg_name_test])
 data_wcg_name = data_wcg_name.groupby("wcg_surname" ).sum().reset_index()
mask = data_wcg_name["wcg_surname_size"] > 1
data_wcg_name = data_wcg_name[mask]
data_wcg_name.drop(["survived_number", "wcg_surname_size"], axis=1, inplace=True)
 train_prep = train_prep.merge(data_wcg_name, how="left", on="wcg_surname" )
guess_ages = np.zeros(( 2,3))
guess_ages
for dataset in combine:
for i in range(0, 2):
for j in range(0, 3):
guess_df = dataset[(dataset['Sex'] == i)& \ 
(dataset['Pclass'] == j+1)]['Age'].dropna()
age_guess = guess_df.median()
guess_ages[i,j] = int(age_guess/0.5 + 0.5)* 0.5
for i in range(0, 2):
for j in range(0, 3):
dataset.loc[(dataset.Age.isnull())&(dataset.Sex == i)&(dataset.Pclass == j+1),\
'Age'] = guess_ages[i,j]
dataset['Age'] = dataset['Age'].astype(int)
 df.head()<feature_engineering>
data_wcg_ticket = train_prep.copy() [["Sex_female", "boy", "wcg_ticket", "Survived"]]
data_wcg_ticket["wcg_ticket_size"] = 1
mask = data_wcg_ticket["wcg_ticket"] != "no_group"
data_wcg_ticket = data_wcg_ticket[mask]
columns = {'Survived': 'survived_number'}
data_wcg_ticket = data_wcg_ticket.groupby("wcg_ticket" ).agg({'Survived':'sum', 'wcg_ticket_size':'count'} ).reset_index().rename(columns=columns)
 data_wcg_ticket["wcg_ticket_all_died"] = data_wcg_ticket["survived_number"].apply(lambda x: 1 if x == 0 else 0)
 data_wcg_ticket["wcg_ticket_all_survived"] = data_wcg_ticket["survived_number"] == data_wcg_ticket["wcg_ticket_size"]
data_wcg_ticket["wcg_ticket_all_survived"] = data_wcg_ticket["wcg_ticket_all_survived"] * 1
wcg_ticket_test = test_prep.copy() [["wcg_ticket"]]
wcg_ticket_test["wcg_ticket_size"] = 1
wcg_ticket_test["survived_number"] = 0
wcg_ticket_test["wcg_ticket_all_died"] = 0
wcg_ticket_test["wcg_ticket_all_survived"] = 0
mask = wcg_ticket_test["wcg_ticket"] != "no_group"
wcg_ticket_test = wcg_ticket_test[mask]
data_wcg_ticket = pd.concat([data_wcg_ticket, wcg_ticket_test])
 data_wcg_ticket = data_wcg_ticket.groupby("wcg_ticket" ).sum().reset_index()
mask = data_wcg_ticket["wcg_ticket_size"] > 1
data_wcg_ticket = data_wcg_ticket[mask]
data_wcg_ticket.drop(["survived_number", "wcg_ticket_size"], axis=1, inplace=True)
 train_prep = train_prep.merge(data_wcg_ticket, how="left", on="wcg_ticket" )
for dataset in combine:
dataset.loc[ dataset['Age'] <= 16, 'Age'] = 0
dataset.loc[(dataset['Age'] > 16)&(dataset['Age'] <= 32), 'Age'] = 1
dataset.loc[(dataset['Age'] > 32)&(dataset['Age'] <= 48), 'Age'] = 2
dataset.loc[(dataset['Age'] > 48)&(dataset['Age'] <= 64), 'Age'] = 3
dataset.loc[ dataset['Age'] > 64, 'Age']
df.head()<feature_engineering>
for i, row in train_prep.iterrows() :
if(( train_prep.loc[i, "wcg_name_all_died"] == 1)or(train_prep.loc[i, "wcg_ticket_all_died"] == 1)) :
train_prep.loc[i, "all_died"] = 1
elif(( train_prep.loc[i, "wcg_name_all_died"] == 0)or(train_prep.loc[i, "wcg_ticket_all_died"] == 0)) :
train_prep.loc[i, "all_died"] = 0
else:
train_prep.loc[i, "all_died"] = np.nan
if(( train_prep.loc[i, "wcg_name_all_survived"] == 1)or(train_prep.loc[i, "wcg_ticket_all_survived"] == 1)) :
train_prep.loc[i, "all_survived"] = 1
elif(( train_prep.loc[i, "wcg_name_all_survived"] == 0)or(train_prep.loc[i, "wcg_ticket_all_survived"] == 0)) :
train_prep.loc[i, "all_survived"] = 0
else:
train_prep.loc[i, "all_survived"] = np.nan
for dataset in combine:
dataset['Age*Class'] = dataset.Age * dataset.Pclass<sort_values>
train_prep.drop(["wcg_surname", "wcg_ticket", "wcg_name_all_died", "wcg_name_all_survived", "wcg_ticket_all_died", "wcg_ticket_all_survived"], axis=1, inplace=True )
for dataset in combine:
dataset['FamilySize'] = dataset['SibSp'] + dataset['Parch'] + 1
df[['FamilySize', 'Survived']].groupby(['FamilySize'], as_index=False ).mean().sort_values(by='Survived', ascending=False )<feature_engineering>
nanbyfeature2 = pd.DataFrame(train_prep.isna().sum() ).sort_values(by=0, ascending=False)
 nanbyfeature2["percent"] = np.round(nanbyfeature2[0] / len(train_prep)* 100,2)
 nanbyfeature2
for dataset in combine:
dataset['IsAlone'] = 0
dataset.loc[dataset['FamilySize'] == 1, 'IsAlone'] = 1
df[['IsAlone', 'Survived']].groupby(['IsAlone'], as_index=False ).mean()<drop_column>
params = {
'learning_rate': [0.01, 0.05, 0.1],
'subsample': [1],
'colsample_bylevel': [1],
'colsample_bynode': [1],
'colsample_bytree': [0.5],
'gamma': [0, 1, 2],
'max_delta_step': [0],
'max_depth': [2, 3],
'min_child_weight': [1.6],
'n_estimators': [100],
'random_state': [42],
'scale_pos_weight': [1],
'seed': [42],
'n_jobs': [-1],
'reg_lambda': [1, 2, 4, 16]
}
df = df.drop(['Parch', 'SibSp', 'FamilySize'], axis=1)
 test_df = test_df.drop(['Parch', 'SibSp', 'FamilySize'], axis=1)
 combine = [df, test_df]
df.head()<data_type_conversions>
gridsearch, scaler = my_xgb(train_prep, "Survived", params )
test_df['Fare'].fillna(test_df['Fare'].dropna().median() , inplace=True )<count_missing_values>
display(gridsearch.best_params_ )
test_df.isnull().sum()<sort_values>
X_training_error = train_prep.copy()
y_training_error_true = X_training_error.pop("Survived")
 X_training_error_scaled = scaler.transform(X_training_error)
 y_training_error_pred = gridsearch.predict(X_training_error_scaled)
 training_set["true"] = y_training_error_true
training_set["pred"] = y_training_error_pred
true_pos = training_set["true"] == 1
true_false = training_set["true"] == 0
pred_pos = training_set["pred"] == 1
pred_false = training_set["pred"] == 0
df['FareBand'] = pd.qcut(df['Fare'], 4)
 df[['FareBand', 'Survived']].groupby(['FareBand'], as_index=False ).mean().sort_values(by='FareBand', ascending=True )<data_type_conversions>
mask_results = training_set["Sex"] != -1
true_positive = len(training_set[true_pos & pred_pos & mask_results])
 false_positive = len(training_set[true_false & pred_pos & mask_results])
 true_negative = len(training_set[true_false & pred_false & mask_results])
 false_negative = len(training_set[true_pos & pred_false & mask_results])
 print(f"Accuracy: {(training_set['true'] == training_set['pred'] ).sum() / len(training_set):.2%}")
 print(f"True positives: {true_positive}({true_positive /(true_positive + false_negative):.0%})")
 print(f"False positives: {false_positive}")
 print(f"True negatives: {true_negative}({true_negative /(true_negative + false_positive):.0%})")
 print(f"False negatives: {false_negative}" )
for dataset in combine:
dataset.loc[ dataset['Fare'] <= 7.91, 'Fare'] = 0
dataset.loc[(dataset['Fare'] > 7.91)&(dataset['Fare'] <= 14.454), 'Fare'] = 1
dataset.loc[(dataset['Fare'] > 14.454)&(dataset['Fare'] <= 31), 'Fare'] = 2
dataset.loc[ dataset['Fare'] > 31, 'Fare'] = 3
dataset['Fare'] = dataset['Fare'].astype(int)
 df = df.drop(['FareBand'], axis=1)
 combine = [df, test_df]
df.head(10 )<prepare_x_and_y>
mask_results = training_set["Sex"] == "female"
true_positive = len(training_set[true_pos & pred_pos & mask_results])
 false_positive = len(training_set[true_false & pred_pos & mask_results])
 true_negative = len(training_set[true_false & pred_false & mask_results])
 false_negative = len(training_set[true_pos & pred_false & mask_results])
 print(f"Accuracy: {(training_set['true'] == training_set['pred'] ).sum() / len(training_set):.2%}")
 print(f"True positives: {true_positive}({true_positive /(true_positive + false_negative):.0%})")
 print(f"False positives: {false_positive}")
 print(f"True negatives: {true_negative}({true_negative /(true_negative + false_positive):.0%})")
 print(f"False negatives: {false_negative}" )
X_train = df.drop("Survived", axis=1)
 Y_train = df["Survived"]
X_test = test_df.drop("PassengerId", axis=1 ).copy()
X_train.shape, Y_train.shape, X_test.shape<choose_model_class>
mask_results = training_set["Sex"] == "male"
true_positive = len(training_set[true_pos & pred_pos & mask_results])
 false_positive = len(training_set[true_false & pred_pos & mask_results])
 true_negative = len(training_set[true_false & pred_false & mask_results])
 false_negative = len(training_set[true_pos & pred_false & mask_results])
 print(f"Accuracy: {(training_set['true'] == training_set['pred'] ).sum() / len(training_set):.2%}")
 print(f"True positives: {true_positive}({true_positive /(true_positive + false_negative):.0%})")
 print(f"False positives: {false_positive}")
 print(f"True negatives: {true_negative}({true_negative /(true_negative + false_positive):.0%})")
 print(f"False negatives: {false_negative}" )
logreg = LogisticRegression()
logreg.fit(X_train, Y_train)
 Y_pred = logreg.predict(X_test)
 acc_log = round(logreg.score(X_train, Y_train)* 100, 2)
 acc_log<predict_on_test>
mask_results = training_set["Sex"] == "male"
mask_results2 = training_set["Name"].str.contains("Master.")
 true_positive = len(training_set[true_pos & pred_pos & mask_results & ~mask_results2])
 false_positive = len(training_set[true_false & pred_pos & mask_results & ~mask_results2])
 true_negative = len(training_set[true_false & pred_false & mask_results & ~mask_results2])
 false_negative = len(training_set[true_pos & pred_false & mask_results & ~mask_results2])
 print(f"Accuracy: {(training_set['true'] == training_set['pred'] ).sum() / len(training_set):.2%}")
 print(f"True positives: {true_positive}({true_positive /(true_positive + false_negative):.0%})")
 print(f"False positives: {false_positive}")
 print(f"True negatives: {true_negative}({true_negative /(true_negative + false_positive):.0%})")
 print(f"False negatives: {false_negative}" )
knn = KNeighborsClassifier(n_neighbors = 3)
 knn.fit(X_train, Y_train)
 Y_pred = knn.predict(X_test)
 acc_knn = round(knn.score(X_train, Y_train)* 100, 2)
 acc_knn<predict_on_test>
test_data = pd.read_csv("/kaggle/input/titanic/test.csv" )
gaussian = GaussianNB()
gaussian.fit(X_train, Y_train)
 Y_pred = gaussian.predict(X_test)
 acc_gaussian = round(gaussian.score(X_train, Y_train)* 100, 2)
 acc_gaussian<predict_on_test>
test_data_prepared = prepare_data(test_data)
 test_data_prepared = test_data_prepared.merge(data_wcg_name, how="left", on="wcg_surname")
 test_data_prepared["wcg_name_all_died"]
test_data_prepared["wcg_name_all_survived"]
test_data_prepared = test_data_prepared.merge(data_wcg_ticket, how="left", on="wcg_ticket")
 test_data_prepared["wcg_ticket_all_died"]
test_data_prepared["wcg_ticket_all_survived"]
for i, row in test_data_prepared.iterrows() :
if(( test_data_prepared.loc[i, "wcg_name_all_died"] == 1)or(test_data_prepared.loc[i, "wcg_ticket_all_died"] == 1)) :
test_data_prepared.loc[i, "all_died"] = 1
elif(( test_data_prepared.loc[i, "wcg_name_all_died"] == 0)or(test_data_prepared.loc[i, "wcg_ticket_all_died"] == 0)) :
test_data_prepared.loc[i, "all_died"] = 0
else:
test_data_prepared.loc[i, "all_died"] = np.nan
if(( test_data_prepared.loc[i, "wcg_name_all_survived"] == 1)or(test_data_prepared.loc[i, "wcg_ticket_all_survived"] == 1)) :
test_data_prepared.loc[i, "all_survived"] = 1
elif(( test_data_prepared.loc[i, "wcg_name_all_survived"] == 0)or(test_data_prepared.loc[i, "wcg_ticket_all_survived"] == 0)) :
test_data_prepared.loc[i, "all_survived"] = 0
else:
test_data_prepared.loc[i, "all_survived"] = np.nan
test_data_prepared.drop(["wcg_surname", "wcg_ticket", "wcg_name_all_died", "wcg_name_all_survived", "wcg_ticket_all_died", "wcg_ticket_all_survived"], axis=1, inplace=True )
gaussian = GaussianNB()
gaussian.fit(X_train, Y_train)
 Y_pred = gaussian.predict(X_test)
 acc_gaussian = round(gaussian.score(X_train, Y_train)* 100, 2)
 acc_gaussian<compute_train_metric>
test_data_prepared_scaled = scaler.transform(test_data_prepared)
 y_test = gridsearch.predict(test_data_prepared_scaled )
perceptron = Perceptron()
perceptron.fit(X_train, Y_train)
 Y_pred = perceptron.predict(X_test)
 acc_perceptron = round(perceptron.score(X_train, Y_train)* 100, 2)
 acc_perceptron<compute_train_metric>
submission = pd.DataFrame(y_test.copy())
 submission['PassengerId'] = pd.read_csv("/kaggle/input/titanic/test.csv")['PassengerId'].copy()
submission.rename(columns={0:'Survived'}, inplace=True)
 submission = submission[['PassengerId', 'Survived']]
submission.to_csv("submission.csv", index=False )