text_plain_output_1.png
import matplotlib.pyplot as plt

import numpy as np

import pandas as pd

import seaborn as sns

df = pd.read_csv('../input/train.csv', index_col='PassengerId')

df.dropna().describe()

ages_mean = df.pivot_table('Age', index=['Title'], columns=['Sex', 'Pclass'], aggfunc='mean')
ages_mean

ages_std = df.pivot_table('Age', index=['Title'], columns=['Sex', 'Pclass'], aggfunc='std')
ages_std

def age_guesser(person):
    gender = person['Sex']
    mean_age = ages_mean[gender].loc[person['Title'], person['Pclass']]
    std = ages_std[gender].loc[person['Title'], person['Pclass']]
    persons_age = np.random.randint(mean_age - std, mean_age + std)
    return persons_age
unknown_age = df['Age'].isnull()
people_w_unknown_age = df.loc[unknown_age, ['Age', 'Title', 'Sex', 'Pclass']]
people_w_unknown_age['Age'] = people_w_unknown_age.apply(age_guesser, axis=1)

known_age = df['Age'].notnull()
people_w_known_age = df.loc[known_age, ['Age', 'Title', 'Sex', 'Pclass']]
df['new_age'] = pd.concat([people_w_known_age['Age'], people_w_unknown_age['Age']])

for pclass in [1, 2, 3]:
    df[df['Pclass'] == pclass]['Age'].plot.kde(figsize=(12, 10))
    df[df['Pclass'] == pclass]['new_age'].plot.kde()
plt.xlim(-10, 90)
plt.ylim(0, 0.05)
plt.xlim(-10, 90)
plt.ylim(0, 0.05)

plt.xlim(0, None)

df['parent'] = 0
df.loc[(df.Parch > 0) & (df.new_age >= 18), 'parent'] = 1
df['child'] = 0
df.loc[(df.Parch > 0) & (df.new_age < 18), 'child'] = 1

sns.factorplot(x='Pclass', y='Survived', data=df, kind='bar', size=5, aspect=1.5, ci=None, hue='family')
plt.title('Survival Rate by Class and Family Size')