Kaggle competition: Titanic - Machine Learning from Disaster
y_predLogRegTrain = logreg.predict(X_test)
<predict_on_test>
sns.set(style="ticks", context="talk")
 warnings.filterwarnings('ignore' )
y_predLogReg = logreg.predict(test.posts)
<compute_test_metric>
testing = pd.read_csv('/kaggle/input/titanic/test.csv')
 train = pd.read_csv('/kaggle/input/titanic/train.csv')
 test = testing.copy()
target = 'Survived'
data = pd.concat([train, test], axis = 0)
 data.info()
data.head(5 )
print('accuracy %s' % accuracy_score(y_predLogRegTrain, y_test)) 
<predict_on_test>
data['Embarked'] = data['Embarked'].fillna(data['Embarked'].mode() [0] )
y_probslogreg = logreg.predict_proba(X_test)
 print("The log loss error for our model is: ", log_loss(y_test, y_probslogreg))<compute_test_metric>
data[data['Fare'].isna() ]
print(classification_report(y_test, y_predLogRegTrain))<feature_engineering>
data_corr = data.corr().abs().unstack().sort_values(kind = "quicksort", ascending = False ).reset_index()
data_corr.loc[data_corr['level_0'] == 'Fare']
DecisionTree = Pipeline([('vect', CountVectorizer(stop_words='english', ngram_range=(1, 2))), 
('tfidf', TfidfTransformer()), 
('clf', DecisionTreeClassifier())])
 clf = DecisionTree.fit(X_train, y_train)
<predict_on_test>
data['Fare'] = data['Fare'].fillna(data.groupby(['Pclass'])['Fare'].transform('median'))
y_predTree = clf.predict(X_test)
 print('accuracy %s' % accuracy_score(y_predTree, y_test)) 
<predict_on_test>
data.isna().sum()
y_probsTree = clf.predict_proba(X_test)
 print("The log loss error for our model is: ", log_loss(y_test, y_probsTree))<compute_test_metric>
data['Title'] = data.Name.apply(lambda x: re.search('([A-Z][a-z]+)\.', x ).group(1))
data['Title'].unique()
print(classification_report(y_test, y_predTree)) 
<predict_on_test>
data['Title'].value_counts(normalize = True ).round(3 )
clf = Pipeline([('vect', CountVectorizer(stop_words='english')) , 
('tfidf', TfidfTransformer()), 
('clf', SVC(kernel='polynomial')) ])
 clf.fit(X_train, y_train)
 y_predSVC = clf.predict(X_test)
 print('accuracy %s' % accuracy_score(y_predSVC, y_test)) 
<load_from_csv>
data['Age'].isna().sum()
sample = pd.read_csv('.. /input/random_example.csv' )<create_dataframe>
data[data['Age'] < 1]
submission = pd.DataFrame(data = sample['id'], columns= ['id'])
 submission['Type'] = y_predLogReg<data_type_conversions>
data.loc[data['Age'] < 1, 'Age'] = None
data['Age'].isna().sum()
submission['mind'] = submission['Type'].apply(lambda x: x[0] == 'E' ).astype('int')
 submission['energy'] = submission['Type'].apply(lambda x: x[1] == 'N' ).astype('int')
 submission['nature'] = submission['Type'].apply(lambda x: x[2] == 'T' ).astype('int')
 submission['tactics'] = submission['Type'].apply(lambda x: x[3] == 'J' ).astype('int')
 submission = submission.drop(['Type'],axis=1 )<save_to_csv>
data_corr = data.corr().abs().unstack().sort_values(kind = "quicksort", ascending = False ).reset_index()
data_corr.loc[data_corr['level_0'] == 'Age']
submission.to_csv('submit_2.csv', index=False )<set_options>
data['Age'] = data['Age'].fillna(data.groupby(['Pclass', 'Title'])['Age'].transform('median'))
data['Age'].isna().sum()
warnings.filterwarnings('ignore')
 class bold:
