text_plain_output_1.png
from csv import DictReader

from keras import layers

from keras import models

import numpy as np # linear algebra

import random

people = list(DictReader(open('../input/train.csv')))
people

def letter_for_cabin(cabin):
    return cabin[0] if len(cabin) else ''
cabin_letters = list(set([letter_for_cabin(p['Cabin']) for p in people]))

def onehot(idx, maximum):
    v = [0] * maximum
    v[idx] = 1
    return v
ages = [float(p['Age']) for p in people if p['Age']]
avg_age = sum(ages) / len(ages)
def vectorize(person):
    pclass = int(person['Pclass'])
    salutations = [1 if sal in person['Name'] else 0 for sal in ['Mr.', 'Miss.', 'Mrs.', 'Master.']]
    embarked = 'CQS'.index(person['Embarked'])
    age = float(person['Age']) if person['Age'] else avg_age
    sibs = 0
    parents = 0
    spouses = 0
    children = 0
    if age > 18:
        children = int(person['Parch'])
        spouses = int(person['SibSp'])
    else:
        parents = int(person['Parch'])
        sibs = int(person['SibSp'])
    sex = -1 if person['Sex'] == 'male' else 1
    fare = float(person['Fare'])
    cabin = cabin_letters.index(letter_for_cabin(person['Cabin']))
    return np.array([pclass, sex, fare, age] + salutations + onehot(embarked, 3) + [sibs, parents, spouses, children] + onehot(cabin, len(cabin_letters)))
def make_vecs_and_labels(people):
    xs = np.stack([vectorize(p) for p in people])
    ys = np.stack([int(p['Survived']) for p in people])
    return (xs, ys)
test_split = 0.8
random.shuffle(people)
input_dim = len(vectorize(people[0]))

x, y = make_vecs_and_labels(people)
def create_model(n_layers=3, layer_size=256, activation='relu', dropout=0):
    model = models.Sequential()
    prev_dim = input_dim
    for i in range(n_layers):
        model.add(layers.Dense(layer_size, input_dim=prev_dim))
        prev_dim = layer_size
        model.add(layers.Activation(activation))
        if dropout > 0:
            model.add(layers.Dropout(dropout))
    model.add(layers.Dense(2))
    model.add(layers.Activation('softmax'))
    model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
    return model

options = {'n_layers': [1, 2, 3, 4], 'layer_size': [128, 256, 512, 1024], 'activation': ['relu', 'softmax'], 'dropout': [0, 0.1]}
def all_configs(options):
    if len(options) == 0:
        return [[]]
    else:
        child_configs = all_configs(options[1:])
        configs = []
        key = options[0][0]
        for value in options[0][1]:
            for child in child_configs:
                configs.append([(key, value)] + child)
        return configs
configs = all_configs(list(options.items()))
random.shuffle(configs)
results = []
for config in configs[:20]:
    model = create_model(**dict(config))
    hist = model.fit(x, np.expand_dims(y, -1), validation_split=0.8, verbose=0, nb_epoch=20)
    results.append((dict(config), max(hist.history['val_acc'])))

best = max(results, key=lambda x: x[1])
best