Kaggle獲得了一份泰坦尼克號乘客的數(shù)據(jù),來進(jìn)行分析蓉冈,哪些因素,會讓乘客的生還率更高
提出問題
影響乘客生還的因素很多轩触,這里只對乘客的性別寞酿、年齡、乘客等級脱柱、這三個因素感興趣伐弹,
看看這四個因素是否會影響乘客的生還率。
- 1.性別是否會影響生還率
- 2.年齡是否會影響生還率
- 3.乘客等級會否會影響乘客率
- 4.性別和艙位共同對生還率的影響
- 5.年紀(jì)和性別共同對生還率的影響
- 6.年紀(jì)和等級共同對生還率的影響
這里榨为。乘客的性別惨好、年齡、等級随闺、是三個自變量日川,生還率是因變量
數(shù)據(jù)加工
導(dǎo)入包
import numpy as np
import pandas as pd
from pandas import Series,DataFrame
import matplotlib.pyplot as plt
from __future__ import division
from scipy import stats
import seaborn as sns
###首先導(dǎo)入各種模塊
###讓圖片在ipython notebook上直接顯示
%matplotlib inline
加載數(shù)據(jù)
path='/Users/zhongyaode/Desktop/udacity—data/'
df=pd.read_csv(path+'titanic-data.csv')
熟悉數(shù)據(jù)
先看看數(shù)據(jù)里有哪些信息,這些信息是怎樣的格式
- PassengerId:乘客ID
- Survived:是否獲救矩乐,用1和Rescued表示獲救,用0或者not saved表示沒有獲救
- Pclass:乘客等級龄句,“1”表示Upper,“2”表示Middle散罕,“3”表示Lower
- Name:乘客姓名
- Sex:性別
- Age:年齡
- SibSp:乘客在船上的配偶數(shù)量或兄弟姐妹數(shù)量)
- Parch:乘客在船上的父母或子女?dāng)?shù)量
- Ticket:船票信息
- Fare:票價
Cabin:是否住在獨(dú)立的房間分歇,“1”表示是,“0”為否
embarked:表示乘客上船的碼頭距離泰坦尼克出發(fā)碼頭的距離欧漱,數(shù)值越大表示距離越遠(yuǎn)
#查看前五行數(shù)據(jù)卿樱。了解數(shù)據(jù)包含的信息,
df.head()
屏幕快照 2017-05-22 下午10.45.35.png
#查看各字段的數(shù)據(jù)類型
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 891 entries, 0 to 890
Data columns (total 12 columns):
PassengerId 891 non-null int64
Survived 891 non-null int64
Pclass 891 non-null int64
Name 891 non-null object
Sex 891 non-null object
Age 714 non-null float64
SibSp 891 non-null int64
Parch 891 non-null int64
Ticket 891 non-null object
Fare 891 non-null float64
Cabin 204 non-null object
Embarked 889 non-null object
dtypes: float64(2), int64(5), object(5)
memory usage: 83.6+ KB
#查看數(shù)據(jù)的摘要信息
df.describe()
屏幕快照 2017-05-22 下午10.45.47.png
從數(shù)據(jù)摘要中可以看出硫椰。乘客的生還率大約在38%,超越50的乘客在3等級萨蚕,乘客的平均年齡在30歲左右靶草,普遍比較年輕
數(shù)據(jù)清洗
處理缺失值
#Embarked有非常少的兩個缺失值,這里用'S'填充
df['Embarked']=df['Embarked'].fillna('S')
#處理Age的缺失值岳遥,Age是連續(xù)數(shù)據(jù)奕翔,這里用平均值填充缺失值
age_mean=df['Age'].mean()
df['Age']=df['Age'].fillna(age_mean)
處理性別數(shù)據(jù)
#這里把性別數(shù)據(jù)值字符串不便于計算換成數(shù)值,
#用1代表男性浩蓉,用0代表女性派继,將性別數(shù)值化
def sex_value(Sex):
if Sex=='male':
return 1
else:
return 0
df['Sex']=df['Sex'].apply(lambda x:sex_value(x))
數(shù)據(jù)探索
#獲取生還乘客的數(shù)據(jù)
survives_passenger_df=df[df['Survived']==1]
#定義幾個常用的方法
#按照xx對乘客進(jìn)行分組宾袜,計算每組的人數(shù)
def xx_group_all(df,xx):
#按照xx對乘客進(jìn)行分組后 ,每個組的人數(shù)
return df.groupby(xx)['PassengerId'].count()
#計算每個組的生還率
def group_passenger_survived_rate(xx):
#按xx對乘客進(jìn)行分組后每個組的人數(shù)
group_all=xx_group_all(df,xx)
#按xx對乘客進(jìn)行分組后每個組生還者的人數(shù)
group_survived_value=xx_group_all(survives_passenger_df,xx)
#按xx對乘客進(jìn)行分組后驾窟,每組生還者的概率
return group_survived_value/group_all
#輸出餅圖
def print_pie(group_data,title):
group_data.plt.pie(title=title,figsize=(6,6),autopct='%.2f%%'\
,startangle=90,legend=True)
#輸出柱狀圖
def print_bar(data,title):
bar=data.plot.bar(title=title)
for p in bar.patches:
bar.annotate('%.2f%%'%(p.get_height()*100),(p.get_x()*1.005\
,p.get_height()*1.005))
性別對生還率的影響
#不同性別對生還率的影響
df_sex1=df['Sex'][df['Survived']==1]
df_sex0=df['Sex'][df['Survived']==0]
plt.hist([df_sex1,df_sex0],
stacked=True,
label=['Rescued','not saved'])
plt.xticks([-1,0,1,2],[-1,'F','M',2])
plt.legend()
plt.title('Sex_Survived')
<matplotlib.text.Text at 0x118427d90>
output_18_1.png
看出全體乘客中男性占了大部分庆猫,但是生還乘客中女性占了大部分;
- 得出結(jié)論:女性的生還概率比男性的更高
乘客等級對生還率的影響
#不同等級對生還率的影響
df_sex1=df['Pclass'][df['Survived']==1]
df_sex0=df['Pclass'][df['Survived']==0]
plt.hist([df_sex1,df_sex0],
stacked=True,
label=['Rescued','not saved'])
plt.xticks([1,2,3],['Upper','Middle','lower'])
plt.legend()
plt.title('Pclass_Survived')
<matplotlib.text.Text at 0x11807edd0>
output_21_1.png
全體乘客中l(wèi)ower等級的乘客超過了一半绅络,生還乘客中upper等級的人最多月培,
對比各個等級的死亡人數(shù)和生還人數(shù):
- **可以得出結(jié)論:Upper等級生還概率大于Middle、lower的生存概率恩急,等級越好生還概率越好
年齡對生還率的影響
#不同年齡對生還率的影響
df_sex1=df['Age'][df['Survived']==1]
df_sex0=df['Age'][df['Survived']==0]
plt.hist([df_sex1,df_sex0],
stacked=True,
label=['Rescued','not saved'])
#plt.xticks([1,2,3],['Upper','Middle','lower'])
plt.legend()
plt.title('title')
plt.title('Age_Survived')
<matplotlib.text.Text at 0x118698690>
output_24_1.png
#不同年齡段對生還率的影響elderly杉畜,child,youth
#年齡數(shù)據(jù)進(jìn)行處理,0-18為child(少年)衷恭,18-40為youth(青年)此叠,40-80為elderly(老年)
def age_duan(age):
if age<=18:
return 1
elif age<=40:
return 2
else:
return 3
df['Age']=df['Age'].apply(lambda x:age_duan(x))
df_sex1=df['Age'][df['Survived']==1]
df_sex0=df['Age'][df['Survived']==0]
plt.hist([df_sex1,df_sex0],
stacked=True,
label=['Rescued','not saved'])
plt.xticks([1,2,3],['child','youth','elderly'])
plt.legend()
plt.title('Age_Survived')
<matplotlib.text.Text at 0x11854e410>
output_26_1.png
全部乘客中大部分人否在30歲左右,而0-10的生還率比其他年齡段都要高
- 結(jié)論:0-10歲的生還率率最高随珠,20-40之間的生還人數(shù)最多
多因素分析
性別和乘客等級共同對生還率的影響
print_bar(group_passenger_survived_rate(['Sex','Pclass']),'Sex_Pclass_Survived')
output_30_0.png
可以看到灭袁,對生還率的影響性別>乘客等級,其次是乘客等及對生還率的影響是1>2>3等姐
性別和年紀(jì)對生還率的影響
dd0=df[['Age','Sex','Pclass']]
dd11=df[['Age','Sex','Pclass']][df['Survived']==1]
c=Pclass_survived_all(dd11,['Age','Sex','Pclass'])
dd0['Sex'].count()
891
#按Pclass分組計算每組的人數(shù)
def Pclass_survived_all(data,Pclass):
return data.groupby(Pclass)['Sex'].count()
#按Pclass分組計算每組的生還率
def Pclass_survived_probability(data):
#計算每組生還者的人數(shù)
groupby_survived=Pclass_survived_all(dd11,data)
#計算每組的總?cè)藬?shù)
groupby_survived_all=Pclass_survived_all(dd0,data)
return groupby_survived/groupby_survived_all
print_bar(Pclass_survived_probability(['Sex','Age']),'Sex_Sge_Survived')
output_36_0.png
可以看出牙丽,對生還率影響大的是性別简卧,女性>男性
其次少年的生還率大于青年和老年,青年跟老年的對生還率差不多
年齡和乘客等級共同對生還率的影響
#Age中用1表示少年,用2表示青年,用3表示老年
print_bar(Pclass_survived_probability(['Age','Pclass']),'age_pclass_Survivedd')
output_39_0.png
可以看出乘客的等級對生還率的影響>乘客年齡的影響
年齡越大生還率越小烤芦,乘客等級越差生還率越差
結(jié)論
通過分析举娩,可以看出對生還率影響最大的因素是乘客等級,其次是性別构罗,最后年齡段也對生化率有影響
分析的局限性
- 這里并沒有從統(tǒng)計上分析得出這些結(jié)果的偶然性铜涉,所以并不知道這里的結(jié)果是真正的差異造成的還是噪音造成的
- 年齡字段有一些缺失值,因?yàn)槭沁B續(xù)數(shù)據(jù)這里用的是全體乘客年齡的均值填充缺失值遂唧,這樣會縮小年齡之間的差異芙代,也會影響分析結(jié)果
結(jié)果的相關(guān)性
這里的數(shù)據(jù)并非通過試驗(yàn)得出,所以無法說自變量之間的因果性盖彭,只能說她們之間有相關(guān)性
參考文章![Microsoft Dynamics AX 技術(shù)博
]http://www.cnblogs.com/msdynax/p/6099814.html
[審閱參考意見]https://review.udacity.com/#!/reviews/521378