<jupyter_start><jupyter_text>Titanic passengers dataset Analysis IntroductionThe sinking of the RMS Titanic occurred on the night of 14 April through to the morning of 15 April 1912 in the North Atlantic Ocean, four days into the ship's maiden voyage from Southampton to New York City. The largest passenger liner in service at the time, Titanic had an estimated 2,224 people on board when she struck an iceberg at around 23:40 (ship's time)[a] on Sunday, 14 April 1912. Her sinking two hours and forty minutes later at 02:20 (05:18 GMT) on Monday, 15 April resulted in the deaths of more than 1,500 people, which made it one of the deadliest peacetime maritime disasters in history.Wikipedia contributors. "Sinking of the RMS Titanic." Wikipedia, The Free Encyclopedia. Wikipedia, The Free Encyclopedia, 24 Jul. 2017. Web. 8 Aug. 2017Titanic Data dataset contains demographics and passenger information from 891 of the 2224 passengers and crew on board the Titanic.This Analysis is used to find factors that made people more likely to survive: 1. Could the rich really buy their places in the lifeboats? 2. Did women and children really survived in better proportion in comparison of men independantly of their social ranking ? (Women and children before men in the lifeboats policy) 3. Then, did a young girl or lady from a rich family really had better chances of surviving overall?4. Overall did a whole family had better chances of surviving than single persons?  Data WranglingTitanic_data.csv will be the core and only csv used for this analysis.<jupyter_code># import all necessary packages and functions.
import csv
import numpy as np
import pandas as pd
from IPython.display import display
import matplotlib.pyplot as plt
import matplotlib
import seaborn as sns
sns.set_style('whitegrid')
matplotlib.style.use('ggplot')

%matplotlib inline<jupyter_output><empty_output><jupyter_text>Understanding the data and its variables/featuresReport to: https://www.kaggle.com/c/titanic/data for more information on the variables definition<jupyter_code># Display the first few rows of the station data file.
titanic_data = pd.read_csv('titanic_data.csv')
display(titanic_data.head())
titanic_data.info()
# Drop variables that are not required to answer raised questions and display head again
titanic_data = titanic_data.drop(['Ticket', 'Name', 'PassengerId', 'Cabin'], axis=1)
display(titanic_data.head())<jupyter_output><empty_output><jupyter_text>I am a rich, let me enter this lifeboat immediatly! Of course Monsieur, part ici...'After leaving Southampton on 10 April 1912, Titanic called at Cherbourg in France and Queenstown (now Cobh) in Ireland before heading west to New York.'<jupyter_code># Fill the two missing values in Embarked with the most occurred value "S"
titanic_data['Embarked'] = titanic_data['Embarked'].fillna("S")
# Plot on 3 axis with matplotlib subplots method to see general Embarked variable trend
fig, (axis1,axis2,axis3) = plt.subplots(nrows=1, ncols=3,figsize=(15,5))
sns.countplot(x='Embarked', data=titanic_data, edgecolor='black', ax=axis1)
sns.countplot(x='Survived', hue='Embarked', data=titanic_data, order=[1,0], edgecolor='black', ax=axis2)

# Group by embarked, and get the mean for survived passengers for each value in Embarked
embark_mean = titanic_data[["Embarked", "Survived"]].groupby(['Embarked'],as_index=False).mean()
sns.barplot(x='Embarked', y='Survived', data=embark_mean,order=['S','C','Q'],edgecolor='black', ax=axis3)<jupyter_output><empty_output><jupyter_text>It seems that the people embarking from Cherbourg in France were a bit more likely to survive. Were they richer overall? or was this group composed of more children and women? At first, the fares variable could help us determine if they were indeed richer.<jupyter_code># Finding the extremums for the range of the Fares
titanic_data.describe()
# get fare for survived and none surviving passengers overall
fare_not_survived = titanic_data["Fare"][titanic_data["Survived"] == 0]
fare_survived = titanic_data["Fare"][titanic_data["Survived"] == 1]

# get fare for Embarked variable
Cherbourg = titanic_data["Fare"][titanic_data["Embarked"] == 'C']
Queen = titanic_data["Fare"][titanic_data["Embarked"] == 'Q']
South = titanic_data["Fare"][titanic_data["Embarked"] == 'S']
# convert from float to int
titanic_data['Fare'] = titanic_data['Fare'].astype(int)

# ploting the fare for surviving and none surviving persons bins size represents 10$
fig, (axis1,axis2,axis3) = plt.subplots(nrows=1, ncols=3,figsize=(15,5))
fare_survived.plot(kind='hist', figsize=(15,3), xlim=(0,550), ax=axis1, color = 'green', edgecolor='black', title='Alive')
fare_not_survived.plot(kind='hist', figsize=(15,3), xlim=(0,550), ax=axis2, color = 'red', edgecolor='black', title='Dead')

