Titanic survivors: prediction of survivors

I use neural networks to predict which passengers will survive, with 66 % accuracy.

Question

Predict who will survive.

Data

It is described on https://www.kaggle.com/c/titanic/data.

Method

Neutral networks

Code

## Import necessary modules

import keras
from keras.layers import Dense
from keras.models import Sequential
from keras.utils import to_categorical

## Import data
df = pd.read_csv('titatic.csv')
df.head()

## y (survived) and x (all other variables)

# y: df['survived'] is binary (n x 1) so create a (n x 2) matrix
y = to_categorical(df['survived']) 
# X: drop y column from df and save as a matrix
X = df.drop(['survived'], axis=1).as_matrix()

## Specify
model = Sequential()
n_cols = df.shape[1] - 1 #use this to chose number of nodes
shape = (n_cols,)
model.add(Dense(32, activation='relu', input_shape=shape)
model.add(Dense(2, activation='softmax'))

## Compile
model.compile(optimizer='sgd', 
              loss='categorical_crossentropy', 
              metrics=['accuracy'])

## Fit
model.fit(X, y)

## Output: Accuracy is 66%

For an explanation of accuracy, read my post on evaluation of binary classificers.

 

Titanic survivors: prediction of survivors