/
Titantic Analysis .py
358 lines (170 loc) · 6.59 KB
/
Titantic Analysis .py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#!/usr/bin/env python
# coding: utf-8
# Titanic Dataset
#
# In this Dataset we will analyse the Titanic Dataset.
# In[3]:
#Using Pandas to open the Dataset
import pandas as pd
from pandas import Series, DataFrame
#Set up Titanic csv file as a DataFrame
titanic_df = pd.read_csv('train.csv')
# In[4]:
#Taking a preview of the data
titanic_df.head()
# In[5]:
titanic_df.info()
# Looking at the dataset, we will try to answer the these questions:
#
#
# 1.) Who were the passengers on the Titanic? (Ages,Gender,Class,..etc)
# 2.) What deck were the passengers on and how does that relate to their class?
# 3.) Where did the passengers come from?
# 4.) Who was alone and who was with family?
#
# Then we'll dig deeper, with a broader question:
#
# 5.) What factors helped someone survive the sinking?
# In[6]:
# Let's import what we'll need for the analysis and visualization
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
get_ipython().run_line_magic('matplotlib', 'inline')
# In[15]:
# Let's first check gender
sns.catplot(x='Sex',kind='count',data=titanic_df)
# Looing at the Bar Plot of Male and Female, we can see that there are half as many females as males.
# In[17]:
# Now let's seperate the genders by classes, remember we can use the 'hue' arguement here!
sns.catplot(x='Pclass',kind='count',data=titanic_df,hue='Sex')
# Visualizing the male and female ratio by class, we can see that there are more males in the third class and the ratio of male-female is not proportional in third class than in 1st and 2nd class.
#
# It would be interesting to split the into male, female, and children. We will do this by considering anyone under 16 as children and apply a function to create a new column.
# In[18]:
def male_female_child(passenger):
#Take the Age and Sex
age,sex = passenger
#Compare the age, otherwise leave the sex
if age < 16:
return 'child'
else:
return sex
#We will define a new column called 'person', remember to specify axis=1 for columns and not index
titanic_df['person'] = titanic_df[['Age','Sex']].apply(male_female_child, axis =1)
# In[19]:
titanic_df[0:10]
# In[20]:
# Let's try the factorplot again!
sns.catplot(x='Pclass', kind='count',data=titanic_df,hue='person')
# In[27]:
# Getting a distribution of ages
titanic_df['Age'].hist(bins=70)
# In[29]:
#Getting a quick comparison of male, female, child
titanic_df['person'].value_counts()
# In[33]:
fig = sns.FacetGrid(titanic_df, hue='Sex', aspect=4)
fig.map(sns.kdeplot,'Age',shade=True)
# Set the x max limit by the oldest passenger
oldest = titanic_df['Age'].max()
#Since we know no one can be negative years old set the x lower limit at 0
fig.set(xlim=(0,oldest))
#Finally add a legend
fig.add_legend()
# In[35]:
fig = sns.FacetGrid(titanic_df, hue='person', aspect=4)
fig.map(sns.kdeplot,'Age',shade=True)
# Set the x max limit by the oldest passenger
oldest = titanic_df['Age'].max()
#Since we know no one can be negative years old set the x lower limit at 0
fig.set(xlim=(0,oldest))
#Finally add a legend
fig.add_legend()
# In[36]:
fig = sns.FacetGrid(titanic_df, hue='Pclass', aspect=4)
fig.map(sns.kdeplot,'Age',shade=True)
# Set the x max limit by the oldest passenger
oldest = titanic_df['Age'].max()
#Since we know no one can be negative years old set the x lower limit at 0
fig.set(xlim=(0,oldest))
#Finally add a legend
fig.add_legend()
# In[38]:
titanic_df.head()
# In[39]:
# First we'll drop the NaN values and create a new object, deck
deck = titanic_df['Cabin'].dropna()
# In[40]:
deck.head()
# In[45]:
#We will try to grab that letter with a simple for loop
#Set empty list
levels = []
#Loop to grab first letter
for level in deck:
levels.append(level[0])
#Reset DataFrame and use factor plot
cabin_df = DataFrame(levels)
cabin_df.columns = ['Cabin']
sns.catplot(x='Cabin',kind='count',data=cabin_df,palette='winter_d')
# In[46]:
# Redefine cabin_df as everything but where the row was equal to 'T'
cabin_df = cabin_df[cabin_df.Cabin != 'T']
#Replot
sns.catplot(x='Cabin',kind='count',data=cabin_df,palette='summer')
# Now after analysing the deck, we will see where did the passengers come from
# In[49]:
sns.catplot(x='Embarked',kind='count',data=titanic_df,hue='Pclass',row_order=['C','Q','S'])
# An interesting find here is that in Queenstown, almost all the passengers that boarded there were 3rd class. It would be intersting to look at the economics of that town in that time period for further investigation.
#
# Now let's take a look at the 4th question:
#
# 4.) Who was alone and who was with family?
# In[50]:
# Let's start by adding a new column to define alone
# We'll add the parent/child column with the sibsp column
titanic_df['Alone'] = titanic_df.Parch + titanic_df.SibSp
titanic_df['Alone']
# In[54]:
# Look for >0 or ==0 to set alone status
titanic_df['Alone'].loc[titanic_df['Alone'] >0] = 'With Family'
titanic_df['Alone'].loc[titanic_df['Alone'] == 0] = 'Alone'
# Note it's okay to ignore an error that sometimes pops up here. For more info check out this link
url_info = 'http://stackoverflow.com/questions/20625582/how-to-deal-with-this-pandas-warning'
# In[55]:
titanic_df.head()
# In[56]:
# Now let's get a simple visualization!
sns.catplot(x='Alone',kind='count',data=titanic_df,palette='Blues')
# In[57]:
# Let's start by creating a new column for legibility purposes through mapping (Lec 36)
titanic_df["Survivor"] = titanic_df.Survived.map({0: "no", 1: "yes"})
# Let's just get a quick overall view of survied vs died.
sns.catplot(x='Survivor',kind='count',data=titanic_df,palette='Set1')
# Looking at the graph, it looks that more people died than survived. The ratio is almost double.
# In[65]:
# Let's use a factor plot again, but now considering class
sns.factorplot(x='Pclass',y='Survived',data=titanic_df)
# In[68]:
# Let's use a factor plot again, but now considering class and gender
sns.factorplot(x='Pclass',y='Survived',hue='person',data=titanic_df)
# In[94]:
# Let's use a linear plot on age versus survival
sns.lmplot('Age','Survived',data=titanic_df)
# In[95]:
# Let's use a linear plot on age versus survival
sns.lineplot('Age','Survived',data=titanic_df)
# In[98]:
# Let's use a linear plot on age versus survival using hue for class seperation
generations=[10,20,40,60,80]
sns.lmplot('Age','Survived',hue='Pclass',data=titanic_df,palette='winter',x_bins=generations)
# In[102]:
sns.lmplot('Age','Survived',hue='Sex',data=titanic_df,palette='winter',x_bins=generations)
# In[ ]:
# In[ ]:
# In[ ]:
# In[ ]:
# In[ ]:
# In[ ]:
# In[ ]: