text_html_output_1.png
import numpy as np

import pandas as pd

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

data = pd.concat([train, test])
data.shape

train.columns

train = train.drop(['Cabin', 'Ticket'], axis=1)
test = test.drop(['Cabin', 'Ticket'], axis=1)
(train.shape, test.shape)

pd.crosstab(train['Title'], train['Sex'])

data = [train, test]
for d in data:
    d['Title'] = d['Title'].replace(['Capt', 'Col', 'Countess', 'Don', 'Dr', 'Jonkheer', 'Lady', 'Major', 'Sir', 'Rev'], 'Rare')
    d['Title'] = d['Title'].replace(['Mlle', 'Mme'], 'Miss')
    d['Title'] = d['Title'].replace('Mme', 'Mrs')
train[['Title', 'Survived']].groupby('Title').mean()

titles = {'Master': 1, 'Miss': 2, 'Mr': 3, 'Mrs': 4, 'Ms': 5, 'Rare': 6}
for d in data:
    d['Title'] = d['Title'].map(titles)
    d['Title'] = d['Title'].fillna(0)

train = train.drop(['PassengerId', 'Name'], axis=1)
test = test.drop(['PassengerId', 'Name'], axis=1)
data = [train, test]
(train.shape, test.shape)

for i in data:
    i['Sex'] = i['Sex'].map({'male': 0, 'female': 1}).astype(int)

ages = np.zeros((2, 3))
ages

for d in data:
    for i in range(0, 2):
        for j in range(0, 3):
            age_data = d[(d['Sex'] == i) & (d['Pclass'] == j + 1)]['Age'].dropna()
            age = age_data.median()
            ages[i, j] = age
    for i in range(0, 2):
        for j in range(0, 3):
            d.loc[d['Age'].isnull() & (d['Sex'] == i) & (d['Pclass'] == j + 1), 'Age'] = ages[i, j]
    d['Age'] = d['Age'].astype(int)

train['SegAge'] = pd.cut(train['Age'], 5)
train[['SegAge', 'Survived']].groupby('SegAge', as_index=False).mean()