泰坦尼克号数据_泰坦尼克号数据分析报告
1、探索问题
本文主要探寻泰坦尼克号乘客的生还率和各因素(船舱等级、年龄、性别、票价、上船港口等)的关系。
#!usr/bin/env python #-- coding:utf-8 -- import numpy as np import pandas as pd from pandas import Series,DataFrame import matplotlib.pyplot as plt import matplotlib as mpl import math %matplotlib inline2、调查数据 #利用Pandas自带的read_csv()函数加载数据 titanic_data = pd.read_csv('titanic_data.csv') titanic_data.head() #查看各列数据情况 http://titanic_data.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
可以看出,数据共有891行,12列,其中3列有缺失数据,Age列缺失177行,Cabin列缺失687行,缺失严重,Embarked列缺失2行。Name、Ticket、Cabin对探索问题无帮助。
#删除无用数据 titanic_data=titanic_data.drop(['Name','Ticket','Cabin'],axis=1) http://titanic_data.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 891 entries, 0 to 890
Data columns (total 9 columns):
PassengerId 891 non-null int64
Survived 891 non-null int64
Pclass 891 non-null int64
Sex 891 non-null object
Age 714 non-null float64
SibSp 891 non-null int64
Parch 891 non-null int64
Fare 891 non-null float64
Embarked 889 non-null object
dtypes: float64(2), int64(5), object(2)
memory usage: 62.7+ KB
检查是否有重复数据 if ~titanic_data.duplicated().any(): print('titanic_data表格无重复数据') else: print('titanic_data有重复数据')
titanic_data表格无重复数据处理缺失数据
处理Age列缺失数据,用“均值”进行填充, 并将年龄数据修正为整型
titanic_data['Age'].fillna(titanic_data.Age.mean(),inplace=True) def may_int(i): return int(i) titanic_data['Age']=titanic_data['Age'].apply(may_int)
处理Embarked列的数据,用Embarked列的众数填充两个缺失空格
print(titanic_data['Embarked'].value_counts())
S 646
C 168
Q 77
Name: Embarked, dtype: int64
#Emvarked列用众数填充缺失项 titanic_data['Embarked'].fillna('S',inplace=True) http://titanic_data.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 891 entries, 0 to 890
Data columns (total 9 columns):
PassengerId 891 non-null int64
Survived 891 non-null int64
Pclass 891 non-null int64
Sex 891 non-null object
Age 891 non-null int64
SibSp 891 non-null int64
Parch 891 non-null int64
Fare 891 non-null float64
Embarked 891 non-null object
dtypes: float64(1), int64(6), object(2)
memory usage: 62.7+ KB3、展示数据****泰坦尼克号乘客生还情况
In [54]:
Survived_count=titanic_data['Survived'].value_counts() print(Survived_count) #创建一个作条形图、饼状图的函数 def Bar_pie(Survived_count): #生还遇难人数条形图 #创建一个figure fig_S=plt.figure(figsize=(10,5)) #创建一个subplot ax_s=fig_S.add_subplot(121) #用plot()函数作条形图 Survived_count.plot(kind='bar') #标签 label=Survived_count.index ax_s.set_xticklabels(label,rotation="horizontal") #添加标题 name=Survived_count.name ax_s.set_title('Bar plot of %s'%name) #添加x、y轴标签 ax_s.set_ylabel('number of people') ax_s.set_xlabel('%s'%name) #作生还、遇难比例饼状图 plt.subplot(122) plt.pie(x=Survived_count,labels=label,autopct='%3.1f %%', shadow=False,labeldistance=1.1,startangle=90,pctdistance=0.6) #添加标题 plt.title('Pie plot of %s'%name) Bar_pie(Survived_count)
0 549
1 342
Name: Survived, dtype: int64

在891名乘客中遇难者共计549人,在遇难者中占比为61.6%,存活者共计342人,在总人数中占比为38.4%。各等级舱位的乘客数量

三等舱的乘客占比最高(55.1%),其后是一等舱(24.2%),二等舱的乘客占比最少(20.7%)。从男女乘客构成情况来看:男性占577人、女性则有314人