START = '\033[1m'
END = '\033[0m'
print('\t'+'\t'+bold.START+ 'What Is Your Myers Briggs Personality Type?' + bold.END)
 HTML("<iframe width='560' height='315' src='https://www.youtube.com/embed/M4YLO-2Tb2w/' frameborder='0' allowfullscreen></iframe>")
<set_options>
data_corr = data.corr().abs().unstack().sort_values(kind = "quicksort", ascending = False ).reset_index()
data_corr.loc[data_corr['level_0'] == 'Age']
warnings.filterwarnings('ignore' )<load_from_csv>
data[['Pclass', 'Embarked', 'Survived']].groupby(['Pclass', 'Embarked'] ).mean().round(2 )
df_train = pd.read_csv('.. /input/train.csv' )<categorify>
print('Pclass: ', data[data['Cabin_group'] == 'T']['Pclass'].values)
 data.loc[data['Cabin'] == 'T', 'Cabin_group'] = 'A'
df_train['Mind'] = df_train['type'].str[0]
df_train['Energy'] = df_train['type'].str[1]
df_train['Nature'] = df_train['type'].str[2]
df_train['Tactics'] = df_train['type'].str[3]
df_train = df_train.replace({'Mind' : {'I':0, 'E':1},
'Energy' : {'S':0, 'N':1},
'Nature' : {'F':0, 'T':1},
'Tactics' : {'P':0, 'J':1}})
 df_train = df_train.drop('type', axis = 1 )<load_from_csv>
data[['Cabin_group', 'Pclass', 'Survived']].groupby(['Cabin_group', 'Pclass'] ).mean().round(2 )
df_test = pd.read_csv('.. /input/test.csv')
 len_train = len(df_train)
 df_all = pd.concat([df_train, df_test], sort=False )<feature_engineering>
data['Sex_int'] = data['Sex'].replace({'male': 1, 'female': 0})
 data['Embarked_int'] = data['Embarked'].replace({'S': 0, 'C': 1, 'Q':2})
 data['Title_int'] = data['Title'].replace({'Mr': 0, 'Mrs': 1, 'Miss':2, 'Master':3, 'Special':4})
 data['age_group_int'] = data['age_group'].replace({'Adults': 0, 'Middle age': 1, 'Infants':2, 'Adolescents':3, 'Preschool':4, 'Children':5, 'Seniors':6})
 data['Cabin_group_int'] = data['Cabin_group'].replace({'unkown': 0, 'ABC': 1, 'DE':2, 'FG':3} )
tfv = TfidfVectorizer(stop_words='english',max_features=10000,lowercase=True,max_df=0.75,ngram_range=(1,2))
X = tfv.fit_transform(df_all['posts'] )<split>
data = data.loc[:,~data.columns.str.endswith('_int')]
train_X = X[:len_train]<prepare_x_and_y>
dummy_features = ['Sex'
, 'Pclass'
, 'Embarked'
, 'Cabin_group'
, 'Title'
, 'age_group'
]
for col in dummy_features:
data[col] = data[col].astype(object)
 drop_features = ['PassengerId', 'Ticket', 'Name', 'Cabin'
,'small_family_size'
,'Alone'
,'SibSp'
,'Parch'
,'Age'
]
data = pd.concat([data, pd.get_dummies(data[dummy_features], drop_first = True)], axis = 1, sort = False)
 data.drop(columns = data[dummy_features], inplace = True)
 data.drop(columns = data[drop_features], inplace = True)
 data.tail()
y_mind = df_train['Mind']
y_energy = df_train['Energy']
y_nature = df_train['Nature']
y_tactics = df_train['Tactics']<choose_model_class>
train.dropna(inplace = True)
 test.dropna(inplace = True )
lr = LogisticRegression(n_jobs = -1)
 rf = RandomForestClassifier(n_jobs=-1)
 adb = AdaBoostClassifier()
