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

import pandas as pd

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

chance_survive = len(train[train['Survived'] == 1]) / len(train['Survived'])

def plot_survival(df, index, color='blue', use_index=True, num_xticks=0, xticks='', position=0.5, legend=['General probabilty of survival']):
    df_pivot = df.pivot_table(index=index, values='Survived')
    if num_xticks > 0:
        plt.xticks(range(num_xticks), xticks)

cut_points = [-1, 0, 5, 12, 18, 35, 60, 100]
label_names = ['Missing', 'Infant', 'Child', 'Teenager', 'Young Adult', 'Adult', 'Senior']
def process_age(df, cut_points, label_names):
    df['Age'] = df['Age'].fillna(-0.5)
    df['Age_categories'] = pd.cut(df['Age'], cut_points, labels=label_names)
    return df

train = process_age(train, cut_points, label_names)
holdout = process_age(holdout, cut_points, label_names)

def process_fare(df, cut_points, label_names):
    df['Fare_categories'] = pd.cut(df['Fare'], cut_points, labels=label_names)
    return df

train = process_fare(train, [0, 12, 50, 100, 1000], ['0-12$', '12-50$', '50-100$', '100+$'])

plot_survival(train, 'Fare_categories', use_index=False, num_xticks=len(train['Fare_categories'].unique()) - 1, xticks=train['Fare_categories'].unique().sort_values())