image_output_4.png image_output_3.png image_output_2.png image_output_1.png
import matplotlib.pyplot as plt

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

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

def prepareFeatuers(df):
    df['CabinPrefix'] = [str(cabinname)[0] for cabinname in df.Cabin]
    df.loc[df.Cabin.isnull(), 'CabinPrefix'] = 'None'
    df['CabinKnown'] = [value for value in df.Cabin.isnull()]
    df['TicketSplitLen'] = [len(t.split()) for t in df.Ticket]
    df['Sex_Ind'] = -1
    df.loc[df.Sex == 'female', 'Sex_Ind'] = 1
    df.loc[df.Sex == 'male', 'Sex_Ind'] = 2
    df['Age'] = df.Age.fillna(0)
    df['Fare'] = df.Fare.fillna(0)
    return df
train = prepareFeatuers(train)
test = prepareFeatuers(test)

cols = ['Pclass', 'Sex', 'SibSp', 'Parch', 'Embarked', 'CabinPrefix', 'TicketSplitLen', 'CabinKnown']
for col in cols:
    q = train.groupby(col).Survived.sum()
    t = train.groupby(col).Survived.sum() + train.groupby(col).Survived.count()
    fig, ax = plt.subplots()
    pos = [i for i, name in enumerate(q.index)]
    vals = [name for i, name in enumerate(q.index)]
    ax.barh(pos, t, color='r', label='died')
    ax.barh(pos, q, label='survived')
    ax.set_yticks(pos)
    ax.set_yticklabels(vals)
    ax.set_ylabel(col)
    ax.legend()