knn = KNeighborsClassifier(n_jobs=-1 )<compute_train_metric>
y = train['Survived']
x = train.drop(columns = target)
 x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.3, random_state = 42
, stratify = y
)
print('Logistic Regression
')
 scores = cross_val_score(lr, train_X, y_mind, cv=5)
 print("Accuracy(Mind): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(lr, train_X, y_energy, cv=5)
 print("Accuracy(Energy): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(lr, train_X, y_nature, cv=5)
 print("Accuracy(Nature): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(lr, train_X, y_tactics, cv=5)
 print("Accuracy(Tactics): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
print('-'*30)
 print('Random Forest
')
 scores = cross_val_score(rf, train_X, y_mind, cv=5)
 print("Accuracy(Mind): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(rf, train_X, y_energy, cv=5)
 print("Accuracy(Energy): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(rf, train_X, y_nature, cv=5)
 print("Accuracy(Nature): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(rf, train_X, y_tactics, cv=5)
 print("Accuracy(Tactics): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
print('-'*30)
 print('AdaBoost
')
 scores = cross_val_score(adb, train_X, y_mind, cv=5)
 print("Accuracy(Mind): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(adb, train_X, y_energy, cv=5)
 print("Accuracy(Energy): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(adb, train_X, y_nature, cv=5)
 print("Accuracy(Nature): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(adb, train_X, y_tactics, cv=5)
 print("Accuracy(Tactics): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
print('-'*30)
 print('KNN
')
 scores = cross_val_score(knn, train_X, y_mind, cv=5)
 print("Accuracy(Mind): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(knn, train_X, y_energy, cv=5)
 print("Accuracy(Energy): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(knn, train_X, y_nature, cv=5)
 print("Accuracy(Nature): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(knn, train_X, y_tactics, cv=5)
 print("Accuracy(Tactics): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))<compute_train_metric>
RF = ensemble.RandomForestClassifier()
params = {
'n_estimators':[n for n in range(100, 250, 50)]
,'max_depth':[n for n in range(3, 8)]
,'min_samples_leaf': [n for n in range(3, 6, 1)]
,'max_features' : [None]
,'random_state' : [42]
}
RF_model = GridSearchCV(RF, param_grid = params, cv = 5, n_jobs = -1 ).fit(x_train, y_train)
 print("Best Hyper Parameters:",RF_model.best_params_)
 RF_probs = RF_model.predict_proba(x_test)
 RF_probs = RF_probs[:, 1]
RF_auc = roc_auc_score(y_test, RF_probs)
 print('AUC: %.3f' % RF_auc)
 RF_predictions = RF_model.predict(x_test ).astype(int)
 RF_accuracy = accuracy_score(y_test, RF_predictions)
 print("RF accuracy: %.3f" % RF_accuracy)
 print("RF Recall: " + '%.3f' % recall_score(y_test, RF_predictions))
print("RF Precission: " + '%.3f' % precision_score(y_test, RF_predictions))
print("RF cohen_kappa_score: %.3f" % cohen_kappa_score(y_test, RF_predictions))
plt.figure(figsize =(8, 6))
RF_fpr, RF_tpr, RF_thresholds = roc_curve(y_test, RF_probs)
 plt.plot([0, 1], [0, 1], linestyle = '--')
 plt.plot(RF_fpr, RF_tpr, color = 'tab:green')
 plt.show()
print('Logistic Regression
')
 scores = cross_val_score(lr, train_X, y_mind, cv=5, scoring='f1_macro')
 print("F1(Mind): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(lr, train_X, y_energy, cv=5, scoring='f1_macro')
 print("F1(Energy): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(lr, train_X, y_nature, cv=5, scoring='f1_macro')
 print("F1(Nature): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(lr, train_X, y_tactics, cv=5, scoring='f1_macro')
 print("F1(Tactics): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
print('-'*30)
 print('Random Forest
')
 scores = cross_val_score(rf, train_X, y_mind, cv=5, scoring='f1_macro')
 print("F1(Mind): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(rf, train_X, y_energy, cv=5, scoring='f1_macro')
 print("F1(Energy): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(rf, train_X, y_nature, cv=5, scoring='f1_macro')
 print("F1(Nature): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(rf, train_X, y_tactics, cv=5, scoring='f1_macro')
 print("F1(Tactics): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
print('-'*30)
 print('AdaBoost
')
 scores = cross_val_score(adb, train_X, y_mind, cv=5, scoring='f1_macro')
 print("F1(Mind): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(adb, train_X, y_energy, cv=5, scoring='f1_macro')
 print("F1(Energy): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(adb, train_X, y_nature, cv=5, scoring='f1_macro')
 print("F1(Nature): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(adb, train_X, y_tactics, cv=5, scoring='f1_macro')
 print("F1(Tactics): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
print('-'*30)
 print('KNN
')
 scores = cross_val_score(knn, train_X, y_mind, cv=5, scoring='f1_macro')
 print("F1(Mind): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(knn, train_X, y_energy, cv=5, scoring='f1_macro')
 print("F1(Energy): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(knn, train_X, y_nature, cv=5, scoring='f1_macro')
 print("F1(Nature): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(knn, train_X, y_tactics, cv=5, scoring='f1_macro')
 print("F1(Tactics): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))<compute_train_metric>
data['churn_proba'] = RF_model.best_estimator_.predict_proba(data[x.columns])[:,1]
print('Logistic Regression
')
 scores = cross_val_score(lr, train_X, y_mind, cv=5, scoring='neg_log_loss')
 print("Log Loss(Mind): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(lr, train_X, y_energy, cv=5, scoring='neg_log_loss')
 print("Log Loss(Energy): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(lr, train_X, y_nature, cv=5, scoring='neg_log_loss')
 print("Log Loss(Nature): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(lr, train_X, y_tactics, cv=5, scoring='neg_log_loss')
 print("Log Loss(Tactics): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
print('-'*30)
 print('Random Forest
')
 scores = cross_val_score(rf, train_X, y_mind, cv=5, scoring='neg_log_loss')
 print("Log Loss(Mind): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(rf, train_X, y_energy, cv=5, scoring='neg_log_loss')
 print("Log Loss(Energy): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(rf, train_X, y_nature, cv=5, scoring='neg_log_loss')
 print("Log Loss(Nature): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(rf, train_X, y_tactics, cv=5, scoring='neg_log_loss')
 print("Log Loss(Tactics): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
print('-'*30)
 print('AdaBoost
')
 scores = cross_val_score(adb, train_X, y_mind, cv=5, scoring='neg_log_loss')
 print("Log Loss(Mind): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(adb, train_X, y_energy, cv=5, scoring='neg_log_loss')
 print("Log Loss(Energy): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(adb, train_X, y_nature, cv=5, scoring='neg_log_loss')
 print("Log Loss(Nature): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(adb, train_X, y_tactics, cv=5, scoring='neg_log_loss')
 print("Log Loss(Tactics): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
print('-'*30)
 print('KNN
')
 scores = cross_val_score(knn, train_X, y_mind, cv=5, scoring='neg_log_loss')
 print("Log Loss(Mind): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(knn, train_X, y_energy, cv=5, scoring='neg_log_loss')
 print("Log Loss(Energy): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(knn, train_X, y_nature, cv=5, scoring='neg_log_loss')
 print("Log Loss(Nature): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(knn, train_X, y_tactics, cv=5, scoring='neg_log_loss')
 print("Log Loss(Tactics): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))<compute_train_metric>
predict_RF = RF_model.predict(test ).astype(int)
 submit_RF = pd.DataFrame({'PassengerId': testing['PassengerId'],
'Survived': predict_RF})
 filename_RF = 'Titanic Prediction RF.csv'
submit_RF.to_csv(filename_RF,index=False)
 print('Saved file: ' + filename_RF )