作为一个初学小白,十分清楚实践才能深入理解内容,所以看过大神们关于随机森林解决kaggle房价预测问题(House_Prices)的解答以后,照着完成了一个Titanic生还预测
Competition Description 问题描述如下:
The sinking of the RMS Titanic is one of the most infamous shipwrecks in history. On April 15, 1912, during her maiden voyage, the Titanic sank after colliding with an iceberg, killing 1502 out of 2224 passengers and crew. This sensational tragedy shocked the international community and led to better safety regulations for ships.
One of the reasons that the shipwreck led to such loss of life was that there were not enough lifeboats for the passengers and crew. Although there was some element of luck involved in surviving the sinking, some groups of people were more likely to survive than others, such as women, children, and the upper-class.
In this challenge, we ask you to complete the analysis of what sorts of people were likely to survive. In particular, we ask you to apply the tools of machine learning to predict which passengers survived the tragedy.
我们都知道Titanic与冰山相撞沉船后,因没有足够的救生艇给乘客和船员造成大批人员遇难。尽管在沉船事故中幸存下来的运气有一些因素,但一些群体比其他群体更有可能存活下来,比如妇女、儿童和上层社会。因此我们将应用机器学习的工具来预测哪些乘客可能在这场悲剧中幸存了下来。
首先在kaggle网站上下载三个csv文件,提交样本submission.csv,训练文件train.csv, 以及测试文件test.csv
导入pandas包,pandas可以在python中处理所有数据分析相关工作,保存数据的对象名称为DataFrame
我们先来看一下训练文件的数据集,用read_csv()读入train.csv, 使用head()方法(列出表的前五行)观察数据集
该训练集一共12列,除去PassengerId 11列,10列为独立变量,survived为我们的目标变量,即因变量
随机森林是包含多个决策树的一种分类器,可以避免决策树的过拟合,简单的平均所有树的预测得出一个更好的预测和泛化结果
将训练集和测试集分别加载进 DataFrame 之后,保存目标变量
因为只想保留数据集中的独立变量和特征,所以紧接着在DataFrame 中删除了Survived
训练集和测试集中添加了一个新的临时列('training_set'),是为了将两个数据集连接在一起,放在同一个 DataFrame 中,接下来填充缺失的数值,并通过独热编码(One-Hot Encoding)将分类特征转换为数字特征。
接下来将两个数据集分开,去掉临时列,构建一个有 100 个树的随机森林(通常,树越多结果越好,但这也意味着训练时间的增加),使用计算机的所有 CPU 核心(n_jobs=-1),使用训练集进行拟合,用拟合的随机森林来预测测试集的目标变量
这里的预测结果为小数,而文档中规定,生还为1 遇难为0,我使用了astype将结果转化为最接近的int值
把结果和它们各自的 Id 放在一个 DataFrame 中,并保存到 一个csv文件中
登陆 Kaggle 页面提交csv文件即可
本次只是作为随机森林的一个练习,其实对于这个案例来说,随机森林并不是一个非常好的解决方法
更多深入学习后可能会对这个案例重新详细分析~