大数据第一课(满分作业)——泰坦尼克号生存者预测(Titanic - Machine Learning from Disaster)

Posted know634

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了大数据第一课(满分作业)——泰坦尼克号生存者预测(Titanic - Machine Learning from Disaster)相关的知识,希望对你有一定的参考价值。

大数据第一课(满分作业)——泰坦尼克号生存者预测(Titanic - Machine Learning from Disaster)

1 项目背景

1.1 The Challenge

The sinking of the Titanic is one of the most infamous shipwrecks in history.
On April 15, 1912, during her maiden voyage, the widely considered “unsinkable” RMS Titanic sank after colliding with an iceberg. Unfortunately, there weren’t enough lifeboats for everyone onboard, resulting in the death of 1502 out of 2224 passengers and crew.
While there was some element of luck involved in surviving, it seems some groups of people were more likely to survive than others.
In this challenge, we ask you to build a predictive model that answers the question: “what sorts of people were more likely to survive?” using passenger data (ie name, age, gender, socio-economic class, etc).

1.2 What Data Will I Use in This Competition?

In this competition, you’ll gain access to two similar datasets that include passenger information like name, age, gender, socio-economic class, etc. One dataset is titled ‘train.csv’ and the other is titled ‘test.csv’.
Train.csv will contain the details of a subset of the passengers on board (891 to be exact) and importantly, will reveal whether they survived or not, also known as the “ground truth”.
The ‘test.csv’ dataset contains similar information but does not disclose the “ground truth” for each passenger. It’s your job to predict these outcomes.
Using the patterns you find in the train.csv data, predict whether the other 418 passengers on board (found in test.csv) survived.
Check out the “Data” tab to explore the datasets even further. Once you feel you’ve created a competitive model, submit it to Kaggle to see where your model stands on our leaderboard against other Kagglers.

2 数据获取

从网上https://www.kaggle.com/competitions/titanic/data下载好数据"train.csv"和"test.csv"还有"gender_submission.csv"之后,如图 1所示,接下来利用Excel和Python语句进行数据总览。

3 数据分析

3.1 数据字段分析

"train.csv"的数据字段分析如下表所示。

另外,"test.csv"文件中不包含人员存活的情况。

3.2 导入数据

Python版本3.10(Anaconda3环境)、Python IDE PyCharm(Professional 2022.1)
将Python文件建立在"*.csv"文件的同一目录下,代码和运行结果如下

# 导入数据
train = pd.read_csv("./train.csv")
test = pd.read_csv("./test.csv")

# 查看前5条数据,看是否导入成功
print(train.head())
print()
print(test.head())
print()

3.3 数据清洗(预处理)

数据清洗之前需要明确要处理数据的字段类型,浏览上面输出的结果,共有11个字段(PassengerId和Ticket作为编号可以去除,不需考虑),剩余10个字段可以分为四类,如下:

  1. 数字类特征: 年龄,票价,兄弟姐妹配偶数量,父母小孩数量
  2. 类别特征: 性别,港口,船舱等级,是否存活
  3. 包含数字和字符的特征: 船票和船舱
  4. 文字类特征: 姓名

3.3.1 缺失值处理

# 获取数据的维度和查询各字段缺失值情况
print(train.info())
print()
print(train.isnull().sum())
print()


通过缺失值的查询,发现Age字段缺失大约到了20%,可以采用填充的方式进行处理,而Cabin字段的缺失值高达77%,可以直接删除,最后就是登船港口只有2条数据缺失可以直接删除也可以进行填充。接下来逐步进行缺失值处理。
首先是对于年龄字段,填充的具体方式可以查看数据的分布,一般对于年龄的分布都是属于右偏分布(就是均值大于中位数),可以通过describe()方法查询,输出结果如下:(可以发现均值比50%处的中位数大一点,稍稍右偏,可以采用中位数填充,也可以考虑使用均值填充)

print(train.describe())
print()


接下来进行中位数填充年龄,代码和结果如图所示:

# import sklearn
# print(sklearn.__version__)
# 1.0.2

from sklearn.impute import SimpleImputer
Imp = SimpleImputer(missing_values=np.nan, strategy='median')
New = Imp.fit_transform(train.Age.values.reshape(-1, 1))
train['Age_filled'] = New
print(train.describe())
print()
print(train.head())
print()
print(train.Age)
print()
print(train.Age_filled)
print()


接着就是对于一些不需要考虑和缺失值较多的字段进行删除,操作及输出结果如下:

train.drop(['Age', 'Ticket', 'Cabin', 'Name'], axis=1, inplace=True)
print(train.head())
print()


最后再次检查并且修正

print(train.isnull().sum())
print()
train.dropna(inplace=True)
print(train.isnull().sum())
print()


对于Enbarked字段的缺失值进行删除操作后,整个缺失值的清洗工作就完成了。

