By K.Nikhil
The titanic dataset investigation is to determine what factors made it more likely that a person survived on the Titanic. class, sex, and age. The invistigation says higher class ,females-passengers had greatest possibility of surviving
import pandas as pd
from __future__ import division
import numpy as np
import matplotlib as mt
sinkingship=pd.read_csv("titanic-data.csv")
#CSV is in correct format so not changing any datatypes
#Croping the name to make dataframe to visualize easily
def cropname(name):
spliter = name.split(",")
return spliter[1]
sinkingship['Name'].apply(cropname)
#removing NAN age rows
cleanage = sinkingship[sinkingship['Age'].notnull()]
Twentyf= cleanage['Age'].quantile(q=0.25)
Seventyf= cleanage['Age'].quantile(q=0.75)
max = (Seventyf - Twentyf) * 1.5 + Seventyf
min = Twentyf - (Seventyf-Twentyf) * 1.5
clean_age = cleanage[(cleanage['Age'] > min) & (cleanage['Age'] < max)]
sinkingship.describe()
clean_age.describe()
totalpassengers = len(sinkingship)
statuscount=sinkingship.groupby('Survived')['PassengerId'].count()
survived = sinkingship[sinkingship["Survived"]==1]
notsurvived = sinkingship[sinkingship["Survived"]==0]
Totalsurvivedpassengers = statuscount[1]
Totalnonsurvivedpassengers = statuscount[0]
surviveperc = (Totalsurvivedpassengers*100)/totalpassengers
deathperc = (Totalnonsurvivedpassengers*100)/totalpassengers
#Class wise passengers
classcount = sinkingship.groupby('Pclass')['PassengerId'].count()
class_1_passengers = classcount[1]
class_2_passengers = classcount[2]
class_3_passengers = classcount[3]
#The Panda dataframe of classes counts of SUrvived /death passengers
theclassesdetial=pd.crosstab(sinkingship['Pclass'],sinkingship['Survived'])
type(theclassesdetial)
class_1_passengers_survived=theclassesdetial[0][1]
class_1_passengers_death=theclassesdetial[1][1]
class1_survived_percentage=(class_1_passengers_survived*100)/class_1_passengers
class1_death_percentage=(class_1_passengers_death*100)/class_1_passengers
total_class1_survived_percentage=(class_1_passengers_survived*100)/totalpassengers
total_class1_death_percentage=(class_1_passengers_death*100)/totalpassengers
class_2_passengers_survived=theclassesdetial[0][2]
class_2_passengers_death=theclassesdetial[1][2]
class2_survived_percentage=(class_2_passengers_survived*100)/class_2_passengers
class2_death_percentage= (class_2_passengers_death*100)/class_2_passengers
total_class2_survived_percentage=(class_2_passengers_survived*100)/totalpassengers
total_class2_death_percentage=(class_2_passengers_death*100)/totalpassengers
class_3_passengers_survived=theclassesdetial[0][3]
class_3_passengers_death=theclassesdetial[1][3]
class3_survived_percentage=(class_3_passengers_survived*100)/class_3_passengers
class3_death_percentage= (class_3_passengers_death*100)/class_3_passengers
total_class3_survived_percentage=(class_3_passengers_survived*100)/totalpassengers
total_class3_death_percentage=(class_3_passengers_death*100)/totalpassengers
nmales_survived=len(survived[survived['Sex'] == 'male'])
nmales_died=len(notsurvived[notsurvived['Sex'] == 'male'])
nfemales_survived=len(survived[survived['Sex'] == 'female'])
nfemales_died=len(notsurvived[notsurvived['Sex'] == 'female'])
survivorages = clean_age[clean_age['Survived'] == 1]
nonsurvivorages = clean_age[clean_age['Survived'] == 0]
def printthesinkingstat():
print "Totalpassengers traveling :"+str(totalpassengers)
print "Total Survivers :"+str(Totalsurvivedpassengers)
print "Total Deaths :"+str(Totalnonsurvivedpassengers)
print "survivers percentage :"+str(surviveperc)
print "deaths percentage :"+str(deathperc)
print "class_1_passengers :"+str(class_1_passengers)
print "class_1_passengers_survived :"+str(class_1_passengers_survived)
print "class_1_passengers_death :" +str(class_1_passengers_death)
print "class_1_passengers percentage:"+str((class_1_passengers*100)/totalpassengers)
print "class_1_survived_percentage :"+str(class1_survived_percentage)
print "class_1_death_percentage :"+str(class1_death_percentage)
print "Total_class_1_survived_percentage :"+str(total_class1_survived_percentage)
print "total_class_1_death_percentage :"+str(total_class1_death_percentage)
print "class_2_passengers :"+str(class_2_passengers)
print "class_2_passengers_survived :"+str(class_2_passengers_survived)
print "class_2_passengers_death :"+str(class_2_passengers_death)
print "class_2_passengers percentage:"+str((class_2_passengers*100)/totalpassengers)
print "class_2_survived_percentage :"+str(class2_survived_percentage)
print "class_2_death_percentage :"+str(class2_death_percentage)
print "Total_class_2_survived_percentage :"+str(total_class2_survived_percentage)
print "total_class_2_death_percentage :"+str(total_class2_death_percentage)
print "class_3_passengers :"+str(class_3_passengers)
print "class_3_passengers_survived :"+str(class_3_passengers_survived)
print "class_3_passengers_death :"+str(class_3_passengers_death)
print "class_3_passengers percentage:"+str((class_3_passengers*100)/totalpassengers)
print "class_3_survived_percentage :"+str(class3_survived_percentage)
print "class_3_death_percentage :"+str(class3_death_percentage)
print "Total_class_3_survived_percentage :"+str(total_class3_survived_percentage)
print "total_class_3_death_percentage :"+str(total_class3_death_percentage)
print "Number of males survived :"+str(nmales_survived)
print "No of males died :"+str(nmales_died)
print "Number of females survived :" + str(nfemales_survived)
print "No of females died :" + str(nfemales_died)
printthesinkingstat()
import plotly.graph_objs as go
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
init_notebook_mode(connected=True)
trace1 = go.Bar(
x=['CLASS1', 'CLASS2', 'CLASS3'],
y=[class_1_passengers_survived,class_2_passengers_survived, class_3_passengers_survived],
name='Survived'
)
trace2 = go.Bar(
x=['CLASS1', 'CLASS2', 'CLASS3'],
y=[class_1_passengers_death,class_2_passengers_death,class_3_passengers_death],
name='Death'
)
data = [trace1, trace2]
layout = go.Layout(
barmode='group',
title='Survivors/Deaths plotting based on different Classes of passengers',
xaxis=dict(
title='Different classes of Passengers',
titlefont=dict(
family='Courier New, monospace',
size=18,
color='#7f7f7f'
)
),
yaxis=dict(
title='Frequency-count',
titlefont=dict(
family='Courier New, monospace',
size=18,
color='#7f7f7f'
)
)
)
fig = go.Figure(data=data, layout=layout)
iplot(fig, filename='grouped-bar')
From the above stats we can say that class1 is 24% of total passengers strength where 62.9% of 24% total passengers are asurvived class2 is 20.6% of total passenngers strength where 47.2% of 20.6% total passengers are survived class3 is 55.10% of total passengers strength where 24% of 55.10% are survived the above data shows that the higher the class the better the survival chance From below chisquare and pvalue we can say that class plays a crucial role in surviving factor
import scipy.stats
class_1_passengers_survived = 80
class_2_passengers_survived = 97
class_3_passengers_survived = 372
class_1_passengers_died= 136
class_2_passengers_died= 87
class_3_passengers_died=119
obs = np.array([[class_1_passengers_survived,class_2_passengers_survived,class_3_passengers_survived], [class_1_passengers_died, class_2_passengers_died, class_3_passengers_died]])
chisquare, p, dof, ex = scipy.stats.chi2_contingency(obs, correction=False)
print "pvalue: ", p
print "Chisquaredscore: ", chisquare
import plotly.graph_objs as go
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
init_notebook_mode(connected=True)
fig = {
"data": [
{
"values": [nmales_survived,nmales_died],
"labels": [
"Male-Survived",
"Males-died"
],
"domain": {"x": [0, .48]},
"name": "GHG Emissions",
"hoverinfo":"label+percent+name",
"hole": .4,
"type": "pie"
},
{
"values": [nfemales_survived,nfemales_died],
"labels": [
"Females-Survived",
"Females-died"
],
"textposition":"inside",
"domain": {"x": [.52, 1]},
"hoverinfo":"label+percent+name",
"hole": .4,
"type": "pie"
}],
"layout": {
"title":"Survivors/Deaths for males and females",
"annotations": [
{
"font": {
"size": 20
},
"showarrow": False,
"text": "Males",
"x": 0.20,
"y": 0.5
},
{
"font": {
"size": 20
},
"showarrow": False,
"text": "Females",
"x": 0.8,
"y": 0.5
}
]
}
}
iplot(fig)
To test the statistical difference between the genders, we conduct a difference in proportions test using a
I am following two titled test by spliiting the alpha level AlphaLevel-Value: 0.05(5%)
We Will use two-titled to test the likelyhood weather it is likely or unlikely
Z-critical value of AlphaLevel of 2.5% on negative side is -1.96(According to table) Z-critical value of AlphaLevel of 2.5% on Positive side is 1.96(According to table)
H0:Null hypothesis HA:ALternative hypothesis
H0: male_survivals= female_survivals HA: male_survivals != female_survivals
p = ( male_survivals x total_males + female_survivals x total_females) / (total_males + total_females) p = (0.188908145581 577 + 0.742038216561 314) / (314 + 577) p = 0.384
s = sqrt( p (1-p) [(1/total_males) + (1/total_females)] ) s= sqrt( 0.384 (1-0.384) [(1/577) + (1/314)] ) s= 0.0341
z = ( male_survivals - female_survivals) / s z = (0.188908145581 - 0.742038216561) / 0.0341 z = -16.2
Since:-16.2 < -1.96, we are rejecting the null hypothesis. Indeed, statistical evidence shows that if you were a female you had a higher chance of surviving.
import plotly.graph_objs as go
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
init_notebook_mode(connected=True)
x0 = survivorages['Age']
x1 = nonsurvivorages['Age']
Survivers = go.Histogram(
x=x0,
name='Survivers',
opacity=0.50,
)
Died_passengers = go.Histogram(
x=x1,
name='Died_passengers',
opacity=0.25
)
data = [Survivers,Died_passengers]
layout = go.Layout(
barmode='overlay',
title='Plot based on Survivors and deaths by age factor',
xaxis=dict(
title='Passenger Age',
titlefont=dict(
family='Courier New, monospace',
size=18,
color='#7f7f7f'
)
),
yaxis=dict(
title='Frequency-count',
titlefont=dict(
family='Courier New, monospace',
size=18,
color='#7f7f7f'
)
)
)
fig = go.Figure(data=data, layout=layout)
iplot(fig, filename='grouped-bar')