# plotting the fare mean vs survived variable
fare_mean = titanic_data[["Fare", "Survived"]].groupby(['Survived'],as_index=False).mean()
sns.barplot(x='Survived', y= 'Fare', data= fare_mean,order=[0,1], edgecolor='black', ax=axis3)<jupyter_output><empty_output><jupyter_text>No suprise here, richer people (people who paid a higher fair), where most likely to survive<jupyter_code># Plotting fare histogram by embarked town
fig, (axis1,axis2,axis3) = plt.subplots(nrows=1, ncols=3,figsize=(15,5))
South.plot(kind='hist', figsize=(15,3), xlim=(0,550), edgecolor='black', ax=axis1, color = 'C0', title='Southampton')
Cherbourg.plot(kind='hist', figsize=(15,3), xlim=(0,550), edgecolor='black', ax=axis2, color = 'C1', title='Cherbourg')
Queen.plot(kind='hist', figsize=(15,3), xlim=(0,550), edgecolor='black', ax=axis3, color = 'C2', title='Queen')

Fare_mean = (titanic_data[["Embarked", "Fare"]].groupby(['Embarked'],as_index=False)).mean()
sns.barplot(x='Embarked', y='Fare', data= Fare_mean, order=['S','C','Q'], edgecolor='black')<jupyter_output><empty_output><jupyter_text>And yes, overall, the people boarding from Cherbourg, France, appeared to be richer. By browsing a bit in the csv file, through excel, you can see that: Margaret "Molly" Brown joined Titanic's first class passengers at Cherbourg, as well as Lucy, <NAME> with her husband Sir <NAME>. The Cardeza family had no trouble affording what is believed to have been the most expensive ticket on the ship. However, these Super-Rich outliers are very sure to drag the mean of the fare up.http://www.theaustralian.com.au/archive/in-depth/the-rich-and-famous-aboard-titanic/news-story/f9895dd3b00d8f71aebaa08f3f95ec76http://time.com/money/4283738/titanic-most-expensive-ticket/<jupyter_code>class_mean = titanic_data[["Pclass", "Survived"]].groupby(['Pclass'],as_index=False).mean()
sns.barplot(x='Pclass', y='Survived', data=class_mean,edgecolor='black')<jupyter_output><empty_output><jupyter_text>And yes, you had better chance of surviving if you were boarding as a first class passenger<jupyter_code>sns.countplot(x='Embarked', hue="Pclass", data=titanic_data, edgecolor='black')<jupyter_output><empty_output><jupyter_text>Finally, it seems, indeed, that Cherbourg had a higher proportion of first class members in their number of passengers, but surprisingly there were a lot of third class member too. Excluding the super-rich outliers, could it be possible that there were a lot of women or child, embarked from Chesbourg, in the third class group?<jupyter_code># peaks for survived/not survived passengers by their age
facet = sns.FacetGrid(titanic_data, hue="Survived",aspect=4)
facet.map(sns.kdeplot,'Age',shade= True)
facet.set(xlim=(0, titanic_data['Age'].max()))
facet.add_legend()

# drop all null values, and convert to int
titanic_data['Age'] = titanic_data['Age'].dropna().astype(int)

# average survived passengers by age
fig, axis1 = plt.subplots(1,1,figsize=(18,4))
average_age = titanic_data[['Age', 'Survived']].groupby(['Age'],as_index=False).mean()
sns.barplot(x='Age', y='Survived', data= average_age)



# Instead of having two columns Parch & SibSp, 
# we can have only one column represent if the passenger had any family member aboard or not,
# Meaning, if having any family member(whether parent, brother, ...etc) will increase chances of Survival or not.
titanic_data['Family'] =  titanic_data["Parch"] + titanic_data["SibSp"]
titanic_data.loc[titanic_data['Family'] > 0] = 1
titanic_data.loc[titanic_data['Family'] == 0] = 0<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11634049705847041, 'fraction_numerical': 0.021731300276143596, 'mean_word_length': 6.785046728971962, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 36, 'https://': 1, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '391961', 'license': 'MIT', 'n_tokens_mistral': 2821, 'n_tokens_neox': 2621, 'n_words': 882, 'path': 'Jupyter_Data/Titanic_Practice/Titanic Survival Analysis.ipynb', 'repo_name': 'JulienL3vesque/Hexoskin_RnD_OSM', 'size': '8350'}