image_output_1.png
import matplotlib.pyplot as plt

import numpy as np

import pandas as pd

import seaborn as sns

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+$'])

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

def create_dummies(df, column_name):
    dummies = pd.get_dummies(df[column_name], prefix=column_name)
    df = pd.concat([df, dummies], axis=1)
    return df

for col in ['Age_categories', 'Pclass', 'Sex', 'Embarked', 'Fare_categories']:
    train = create_dummies(train, col)
    holdout = create_dummies(holdout, col)

train = create_dummies(train, 'titles')
holdout = create_dummies(holdout, 'titles')

train = create_dummies(train, 'Cabin')
holdout = create_dummies(holdout, 'Cabin')

columns_cabins_titles = ['Age_categories_Missing', 'Age_categories_Infant', 'Age_categories_Child', 'Age_categories_Teenager', 'Age_categories_Young Adult', 'Age_categories_Adult', 'Age_categories_Senior', 'Pclass_1', 'Pclass_2', 'Pclass_3', 'Sex_female', 'SibSp_scaled', 'Parch_scaled', 'Fare_categories_0-12$', 'Fare_categories_12-50$', 'Fare_categories_50-100$', 'Fare_categories_100+$', 'titles_ Capt', 'titles_ Col', 'titles_ Don', 'titles_ Dr', 'titles_ Jonkheer', 'titles_ Lady', 'titles_ Major', 'titles_ Master', 'titles_ Miss', 'titles_ Mlle', 'titles_ Mme', 'titles_ Mr', 'titles_ Mrs', 'titles_ Ms', 'titles_ Rev', 'titles_ Sir', 'titles_ the Countess', 'Cabin_A', 'Cabin_B', 'Cabin_C', 'Cabin_D', 'Cabin_E', 'Cabin_F', 'Cabin_G', 'Cabin_T', 'Cabin_u']
other_cols = ['Pclass_1', 'Pclass_2', 'Pclass_3']

columns_cabins_titles = ['Age_categories_Missing', 'Age_categories_Infant', 'Age_categories_Child', 'Age_categories_Teenager', 'Age_categories_Young Adult', 'Age_categories_Adult', 'Age_categories_Senior', 'Pclass_1', 'Pclass_2', 'Pclass_3', 'Sex_female', 'SibSp_scaled', 'Parch_scaled', 'Fare_categories_0-12$', 'Fare_categories_12-50$', 'Fare_categories_50-100$', 'Fare_categories_100+$', 'titles_ Capt', 'titles_ Col', 'titles_ Don', 'titles_ Dr', 'titles_ Jonkheer', 'titles_ Lady', 'titles_ Major', 'titles_ Master', 'titles_ Miss', 'titles_ Mlle', 'titles_ Mme', 'titles_ Mr', 'titles_ Mrs', 'titles_ Ms', 'titles_ Rev', 'titles_ Sir', 'titles_ the Countess', 'Cabin_A', 'Cabin_B', 'Cabin_C', 'Cabin_D', 'Cabin_E', 'Cabin_F', 'Cabin_G', 'Cabin_T']

def plot_correlation_heatmap(df):
    corr = df.corr()
    
    sns.set(style="white")
    mask = np.zeros_like(corr, dtype=np.bool)
    mask[np.triu_indices_from(mask)] = True

    f, ax = plt.subplots(figsize=(11, 9))
    cmap = sns.diverging_palette(520, 10, as_cmap=True)

    sns.heatmap(corr, mask=mask, cmap=cmap, vmax=1, center=0,
            square=True, linewidths=.5, cbar_kws={"shrink": .5})
    plt.show()

corrs = train[columns_cabins_titles].corr()

plot_correlation_heatmap(corrs.corr())