In this project we will analyse data associated titanic maiden voyage leading to its crash. We will look for trends among passengers who survived and how they differ from passengers who died.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
df = pd.read_csv('titanic_data.csv')
df.head()
df.shape
df.describe()
From the above summary statistics we can see that:
df.info()
df.drop(['PassengerId', 'Name', 'Ticket', 'Cabin'], axis=1, inplace=True)
df.head()
df.hist(figsize=(10,8));
df[df.Age.isnull()].hist(figsize=(10,8));
Comparing above histograms, we can conclude that both datasets are identical except the age so we can replace missing age with average age.
df.fillna(df.mean(), inplace = True)
df[df.Embarked.isnull()]
df.dropna(inplace = True)
df.info()
Tip: Now that you've trimmed and cleaned your data, you're ready to move on to exploration. Compute statistics and create visualizations with the goal of addressing the research questions that you posed in the Introduction section. It is recommended that you be systematic with your approach. Look at one variable at a time, and then follow it up by looking at relationships between variables.
survived = df.Survived == True
died = df.Survived == False
survived.head()
died.head()
df.Fare[survived].mean()
df.Fare[died].mean()
df.Fare[survived].hist(alpha=0.5, label='survived')
df.Fare[died].hist(alpha=0.5, label='died')
plt.legend();
conclusion: We can see from above histogram that people who survived paid more, especially lower class passengers died than survived.
Changing bin size makes the seperation clear.
df.Fare[survived].hist(alpha=0.5, bins=20, label='survived')
df.Fare[died].hist(alpha=0.5, bins=20, label='died')
plt.legend();
df.groupby('Pclass').Survived.mean()
df.groupby('Pclass').Survived.mean().plot(kind='bar');
df.Age[survived].hist(alpha=0.5, bins=20, label='survived')
df.Age[died].hist(alpha=0.5, bins=20, label='died')
plt.legend();
conclusion: Looks like younger people survived more compared to older people.
df.groupby('Sex').Survived.mean()
df.groupby('Sex').Survived.mean().plot(kind='bar');
conclusion: More female survived than male.
df.Sex.value_counts()
conclusion: There are more males than females
df.groupby('Sex')['Pclass'].value_counts()
df.query('Sex == "female"')['Fare'].median(), df.query('Sex == "male"')['Fare'].median()
conclusion: Females spent more money on ticket.
df.groupby(['Pclass','Sex']).Survived.mean().plot(kind='bar');
df.SibSp[survived].value_counts().plot(kind='bar', alpha=0.5, color='blue', label='survived')
df.SibSp[died].value_counts().plot(kind='bar', alpha=0.5, color='orange', label='died')
plt.legend();
conclusion: people having lot of family doesn't appear to be surviving
df.Parch[survived].value_counts().plot(kind='bar', alpha=0.5, color='blue', label='survived')
df.Parch[died].value_counts().plot(kind='bar', alpha=0.5, color='orange', label='died')
plt.legend();
Finally, summarize your findings and the results that have been performed. Make sure to be clear with regards to the limitations of your exploration. If you haven't done any statistical tests, do not imply any statistical conclusions.
General trends are mentioned in each exploration cases.