text_plain_output_1.png
from sklearn.ensemble import RandomForestClassifier

from sklearn.linear_model import LogisticRegression

from sklearn.metrics import confusion_matrix, accuracy_score

from sklearn.model_selection import train_test_split, cross_val_score, GridSearchCV

from sklearn.naive_bayes import GaussianNB

from sklearn.neighbors import KNeighborsClassifier

from sklearn.preprocessing import OneHotEncoder, StandardScaler

from sklearn.svm import SVC

import matplotlib.pyplot as plt

import numpy as np

import numpy as np # linear algebra

import pandas as pd

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

import seaborn as sns

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
from sklearn.preprocessing import OneHotEncoder, StandardScaler
from sklearn.model_selection import train_test_split, cross_val_score, GridSearchCV
from sklearn.metrics import confusion_matrix, accuracy_score
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.naive_bayes import GaussianNB

data_train = pd.read_csv('/kaggle/input/titanic/train.csv')
data_test = pd.read_csv('/kaggle/input/titanic/test.csv')
data_total = data_train.append(data_test, ignore_index=True)
data_train_length = len(data_train.index)
for c in ['Age', 'Fare']:
    g = sns.FacetGrid(data_train, col='Survived')
    g.map(plt.hist, c)
corr = data_train.corr()

age_mean_by_pclass = data_total[['Pclass', 'Age']].groupby('Pclass').mean()
data_total['Age'] = data_total.apply(lambda row: age_mean_by_pclass.loc[row['Pclass'], 'Age'] if np.isnan(row['Age']) else row['Age'], axis=1)
fare_median_by_pclass = data_total[['Pclass', 'Fare']].groupby('Pclass').median()
data_total['Fare'] = data_total.apply(lambda row: fare_median_by_pclass.loc[row['Pclass'], 'Fare'] if np.isnan(row['Fare']) else row['Fare'], axis=1)
data_total['Embarked'].fillna(data_total['Embarked'].mode()[0], inplace=True)

data_total['isAlone'] = (data_total['SibSp'] + data_total['Parch'] == 0) * 1
data_total['bigGroup'] = (data_total['SibSp'] + data_total['Parch'] > 3) * 1
data_total['Title'] = data_total['Name'].str.extract(' ([A-Za-z]+)\\.', expand=False)
data_total['Title'] = data_total['Title'].replace(['Lady', 'Countess', 'Capt', 'Col', 'Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Other')
data_total['Title'] = data_total['Title'].replace(['Mlle', 'Ms', 'Mme'], 'Miss')
age_bin_count = 10
data_total['Age_Bin'] = pd.cut(data_total['Age'], age_bin_count, labels=list(range(1, age_bin_count + 1)))
fare_bin_count = 5
data_total['Fare_Bin'] = pd.qcut(data_total['Fare'], fare_bin_count, labels=list(range(1, fare_bin_count + 1)))
enc = OneHotEncoder().fit(data_total[['Sex', 'Embarked', 'Title']])
enc_features_arr = enc.transform(data_total[['Sex', 'Embarked', 'Title']]).toarray()
enc_features_labels = [label for cat in enc.categories_ for label in cat]
enc_features_df = pd.DataFrame(enc_features_arr, columns=enc_features_labels).drop(columns=['male', 'S', 'Other'])
data_total = data_total.join(enc_features_df)
data_total = data_total.drop(columns=['PassengerId', 'Name', 'Cabin', 'Ticket', 'SibSp', 'Parch', 'Age', 'Fare', 'Sex', 'Embarked', 'Title'])

data_train = data_total[:data_train_length]
X = data_train.drop(columns=['Survived'], axis=1)
y = data_train['Survived']
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
scaler = StandardScaler().fit(X_train)
X_train = scaler.transform(X_train)
X_test = scaler.transform(X_test)

"""#first round of grid search
svc_params = [{'C': [1,2,3,4,5], 'kernel': ['linear', 'rbf', 'sigmoid'], 'gamma': [.1, .2, .3, .4, .5, .6, .7, .8, .9]},
              {'C': [1,2,3,4,5], 'kernel': ['linear']}]
#output: {'C': 1, 'gamma': 0.2, 'kernel': 'rbf'}, 0.8264360018091361
knn_params = [{'n_neighbors': [3,4,5,6,7,8], 'weights': ['uniform', 'distance'], 'algorithm': ['auto', 'ball_tree', 'kd_tree', 'brute'],
               'leaf_size': [25, 30, 35, 40, 50], 'p': [1, 2]}]
#output: {'algorithm': 'brute', 'leaf_size': 25, 'n_neighbors': 8, 'p': 2, 'weights': 'uniform'}, 0.8294663048394391
rfc_params = [{'n_estimators': [20, 50, 100, 150, 200], 'criterion': ['gini', 'entropy']}]
#output: {'criterion': 'entropy', 'n_estimators': 100}, 0.7979873360470375
"""
svc_params = [{'C': [0.2, 0.4, 0.5, 1, 2, 3, 4, 5], 'kernel': ['rbf'], 'gamma': [0.1, 0.15, 0.2, 0.25, 0.3]}]
knn_params = [{'n_neighbors': [7, 8, 9, 10], 'weights': ['uniform'], 'algorithm': ['brute'], 'p': [2]}]
rfc_params = [{'n_estimators': [80, 90, 100, 110, 120, 130, 140], 'criterion': ['entropy']}]
classifier_names = ['SVC', 'KNN', 'RFC']
params = [svc_params, knn_params, rfc_params]
classifier_objs = [SVC(), KNeighborsClassifier(), RandomForestClassifier()]
best_params = {}
for i in range(len(classifier_names)):
    classifier_objs[i].fit(X_train, y_train)
    grid_search = GridSearchCV(estimator=classifier_objs[i], param_grid=params[i], scoring='accuracy', cv=10).fit(X_train, y_train)
    best_params[classifier_names[i]] = grid_search.best_params_

classifier_tuned = [SVC(C=best_params['SVC']['C'], gamma=best_params['SVC']['gamma'], kernel=best_params['SVC']['kernel']), KNeighborsClassifier(algorithm=best_params['KNN']['algorithm'], n_neighbors=best_params['KNN']['n_neighbors'], p=best_params['KNN']['p'], weights=best_params['KNN']['weights']), LogisticRegression(), RandomForestClassifier(criterion=best_params['RFC']['criterion'], n_estimators=best_params['RFC']['n_estimators']), GaussianNB()]
predictions_train = []
for clf in classifier_tuned:
    clf.fit(X_train, y_train)
    y_pred = clf.predict(X_test)
    predictions_train.append(y_pred)
    print(clf)
    print(confusion_matrix(y_test, y_pred))
    print('accuracy score: ' + str(accuracy_score(y_test, y_pred)))
    print('cross val score: ' + str(cross_val_score(clf, X_train, y_train).mean()))
    print('–––––––––')
predictions_train = pd.DataFrame(predictions_train).transpose()
predictions_train['y_pred_voted'] = predictions_train.mean(axis=1).round()
predictions_train_voted = predictions_train['y_pred_voted'].values
print('Result after voting:')
print(confusion_matrix(y_test, predictions_train_voted))
print(accuracy_score(y_test, predictions_train_voted))