<jupyter_start><jupyter_text>Titanic Survivorship DataOn April 15, 1912, Titanic sank after colliding with an iceberg. 1502 out of 2224 souls on board perished in the incident. The survivorship data contain information about 891 passengers. Less than 40% of the passengers in the data set survived.This analysis focus primarily on the how various factors might improve someone's survival chances in a disaster like the sinking of the Titanic. Does having close relations who can help improve one's odds, can having existing resources help to save oneself from disaster, or, perhaps, is one's lucky of being at the right spot at the right time all that matters?<jupyter_code># Standard imports and boilerplate

import numpy as np
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()

# Helper Function

def survival_rate(df):
    return df["Survived"].sum() / len(df) 
# Read in the Titanic Data

titanic_pd = pd.read_csv("titanic-data.csv")
print(titanic_pd.columns)<jupyter_output>Index(['PassengerId', 'Survived', 'Pclass', 'Name', 'Sex', 'Age', 'SibSp',
       'Parch', 'Ticket', 'Fare', 'Cabin', 'Embarked'],
      dtype='object')<jupyter_text>According to the Kaggle website, the columns are defined as follows:        PassengerId - Unique ID for passengers    Survived - 0->no, 1->yes    Pclass - Approximate social-economic status    Name - Name of passenger    Sex - Sex    Age - Age in years    SibSp -  of siblings or spouses on board    Parch -  of parents or children on board    Ticket - Ticket number    Fare - Passenger fare    Cabin - Cabin number if applicable, NaN otherwise    Embarked - Port of embarkasionSpecial Definitions    Pclass - 1 = upper, 2 = middle, 3 = lower    SibSp - brothers/sisters/stepsiblings/husbands/wifes (mistress/fiances are ignored)    Parch - mother/father/daughter/son/step-daughter/step-son (nannies are ignored)    https://www.kaggle.com/c/titanic/data<jupyter_code>#Initial look at the data

print(titanic_pd.head())
# From the above, there are clearly NaN values in Cabin column.
# To remove all NaN in the data, let's find the columns that contain NaN

for col in titanic_pd.columns:
    for e in titanic_pd[col].isnull():
        if e == True:
            print(col)
            break
# Deal with NaN in Age
# Change NaN in Age to 0 to indicate missing age information.
# Babies with age less than 1 have fractional ages.

titanic_pd["Age"].fillna(value=0);
# Deal with NaN in Cabin

# Without knowing more, the cabin number does not provide more info
# beyond someone having a cabin, which is all I need for my analysis.
# Change value to 1 to reflect the passenger having a cabin
# Change value to 0 to reflect the passenger not having a cabin

# Most people do not have a cabin, thus most likely people with NaN 
# value for their cabin information do not have a cabin. For simplicity,
# I will assume that people with NaN in their cabin data do not have
# cabins, and set their values to 0.

titanic_pd["Cabin"] = titanic_pd["Cabin"].notnull().astype("int");
# Deal with NaN in Embarked
# Change NaN in Embarked to 0 to represent no knowledge of the port of embarkation
# Port of embarkation is also uninteresting for my analysis

titanic_pd["Embarked"].fillna(value=0);
# Looking at the data
overall_survival = survival_rate(titanic_pd)

print("Total Passengers in Data Set:", len(titanic_pd))
print("Overall Survivorship:", str(round(overall_survival, 3)*100)+"%")
print("Possible Factors:")
for col in titanic_pd.columns:
    print("\t"+col)

%matplotlib inline
graph_title = "Overall Survival Passenger Figure in Kaggle Titanic Data Set"
titanic_pd.groupby("Survived").size().plot(kind="barh",
                                           title=graph_title).set(xlabel="Number of Survivors",
                                                                  ylabel="Survivorship (1 = Survived, 0 = Perished)")
# Does having a close relation such as a parent/child or sibling/spouse
# improve the odds?
# I am going to look at the SibSp and Parch for this information.
# First let's look at the SibSp

has_sibsp_pd = titanic_pd[titanic_pd["SibSp"] > 0]
no_sibsp_pd = titanic_pd[titanic_pd["SibSp"] == 0]
has_sibsp_survival = survival_rate(has_sibsp_pd)
no_sibsp_survival = survival_rate(no_sibsp_pd)
print("Number of passengers with at least one sibling or spouse:",
      len(has_sibsp_pd))
print("Survivor Rate:", has_sibsp_survival)
print()
print("Number of passengers with no sibling or spouse:",
      len(no_sibsp_pd))
print("Survivor Rate:", no_sibsp_survival)
print()

%matplotlib inline
plt.title("Impact of SibSp on Survival for passengers on Titantic (fine gradient)")
sns.barplot(x="SibSp", y="Survived", data=titanic_pd).set(xlabel='Number of SibSp', ylabel='Survival Rate')
plt.show()
# Let's look at the ParCh