3.3.2 分类数据独热编码和数值数据分箱

使用乘客的ID作为索引,对于类别数据进行独热编码操作,输出结果如下:

train.set_index('PassengerId', inplace=True)
train = pd.get_dummies(train, columns=["Sex"], drop_first = True)
train = pd.get_dummies(train, columns=["Embarked"], drop_first = True)
print(train.head())
print()


接下来是对数值数据进行分箱操作,首先对于票价的字段,分箱操作及输出结果如下:(低于7.91标记为0,在7.9114.454之间标记为1,1.45431之间标记为2,31以上标记为3)
对于年龄字段进行分箱,操作及输出结果如下:(低于16标记为0,16-32之间标记为1,32-48之间标记为2,48-64之间标记为3,64以上标记为4)

train['Fare'] = train['Fare'].astype(int)
train.loc[train.Fare <= 7.91, 'Fare'] = 0
train.loc[(train.Fare > 7.91) & (train.Fare <= 14.454), 'Fare'] = 1
train.loc[(train.Fare > 14.454) & (train.Fare <= 31), 'Fare'] = 2
train.loc[(train.Fare > 31), 'Fare'] = 3

train['Age_filled'] = train['Age_filled'].astype(int)
train.loc[train['Age_filled'] <= 16, 'Age_filled'] = 0
train.loc[(train['Age_filled'] > 16) & (train['Age_filled'] <= 32), 'Age_filled'] = 1
train.loc[(train['Age_filled'] > 32) & (train['Age_filled'] <= 48), 'Age_filled'] = 2
train.loc[(train['Age_filled'] > 48) & (train['Age_filled'] <= 64), 'Age_filled'] = 3
train.loc[train['Age_filled'] > 64, 'Age_filled'] = 4

print(train.head())
print()

3.4 生存概率

print(train.Survived.value_counts())
print()
print(train.Survived.value_counts()/len(train))
print()
print(train.describe())
print()

3.5 性别与存活率之间的关系

直接按照Sex_male字段进行分组求均值,就可以得到男性和女性之间的存活率,输出结果如下:
总的生存率是0.38,其中女性的存活率是0.74,男性的存活率是0.19。
需要注意的是最后面的两个值的总和并不是1,因为生存率+死亡率是1,男性比例+女性比例是1,而男性生存率+女性生存率总和不一定是1。

3.6 字段之间的关联分析

可以用关联矩阵corr()和热力图heatmap()来展示,操作及输出结果如下:
只需要看一部分即可,图像是关于对角线对称,比如只看右上方的图像。
其中正相关的部分最明显的是船票价格和生存率达到0.3;
负相关部分,性别和生存率之间是 -0.54、 船票价格和船舱等级是 -0.66、船舱等级和生存率之间是 -0.34


3.7 性别与生存率之间的关系

分组条形图barplot(),对比不同组别的数值大小,来直观反映不同组别的差异情况,操作及输出结果如下:

更进一步可以进行细化,在条状图中分为存活率和死亡率,操作及输出结果如下:


3.8 船舱与生存率之间的关系

同理可得

使用阴影图kdeplot()

3.9 票价、年龄和生存率之间的关系

绘制的代码与上图基本一致,传入的字段改变一下即可,输出如下:(红色代表着死亡,蓝色代表着存活,可以发现,票价约低死亡率越高,票价越高,存活率就越高)

根据图形输出结果,可以发现死亡人员主要集中在16-32岁之间,而且在此年龄区间中,人员的死亡率要大于存活率,65岁以上的人员也是死亡率大于存活率,但是在16岁以下的人员存活率明显高于死亡率,剩下两个年龄段的对比不明显。

4 机器学习模型

4.1 准备模型

前面已经进行了数据的处理工作,下面直接就可以进行模型搭建,导入sklearn中常见的分类模型,构建分类器,放置各类模型。

4.2 模型准确度评分

【见全文】

4.3 模型选择及调用

【见全文】

4.3.1 数据处理

【见全文】

4.3.2 模型训练和结果输出

【见全文】

4.3.3 随机森林结果

【见全文】

4.3.4 XGB结果

【见全文】

5 参考文献

【见全文】

6 附录

【见全文】

【更多内容】

视频秒开(电脑端)

备用

备用(b站)

【全文(Word+PDF+Python原码)】

>>>我在这里(Word+PDF+Python原码)<<<

以上是关于大数据第一课(满分作业)——泰坦尼克号生存者预测(Titanic - Machine Learning from Disaster)的主要内容,如果未能解决你的问题,请参考以下文章

泰坦尼克号生存预测(python)

收藏第一课第二周作业-学会计算分类各种指标-超详细教程

利用python进行泰坦尼克生存预测——数据探索分析

TensorFlow2.9泰坦尼克号生存预测—结构化数据建模流程

泰坦尼克Titanic乘客生存预测

泰坦尼克Titanic乘客生存预测