text_plain_output_1.png
from keras.layers import Dense

from keras.layers import Dropout

from keras.models import Sequential

from keras.optimizers import Adam

import matplotlib.pyplot as plt

import numpy as np

import pandas as pd

import tensorflow as tf

train_df = pd.read_csv('../input/titanic/train.csv')
test_df = pd.read_csv('../input/titanic/test.csv')

n_train = 700
X_train_class = train_df['Pclass'].values.reshape(-1, 1)
X_train_sex = train_df['Sex'].values.reshape(-1, 1)
X_train_age = train_df['Age'].values.reshape(-1, 1)
X_train_sib = train_df['SibSp'].values.reshape(-1, 1)
X_train_par = train_df['Parch'].values.reshape(-1, 1)
y = train_df['Survived'].values.T

X_train = np.hstack((X_train_sex[:n_train, :], X_train_class[:n_train, :], X_train_sib[:n_train, :], X_train_age[:n_train, :], X_train_par[:n_train, :]))
X_test = np.hstack((X_train_sex[n_train:, :], X_train_class[n_train:, :], X_train_sib[n_train:, :], X_train_age[n_train:, :], X_train_par[n_train:, :]))
X_train, X_test = (tf.convert_to_tensor(X_train.astype(np.float64)), tf.convert_to_tensor(X_test.astype(np.float64)))
y_train, y_test = (y[:n_train], y[n_train:])

model = Sequential()
model.add(Dense(300, input_dim=5, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(150, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(100, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(50, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(25, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer=Adam(learning_rate=0.01, beta_1=0.9, beta_2=0.999), metrics=['accuracy'])

history = model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=200, verbose=0)

_, train_acc = model.evaluate(X_train, y_train, verbose=2)
_, test_acc = model.evaluate(X_test, y_test, verbose=2)
print('Train: %.3f, Test: %.3f' % (train_acc, test_acc))
plt.plot(history.history['accuracy'], label='train')
plt.plot(history.history['val_accuracy'], label='test')
plt.legend()
plt.show()