has_parch_pd = titanic_pd[titanic_pd["Parch"] > 0]
has_parch_survival = survival_rate(has_parch_pd)
no_parch_pd = titanic_pd[titanic_pd["Parch"] == 0]
no_parch_survival = survival_rate(no_parch_pd)

print("Number of passengers with at least one parent/child on board:",
      len(has_parch_pd))
print("Survivor Rate:", has_parch_survival)
print("Number of passengers with no parent/child on board:",\
      len(no_parch_pd))
print("Survivor Rate:", no_parch_survival)

%matplotlib inline
plt.title("Impact of Parch on Survival for passengers on Titantic (fine gradient)")
sns.barplot(x="Parch", y="Survived", data=titanic_pd).set(xlabel='Number of Parch', ylabel='Survival Rate')
plt.show()
# Does having additional resources such as being male
# (potentially physically stronger) or having wealth improve the odds?
# I am going to look at the Sex and Pclass columns for this information.

male_passengers_pd = titanic_pd[titanic_pd["Sex"] == "male"]
female_passengers_pd = titanic_pd[titanic_pd["Sex"] == "female"]

first_class_pd = titanic_pd[titanic_pd["Pclass"] == 1]
first_class_male_pd = male_passengers_pd[male_passengers_pd["Pclass"] == 1]
first_class_female_pd = female_passengers_pd[female_passengers_pd["Pclass"] == 1]

second_class_pd = titanic_pd[titanic_pd["Pclass"] == 2]
second_class_male_pd = male_passengers_pd[male_passengers_pd["Pclass"] == 2]
second_class_female_pd = female_passengers_pd[female_passengers_pd["Pclass"] == 2]

third_class_pd = titanic_pd[titanic_pd["Pclass"] == 3]
third_class_male_pd = male_passengers_pd[male_passengers_pd["Pclass"] == 3]
third_class_female_pd = female_passengers_pd[female_passengers_pd["Pclass"] == 3]

print("Number of male passengers:",len(male_passengers_pd))
print("Survival Rate:", survival_rate(male_passengers_pd))
print()

print("Number of female passengers:",len(female_passengers_pd))
print("Survival Rate:", survival_rate(female_passengers_pd))
print()

print("Number of passengers in first class:", len(first_class_pd))
print("Males:", len(first_class_male_pd))
print("Females:", len(first_class_female_pd))
print("Overall Survival Rate:", survival_rate(first_class_pd))
print("Male survival rate in first class:", survival_rate(first_class_male_pd))
print("Female survival rate in first class:", survival_rate(first_class_female_pd))
print()

print("Number of passengers in second class:", len(second_class_pd))
print("Males:", len(second_class_male_pd))
print("Females:", len(second_class_female_pd))
print("Overall Survival Rate:", survival_rate(second_class_pd))
print("Male survival rate in second class:", survival_rate(second_class_male_pd))
print("Female survival rate in second class:", survival_rate(second_class_female_pd))
print()

print("Number of passengers in third class:", len(third_class_pd))
print("Males:", len(third_class_male_pd))
print("Females:", len(third_class_female_pd))
print("Overall Survival Rate:", survival_rate(third_class_pd))
print("Male survival rate in third class:", survival_rate(third_class_male_pd))
print("Female survival rate in third class:", survival_rate(third_class_female_pd))
print()
# Does having one's sleeping quarters on particular areas on the ship help with survival?
# I am going to use Cabin information for this information.
# Since I don't have a map of the ship that would show where the cabins are located,
# I'll just use cabin as a generally more desirable place to live, and an indicator for wealth,
# or having othe resources

has_cabin_pd = titanic_pd[titanic_pd["Cabin"] == 1]
no_cabin_pd = titanic_pd[titanic_pd["Cabin"] == 0]

print("Number of passengers with a cabin:", len(has_cabin_pd))
print("Survival Rate:", survival_rate(has_cabin_pd))
print()

print("Number of passengers without a cabin:", len(no_cabin_pd))
print("Survival Rate:", survival_rate(no_cabin_pd))
print()

%matplotlib inline
plt.title("Desirability of having a cabin (or how having a cabin can help one survive)")
sns.barplot(x="Cabin", y="Survived", data=titanic_pd).set(xlabel='Having a Cabin (0 = No, 1 = Yes)',
                                                          ylabel='Survival Rate')
plt.show()<jupyter_output>Number of passengers with a cabin: 204
Survival Rate: 0.666666666667

Number of passengers without a cabin: 687
Survival Rate: 0.299854439592
{'fraction_non_alphanumeric': 0.10003290556103982, 'fraction_numerical': 0.009542612701546561, 'mean_word_length': 5.215405589638719, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '572132', 'license': 'MIT', 'n_tokens_mistral': 3064, 'n_tokens_neox': 2823, 'n_words': 941, 'path': 'Titanic Project/titanic_survivorship.ipynb', 'repo_name': 'yth/dand', 'size': '9117'}