# Import the libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import scipy

# Read the csv file
titanic = pd.read_csv("titanic-data.csv")

"""
Explanation: Titanic Survival Analysis:
Questions:
According to Wikipedia, "Women and children first" is a code of conduct dating from 1860, whereby the lives of women and children were to be saved first in a life-threatening situation, typically abandoning ship, when survival resources such as lifeboats were limited. The wiki page actually gives some insights and statistics on the survival rate of the Titanic; however, in this analysis, I would reconfirm them, and attempt to find out which other factors that determine the survival rate in the Titanic tragedy. 
The questions I am going to answer in this analysis are:

Was there really a "Women and children first" rule on the Titanic?
Did other factors such as wealth/classes and family sizes affect someone's chance of survival?

First steps:
First, we need to import all the libraries needed for the analysis and load the data file:
End of explanation
"""


titanic.shape

titanic.columns

titanic

"""
Explanation: The next step is to explore the dataset:
End of explanation
"""


titanic = titanic.drop(['PassengerId','Name','Ticket', 'Cabin', 'Embarked'], axis=1)

titanic['Survived'].describe()

"""
Explanation: We can see that Passenger ID, Name and Cabin have little value to the analysis, so we drop these columns off the dataset:
End of explanation
"""


titanic['Age'].describe()

average_age = titanic["Age"].mean()
std_age = titanic["Age"].std()
count_nan_age = titanic["Age"].isnull().sum()
# generate random numbers between (mean - std) & (mean + std)
rand = np.random.randint(average_age - std_age, average_age + std_age, size = count_nan_age)

# Fill NAs in age with median age
titanic['Age'][np.isnan(titanic["Age"])] = rand

titanic['Age'].describe()

sns.distplot(titanic['Age'])
plt.ylabel('Distribution')
plt.title("The distribution of ages of Titanic passengers")
plt.show()

"""
Explanation: Data cleaning:
We can see that both the Age column has a lot of NAs. We would need to fill in the blank with random values generated within their standardized value.
End of explanation
"""


# Family size
titanic['Family_size'] = titanic['SibSp'] + titanic['Parch'] + 1

"""
Explanation: Someone's family size would be equal to their number of spouses/siblings and parents/children on the ship, plus themselves:
End of explanation
"""


survived = titanic[titanic['Survived'] == 1]

"""
Explanation: Now we would extract the survived dataset for future analysis:
End of explanation
"""


def passenger_type(person):
    if person['Age'] <= 16:
        return "child"
    elif person['Sex'] == "female":
        return "female_adult"
    else:
        return "male_adult"

titanic['Type'] = titanic.apply(passenger_type, axis = 1)
titanic

titanic['Type'].value_counts()

sns.set(style="darkgrid")
ax = sns.countplot(x="Type", data = titanic)
plt.title("Number of passengers sorted by type")
plt.show()

"""
Explanation: Women and children first?
Assuming people are neutral on the gender of a kid, I would split the passengers into 3 types:
End of explanation
"""


survived = titanic[titanic['Survived'] == 1]
non_survived = titanic[titanic['Survived'] == 0]

survived['Type'].value_counts()

non_survived['Type'].value_counts()

sns.set(style="darkgrid")
ax = sns.countplot(x="Survived", hue = "Type", data = titanic)
plt.title("Numbers of survivals and non-survivals, sorted by type")
plt.show()

"""
Explanation: We can see that male adults are the initial largest type of people on the ship, followed by female adults and child. 
Now looking into the survival rate:
End of explanation
"""


sns.distplot(survived['Age'])
plt.ylabel("Distribution")
plt.title("The distribution of ages of Titanic survivals")
plt.show()

"""
Explanation: Comparing to the initial number of people of each type, we can see that children have more than 50% survival rate, female adults have an impressive survival rate around 75%, while male adults have a small survival rate of around 16% comparing to their intial numbers. So we can see that there was an inherent "women and children first" code when it came to saving people on the ship.
End of explanation
"""


titanic['Pclass'].value_counts()

sns.set(style="darkgrid")
ax = sns.countplot(x = "Pclass", data = titanic)
plt.xlabel("Passenger classes")
plt.title("Number of passengers, sorted by passenger classes")
plt.show()

"""
Explanation: The histogram of the age distribution of the survival group also confirms that younger people had a higher advantage in survival comparing to older ages.
Socio-economic classes:
We can assume that someone's class on the Titanic represented their socio-economic status. Also, we would assume that the fares have a direct correlation with the classes; so we only need to examine one of them.
End of explanation
"""


survived['Pclass'].value_counts()

sns.set(style="darkgrid")
ax = sns.countplot(x = "Pclass", hue = "Survived", data = titanic)
plt.xlabel("Passenger classes")
plt.title("Number of survivals and non-survivals, sorted by passenger classes")
plt.show()

"""
Explanation: Approximately 55% of the passengers belonged to the third class, while the rest of the ship belong to the first and second classes. Now we'll see if the first and second class passengers also paid a premimum when it comes to safety?
End of explanation
"""


titanic.groupby(['Pclass', 'Type']).Type.count()

sns.set(style="darkgrid")
ax = sns.countplot(x = "Pclass", hue = "Type", data = titanic)
plt.title("Number of people per type, sorted by passenger classes")
plt.show()

titanic.groupby(['Pclass', 'Type']).agg({'Survived': 'sum'})

sns.set(style="darkgrid")
ax = sns.countplot(x = "Pclass", hue = "Type", data = survived)
plt.title("Number of survivals and non-survivals per type, sorted by passenger classes")
plt.show()

"""
Explanation: The survival rate of the first class passengers was more than 60%, while the survival rate of the third class ones was merely around 25%. So we can see that there was a bias on weathiness and soci-economic statuses, even in life-threatning situations.
Now, what if we factor in both passenger classes and types (male, female or children), which would have more weight in survival rate?
End of explanation
"""


titanic['Family_size'].value_counts()

"""
Explanation: We can see the women and children of the first class had a significantly impressive survival rate (more than 90% and 80% respectively), when the women and children of the third class had a much lower survival rate (more than 45% and around 40% respectively). However, the women and children from the third class did have a higher survival rate than the men from higher classes. Men from the first class had a survival rate of around 35%, which was actually below the overall survival rate of 38.38%. Men from the second and third classes suffered very low survival rates, which was around 8 % and around 12 % respectively comparing to their initial numbers.
Family size:
Did people have a higher chance of survival if they traveled with family rather than traveling alone? We'll find out.
End of explanation
"""


survived['Family_size'].value_counts()

sns.boxplot(x="Survived", y="Family_size", data=titanic)
plt.title("The distribution of family sizes of non-survivals and survivals")
plt.show()

sns.kdeplot(survived['Family_size'], shade=True)
plt.ylabel("Distribution")
plt.xlabel("Family size")
plt.title("The distribution of family sizes of non-survivals and survivals")
plt.show()

"""
Explanation: We can see that the majority of the ship traveled by themselves, followed by families of 2 or 3. The families that had more than 3 members made up a small part of the ship. Now look into the survival statistics:
End of explanation
"""


