In [1]:
import pandas
import numpy
MY_TITANIC_TRAIN = 'train_titanic.csv'
MY_TITANIC_TEST = 'test_titanic.csv'
titanic_dataframe = pandas.read_csv(MY_TITANIC_TRAIN, header=0)
print('length: {0}'.format(len(titanic_dataframe)))
titanic_dataframe.head(5)
Out[1]:
In [2]:
titanic_dataframe.columns
Out[2]:
VARIABLE DESCRIPTIONS: survival Survival (0 = No; 1 = Yes) pclass Passenger Class (1 = 1st; 2 = 2nd; 3 = 3rd) name Name sex Sex age Age sibsp Number of Siblings/Spouses Aboard parch Number of Parents/Children Aboard ticket Ticket Number fare Passenger Fare cabin Cabin embarked Port of Embarkation (C = Cherbourg; Q = Queenstown; S = Southampton)
SPECIAL NOTES: Pclass is a proxy for socio-economic status (SES) 1st ~ Upper; 2nd ~ Middle; 3rd ~ Lower
Age is in Years; Fractional if Age less than One (1) If the Age is Estimated, it is in the form xx.5
With respect to the family relation variables (i.e. sibsp and parch) some relations were ignored. The following are the definitions used for sibsp and parch.
Sibling: Brother, Sister, Stepbrother, or Stepsister of Passenger Aboard Titanic Spouse: Husband or Wife of Passenger Aboard Titanic (Mistresses and Fiances Ignored) Parent: Mother or Father of Passenger Aboard Titanic Child: Son, Daughter, Stepson, or Stepdaughter of Passenger Aboard Titanic
Other family relatives excluded from this study include cousins, nephews/nieces, aunts/uncles, and in-laws. Some children travelled only with a nanny, therefore parch=0 for them. As well, some travelled with very close friends or neighbors in a village, however, the definitions do not support such relations.
In [3]:
titanic_dataframe.Age.mean()
Out[3]:
In [4]:
survivors = titanic_dataframe[(titanic_dataframe.Survived==1)]
survivors.Age.mean()
Out[4]:
In [5]:
dead_rich = titanic_dataframe[(titanic_dataframe.Survived==0)&(titanic_dataframe.Pclass==1)]
dead_rich.Age.mean()
Out[5]:
In [6]:
is_survivor = titanic_dataframe['Survived'] == 1
is_male = titanic_dataframe['Sex'] == 'male'
not_Queenstown = titanic_dataframe['Embarked'] != 'Q'
over_30 = titanic_dataframe['Age'] > 30
moniq = titanic_dataframe[is_survivor & is_male & not_Queenstown & over_30]
moniq.Age.mean()
Out[6]:
In [7]:
titanic_dataframe.Age.mean() - titanic_dataframe.Age.median()
Out[7]:
In [8]:
survivors.Age.mean() - survivors.Age.median()
Out[8]:
In [9]:
dead_rich.Age.mean() - dead_rich.Age.median()
Out[9]:
In [10]:
moniq.Age.mean() - moniq.Age.median()
Out[10]:
In [11]:
titanic_dataframe['Pclass'].mode().item()
Out[11]:
In [12]:
titanic_dataframe['Embarked'].mode().item()
Out[12]:
In [13]:
survivors['SibSp'].mode().item()
Out[13]:
In [14]:
ticket_sd = titanic_dataframe['Fare'].std()
median_ticket_price = titanic_dataframe['Fare'].median()
mean_ticket_price = titanic_dataframe['Fare'].mean()
abs((median_ticket_price - mean_ticket_price) / ticket_sd)
Out[14]:
In [15]:
import numpy as np
percentile_expense = np.percentile(titanic_dataframe['Fare'], [90, 5])
percentile_expense[0] - percentile_expense[1]
Out[15]:
In [16]:
ticket_price_90 = titanic_dataframe[(titanic_dataframe['Fare'] >= percentile_expense[0]) & (titanic_dataframe['Pclass'] != 1)]
In [17]:
print(len(ticket_price_90))
In [18]:
ticket_price_5 = titanic_dataframe[(titanic_dataframe['Fare'] <= percentile_expense[1]) & (titanic_dataframe['Pclass'] < 3)]
In [19]:
print(len(ticket_price_5))
In [20]:
ports = [port for port in titanic_dataframe.Embarked.unique() if port == port]
by_port = [(p, titanic_dataframe[titanic_dataframe.Embarked==p]) for p in ports]
for p, people in by_port:
print(p, people.Fare.mean())
In [21]:
for p, people in by_port:
print(p, people.Pclass.std())
In [22]:
lower_than_median = titanic_dataframe['Fare'] < median_ticket_price
is_first_class = titanic_dataframe['Pclass'] == 1
cheapo_dude_survivor_fraction = titanic_dataframe[is_male & lower_than_median & is_first_class & is_survivor]
print(cheapo_dude_survivor_fraction.count())
In [23]:
fam = titanic_dataframe[((titanic_dataframe.SibSp > 0) | (titanic_dataframe.Parch > 0)) & (titanic_dataframe.Survived == 1)]
no_fam = titanic_dataframe[((titanic_dataframe.SibSp == 0) & (titanic_dataframe.Parch == 0)) & (titanic_dataframe.Survived == 0)]
fam.Age.mean() - no_fam.Age.mean()
Out[23]:
In [24]:
import matplotlib.pyplot as pl
% matplotlib inline
fare_quantiles = np.percentile(titanic_dataframe.Fare, np.arange(5, 105, 5.0))
survival_quantiles = []
previous_quantile = 0
for f_q in fare_quantiles:
people = titanic_dataframe[(previous_quantile <= titanic_dataframe.Fare) & (titanic_dataframe.Fare < f_q)]
survival_quantiles.append(sum(people.Survived == 1) / float(len(people)))
pl.plot(np.arange(5, 105, 5.0), survival_quantiles)
pl.xlabel('Fare percentile')
pl.ylabel('Survival rate')
Out[24]: