text_html_output_1.png
from lightgbm import LGBMClassifier

from sklearn import metrics

from sklearn import svm

from sklearn.ensemble import AdaBoostClassifier

from sklearn.ensemble import RandomForestClassifier, VotingClassifier

from sklearn.linear_model import LogisticRegression

from sklearn.naive_bayes import GaussianNB

from sklearn.neighbors import KNeighborsClassifier

from sklearn.tree import DecisionTreeClassifier

from xgboost import XGBClassifier

import matplotlib.pyplot as plt

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

train = pd.read_csv('/kaggle/input/titanic/train.csv')
test = pd.read_csv('/kaggle/input/titanic/test.csv')
submission_df = pd.read_csv('/kaggle/input/titanic/gender_submission.csv')

data = pd.concat([train.assign(ind='train'), test.assign(ind='test')], ignore_index=True)

data['Age'] = data.groupby(['Sex', 'Pclass'])['Age'].apply(lambda x: x.fillna(x.median()))
data['Age'] = data['Age'].astype(int)
data.loc[data['Age'] <= 15, 'Age'] = 0
data.loc[(data['Age'] > 15) & (data['Age'] <= 30), 'Age'] = 1
data.loc[(data['Age'] > 30) & (data['Age'] <= 45), 'Age'] = 2
data.loc[(data['Age'] > 45) & (data['Age'] <= 60), 'Age'] = 3
data.loc[data['Age'] > 60, 'Age'] = 4

data['Fare_bins'] = pd.cut(data['Fare'], bins=[0.0, 7.895, 14.45, 31.275, 512.329], labels=[0, 1, 2, 3])
data['Fare_bins'] = data['Fare_bins'].fillna(0)
data['Fare_bins'] = data['Fare_bins'].astype(int)

test_data = test_data.drop(['ind'], axis=1)
train_data = train_data.drop(['ind'], axis=1)

X = train_data.drop(['Survived'], axis=1)
y = train_data['Survived']

traindf_X = pd.get_dummies(X, columns=['Sex', 'Embarked', 'Fare_bins', 'Deck'], prefix=['Sex', 'Embarked', 'Fare_type', 'Deck'])
testdf = pd.get_dummies(test_data, columns=['Sex', 'Embarked', 'Fare_bins', 'Deck'], prefix=['Sex', 'Embarked', 'Fare_type', 'Deck']).drop(['Survived'], axis=1)

from sklearn import metrics
def get_scores(y_preds, y):
    return {'Accuracy': metrics.accuracy_score(y_preds, y), 'Precision': metrics.precision_score(y_preds, y), 'Recall': metrics.recall_score(y_preds, y), 'F1': metrics.f1_score(y_preds, y), 'ROC_AUC': metrics.roc_auc_score(y_preds, y)}

def train_model(model):
    model_ = model
    model_.fit(X_train, y_train)
    y_pred = model_.predict(X_val)
    return get_scores(y_pred, y_val)

model_list = [DecisionTreeClassifier(random_state=42), RandomForestClassifier(random_state=42), XGBClassifier(random_state=42), LGBMClassifier(random_state=42, is_unbalance=True), LogisticRegression(random_state=42), svm.SVC(random_state=42), AdaBoostClassifier(random_state=42), KNeighborsClassifier(), GaussianNB()]
model_names = ['Decision Tree', 'Random Forest', 'XGB Classifier', 'LGBM Classifier', 'Logistic Regression', 'SVC', 'AdaBoost ', 'KNN', 'GaussianNB']

scores = pd.DataFrame(columns=['Name', 'Accuracy', 'Precision', 'Recall', 'F1', 'ROC_AUC'])
for i in range(len(model_list)):
    score = train_model(model_list[i])
    scores.loc[i] = [model_names[i]] + list(score.values())

figure, axis = plt.subplots(2, 3)
figure.set_figheight(15)
figure.set_figwidth(30)
for i in range(2):
    for j in range(3):
        axis[i, j].set_xlim([0.5, 0.9])
axis[0, 0].barh(scores['Name'], scores['Accuracy'], height=0.5)
axis[0, 0].set_title('Accuracy Score')
axis[0, 1].barh(scores['Name'], scores['Precision'], height=0.5)
axis[0, 1].set_title('Precision')
axis[1, 0].barh(scores['Name'], scores['Recall'], height=0.5)
axis[1, 0].set_title('Recall')
axis[1, 2].barh(scores['Name'], scores['F1'], height=0.5)
axis[1, 2].set_title('F1')
axis[0, 2].barh(scores['Name'], scores['ROC_AUC'], height=0.5)
axis[0, 2].set_title('ROC_AUC')
axis[1, 1].set_visible(False)
plt.show()