男乘客共计577人占比64.8%女乘客则有314人占比35.2%年龄分布情况分析
In [84]:
#Age分布直方图 #创建figure和subplot并绘制直方图 fig_Age = plt.figure(figsize=(10, 5)) ax_Age = fig_Age.add_subplot(1, 2, 1) titanic_data['Age'].hist(bins=10, color='g', alpha=0.3, grid=False) #设置x轴刻度标签 将x轴刻度设定为从0到100每隔10个单位显示 ax_Age.set_xticks([i*10 for i in range(11)]) #添加图表标题 并设置x轴和y轴标签 ax_Age.set_title('Age的直方图') ax_Age.set_xlabel('Age') ax_Age.set_ylabel('人数') #绘制箱线图 设置图表布局 fig_Age.tight_layout() plt.subplot(122) titanic_data['Fare'].boxplot(column='Fare', showfliers=False) #设置y轴标签 并添加图表标题 ax_Age.set_ylabel('人数') plt.title('Fare的箱线图')

titanic_data['Age'].describe()
count 891.000000
mean 29.544332
std 13.013778
min 0.000000
25% 22.000000
50% 29.000000
75% 35.000000
max 80.000000
Name: Age, dtype: float64
乘客年龄大概成正态分布,平均年龄29岁多,最大的80岁,最小的不到1岁(利用int()取整,不到1岁的为0).兄弟姐妹、配偶在船上的乘客分布情况条形图
#创建figure、subplot,用plot()作柱状图 fig_SibSp=plt.figure(figsize=(10,5)) ax_SibSp=fig_SibSp.add_subplot(1,2,1) SibSp_count=titanic_data['SibSp'].value_counts() SibSp_count.plot(kind='bar') #添加标题,x轴标签,y轴标签 ax_SibSp.set_title('Bar plot of SibSp') ax_SibSp.set_xlabel('number of SibSp') ax_SibSp.set_ylabel('number of people') #拥有各数量的兄弟姐妹、配偶的乘客比例条形图 plt.subplot(122) SibSp_count.div(SibSp_count.sum()).plot(kind='bar') #添加标题,x、y轴标签 plt.title('Ratio of people in SibSp') plt.xlabel('SibSp') plt.ylabel('ratio')

在船上没有兄弟姐妹或配偶同行的乘客数量较多,在全部乘客中占比为68.2%。父母与子女在船上的客流量分布柱状图能够清晰展示这一趋势。通过Parch属性统计各组别人数并生成柱状图(fig_Parch, ax_Parch),我们能够直观呈现父母与子女随船出行的人数分布情况。进一步地,在同一个布局中(plt.subplot(1,2,2)),我们展示了各组别人数占总人数的比例分布(Parch_count.div(Parch_count.sum())),这一比例条形图能够帮助我们更好地理解不同家庭结构随船出行的比例分布情况。

未携带父母及子女的乘客较多(占76.1%),其次为携带一个家庭成员(占13.2%),再次为携带两个家庭成员(占9%)。票价分布情况

titanic_data['Fare'].describe()
count 891.000000
mean 32.204208
std 49.693429
min 0.000000
25% 7.910400
50% 14.454200
75% 31.000000
max 512.3292
Name: Fare, dtype: float64

各港口上下船人数共有三个(S、C、Q),其中在S港上下船人数较多(占72.5%),其次为C港(18.9%),再次之为Q港(8.6%)。3、各因素对乘客生还情况的影响:各等级舱室中的乘客生还情况存在显著差异。各等级船舱中乘客的生还情况:即不同舱室中存活与死亡的人数统计情况。

titanic_data.groupby('Pclass')['Survived'].mean()
Pclass
1 0.629630
2 0.472826
3 0.242363
Name: Survived, dtype: float64
一等船舱216人,136人生还,生还率63%;
二等船舱184人,87人生还,生还率47.3%;
三等船舱491人,119人生还,生还率24.2%性别对生还率的影响情况
#利用groupby()整理数据 Sex_Survived_data=titanic_data.groupby(['Sex','Survived'])['Survived'].count().unstack() print(Sex_Survived_data) #作条形图 Sex_Survived_data.plot(kind='bar',stacked=True) #添加标题、y轴标签 plt.title('Sex & Survived') plt.ylabel('Number of people')
Survived 0 1
Sex
female 81 233
male 468 109

