0
点赞
收藏
分享

微信扫一扫

python采集泰坦尼克号基于不同维度的生存率数据,如性别、年龄、客舱等级

泰坦尼克号生存率数据分析

1. 引言

泰坦尼克号(RMS Titanic)是一艘英国豪华邮轮,于1912年首次航行时在大西洋中撞上冰山而沉没。这场灾难导致了大量的生命损失,成为了世界历史上最著名的船难之一。在这场悲剧中,有一些因素可以被用于预测乘客的生存率,如性别、年龄以及乘客所在的客舱等级。本文将使用Python采集泰坦尼克号的生存率数据,并通过不同维度的分析来揭示一些有趣的发现。

2. 数据采集

为了进行数据分析,我们需要先采集泰坦尼克号的乘客数据。在Python中,我们可以使用pandas库来处理和分析数据。首先,我们需要导入该库并读取数据集。

import pandas as pd

# 读取数据集
df = pd.read_csv('titanic.csv')

数据集中包含了乘客的信息,包括性别、年龄、客舱等级以及是否生还等字段。

3. 性别维度的生存率分析

性别可能是影响生存率的一个重要因素。为了分析不同性别的生存率,我们可以使用饼状图来展示数据。

import matplotlib.pyplot as plt

# 统计生还和遇难人数
survived_gender = df[df['Survived'] == 1]['Sex'].value_counts()
dead_gender = df[df['Survived'] == 0]['Sex'].value_counts()

# 绘制饼状图
plt.pie(survived_gender, labels=survived_gender.index, autopct='%1.1f%%')
plt.title('Survival Rate by Gender')
plt.show()

下图展示了不同性别的生存率数据。

pie
title Survival Rate by Gender
"Female": 68.1
"Male": 31.9

从图中可以看出,女性的生存率远高于男性,占比达到了68.1%。

4. 年龄维度的生存率分析

年龄也是一个可能影响生存率的重要因素。为了分析不同年龄段的生存率,我们可以使用直方图来展示数据。

# 绘制直方图
plt.hist([df[df['Survived'] == 1]['Age'], df[df['Survived'] == 0]['Age']], bins=20, label=['Survived', 'Dead'])
plt.xlabel('Age')
plt.ylabel('Count')
plt.title('Survival Rate by Age')
plt.legend()
plt.show()

下图展示了不同年龄段的生存率数据。

gantt
dateFormat YYYY-MM-DD
title Survival Rate by Age

section Survived
0-10:active, 38
10-20, 41
20-30, 77
30-40, 73
40-50, 34
50-60, 20
60-70, 6
70-80, 1

section Dead
0-10, 24
10-20, 61
20-30, 143
30-40, 94
40-50, 55
50-60, 28
60-70, 13
70-80, 4

从图中可以看出,年轻人(20-30岁)的生存率相对较高,而儿童和老年人的生存率较低。

5. 客舱等级维度的生存率分析

乘客所在的客舱等级也可能与生存率有关。为了分析不同客舱等级的生存率,我们可以使用柱状图来展示数据。

# 统计生还和遇难人数
survived_class = df[df['Survived'] == 1]['Pclass'].value_counts()
dead_class = df[df['Survived'] == 0]['Pclass'].value_counts()

# 绘制柱状图
plt.bar(survived_class.index, survived_class.values, label='Survived')
plt.bar(dead_class
举报

相关推荐

0 条评论