image_output_1.png
from sklearn import svm

from sklearn.ensemble import RandomForestClassifier

from sklearn.linear_model import LogisticRegression

from sklearn.metrics import accuracy_score

from sklearn.metrics import classification_report

from sklearn.metrics import confusion_matrix

from sklearn.model_selection import train_test_split

from sklearn.neighbors import KNeighborsClassifier

from sklearn.neural_network import MLPClassifier

import matplotlib.pyplot as plt

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

import seaborn as sn

data = pd.read_csv('../input/train.csv')
test_data = pd.read_csv('../input/test.csv')

fig = plt.figure(figsize = (30,20))
ax = fig.gca()
hist = data.hist(ax=ax)

data['Sex'] = data['Sex'].map({'female': 1, 'male': 0})
data['Age'].fillna(data['Age'].mean(), inplace=True)
data['Embarked'].fillna(method='ffill', inplace=True)
one_hot_columns = pd.get_dummies(data['Embarked'], prefix=None)
data = pd.concat([data, one_hot_columns], axis=1)
data.drop(['Embarked'], axis=1, inplace=True)
import re
def ticket_to_float(ticket_str):
    ticket_numbers_only = ''.join((i for i in ticket_str if i.isdigit()))
    if ticket_numbers_only is '':
        return 0
    return int(ticket_numbers_only)
data['Ticket'] = data['Ticket'].apply(ticket_to_float)
test_data['Sex'] = test_data['Sex'].map({'female': 1, 'male': 0})
test_data['Age'].fillna(test_data['Age'].mean(), inplace=True)
test_data['Fare'].fillna(test_data['Fare'].mean(), inplace=True)
test_data['Embarked'].fillna(method='ffill', inplace=True)
one_hot_columns = pd.get_dummies(test_data['Embarked'], prefix=None)
test_data = pd.concat([test_data, one_hot_columns], axis=1)
test_data.drop(['Embarked'], axis=1, inplace=True)
test_data['Ticket'] = test_data['Ticket'].apply(ticket_to_float)

feature_columns = ['Pclass', 'Sex', 'Age', 'SibSp', 'Parch', 'Fare', 'C', 'Q', 'S']

from sklearn.model_selection import train_test_split
X = data[feature_columns]
y = data['Survived']
X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.8, test_size=0.2, random_state=0)
X_train.shape

from sklearn.linear_model import LogisticRegression
from sklearn.neural_network import MLPClassifier
from sklearn import svm
from sklearn.neighbors import KNeighborsClassifier
from sklearn.ensemble import RandomForestClassifier
log_reg = LogisticRegression(solver='sag', random_state=0)
log_reg.fit(X_train, y_train)
n_net = MLPClassifier(hidden_layer_sizes=(4, 4, 4), max_iter=500)
n_net.fit(X_train, y_train)
svmC = svm.SVC(kernel='linear')
svmC.fit(X_train, y_train)
k_NN = KNeighborsClassifier(n_neighbors=3)
k_NN.fit(X_train, y_train)
rfc = RandomForestClassifier(n_estimators=100)
rfc.fit(X_train, y_train)

from sklearn.metrics import confusion_matrix
from sklearn.metrics import classification_report
from sklearn.metrics import accuracy_score
if y_test is not None:
    y_pred_log_reg = log_reg.predict(X_test)
    confusion_matrix_log_reg = confusion_matrix(y_test, y_pred_log_reg)
    target_names = ['Survived', 'Not Survived']

if y_test is not None:
    y_pred_n_net = n_net.predict(X_test)
    confusion_matrix_n_net = confusion_matrix(y_test, y_pred_n_net)
    target_names = ['Survived', 'Not Survived']

if y_test is not None:
    y_pred_svm = svmC.predict(X_test)
    confusion_matrix_svm = confusion_matrix(y_test, y_pred_svm)
    sn.heatmap(confusion_matrix_svm, annot=True, cmap='Blues', fmt='g')
    target_names = ['Survived', 'Not Survived']
    print(classification_report(y_test, y_pred_svm, target_names=target_names))