根据给定数据集 titanic_data 进行分析可得以下结论
按性别分组计算乘客存活率
数据处理方法
具体操作步骤如下
最后调用自定义函数 Bar_2pie 对 SibSp_Survived_data_count 进行可视化呈现
具体实现细节
运行结果分析


无兄弟姐妹配偶的乘客共有608人,210人生还,生还率34.5%;
有兄弟姐妹配偶的乘客共有283人,132人生还,生还率46.6%.有、无父母孩子的乘客的生存情况
#用groupby()函数整理数据 Parch_Survived_data=titanic_data.groupby(['Parch','Survived'])['Survived'].count().unstack() Parch_Survived_data_count=Parch_Survived_data.loc[:1,] Parch_Survived_data_count.loc[1]=Parch_Survived_data.loc[1:].sum() #用Bar_2pie()函数作一个堆叠条形图,两个饼状图 Bar_2pie(Parch_Survived_data_count)
Survived 0 1
Parch
0 445.0 233.0
1 104.0 109.0


未携带父母的孩子共有678位乘客,在这次灾难中只有233人存活下来,其生还率为34.4%;携带父母的孩子共有213位乘客,在这次灾难中只有109人存活下来(生还率51.2%)。各港口乘客生存情况分布柱状图

根据上船港口分组计算的平均生存率为:
C港乘客共168人中55.4%存活;
Q港乘客共77人中约39%存活;
S港乘客中有近三成(约33.9%)存活。
此外观察发现:
随着车费价格的提高 存活比例逐渐降低;
在票价较高的等级中 存活比例相对较低;
而最便宜票段却显示出相对较高的存活比例。

存活者的平均票 fare price for survivors is notably higher than that of victims. Fare prices are closely associated with survival likelihood. Age distribution box plot of survivors vs victims

生存者与遇难者的平均年龄大致相当,在此过程中生存者的年龄分布范围相对较为宽泛。然而,在泰坦尼克号失事调查中发现,在所有乘客中无论是男性还是女性的存活概率均呈现出显著差异性
- 泰坦尼克号上有三种船舱类型:一等、二等、三等。一等船舱216人,136人生还,生还率63%;二等船舱184人,87人生还,生还率47.3%;三等船舱491人,119人生还,生还率24.2%.可见船舱等级越高,生还率越大。
- 船上有乘客891人,其中女乘客314人,233人生还,生还率74.2%;男乘客577人,109人生还,生还率18.9%.可见女性乘客更易生还。
- 船上无兄弟姐妹配偶的乘客共有608人,210人生还,生还率34.5%;有兄弟姐妹配偶的乘客共有283人,132人生还,生还46.6%.可见有兄弟姐妹配偶的乘客更易生还。
- 船上无父母孩子的乘客共有678人,233人生还,生还率34.4%;有父母孩子的乘客共有213人,109人生还,生还率51.2%.可见有父母孩子的乘客更易生还。
- 由891个样本数据可知乘客上船港口有三个:C、Q、S。其中C港口上船的乘客有168人,93人生还,生还率55.4%;Q港口上船的乘客有77人,30人生还,生还率39%;S港口上船的乘客有646人,219人生还,生还率33.9%.可见生还率还与上船港口有关。
- 票价与生还有一定相关性,生还者的平均票价要比遇难者的高。
- 年龄与生还也有一定相关性,生还者的年龄范围比遇难者年龄范围更大一些。
分析限制
- 此份数据不是泰坦尼克号全体乘客的数据。
- 可能存在其他因素影响着乘客的生存情况。如救援反应迟缓、乘客健康状况以及船上配备的急救设备数量等。
- 我们无法确切得知数据的真实情况。
