Titanic数据集分析.doc_第1页
Titanic数据集分析.doc_第2页
Titanic数据集分析.doc_第3页
Titanic数据集分析.doc_第4页
Titanic数据集分析.doc_第5页
已阅读5页,还剩15页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

泰坦尼克数据集探索1. 简介:从泰塔尼克数据集中,根据每个乘客的信息,建立模型并进行预测。整篇文章分为三步:1. 特征选择2. 缺失数据处理3. 预测1.1 导入软件包并检查数据 library(ggplot2) # 可视化 library(ggthemes) # 可视化 library(scales) # 可视化 library(dplyr) # 数据处理 library(mice) # 填充缺失数据 library(randomForest) # 分类算法 #数据的导入 setwd(D:/Titanic)#设置默认功过路径 train test full # check data str(full)我们观察到一共有1309条数据,每一条数据有12个相关变量。2. 特征工程头衔# 从名称中挖掘 # 从乘客名字中提取头衔 #R中的grep、grepl、sub、gsub、regexpr、gregexpr等函数都使用正则表达式的规则进行匹配。默认是egrep的规则,sub函数只实现第一个位置的替换,gsub函数实现全局的替换。 full$Title # 查看按照性别划分的头衔数量 table(full$Sex, full$Title)我们发现头衔的类别太多,并且好多出现的频次是很低的,我们可以将这些类别进行合并 rare_title # 重命名称呼 full$Titlefull$Title = Mlle full$Titlefull$Title = Ms full$Titlefull$Title = Mme full$Titlefull$Title %in% rare_title # 再次查看按照性别划分的头衔数量 table(full$Sex, full$Title)可以看到头衔的个数得到了大量的缩减 #sapply()函数:根据传入参数规则重新构建一个合理的数据类型返回 full$Surname # Create a family size variable including the passenger themselves full$Fsize # Create a family variable full$Family #为了直观显示,我们可以用ggplot2 画出家庭成员数量和生存家庭数情况的图形 ggplot(full1:891, aes(x = Fsize, fill = factor(Survived) + geom_bar(stat=count, position=dodge) + scale_x_continuous(breaks=c(1:11) + labs(x = Family Size) + theme_few() full$FsizeDfull$Fsize = 1 full$FsizeDfull$Fsize 1 full$FsizeDfull$Fsize 4 # Show family size by survival using a mosaic plot mosaicplot(table(full$FsizeD, full$Survived), main=Family Size by Survival, shade=TRUE)尝试创建一些新的特征 # This variable appears to have a lot of missing values full$Cabin1:28 # Create a Deck variable. Get passenger deck A - F: full$Deck # Use ggplot2 to visualize embarkment, passenger class, & median fare ggplot(embark_fare, aes(x = Embarked, y = Fare, fill = factor(Pclass) + geom_boxplot() + geom_hline(aes(yintercept=80), + colour=red, linetype=dashed, lwd=2) + scale_y_continuous(labels=dollar_format() + theme_few()可以看到出发的一级乘客的中位票价与我们的登机手续费乘客支付的80美元相当。我们接近在这里和那里确定了几个缺失值的位置。 1044行上的乘客的票价是缺失值。 # Since their fare was $80 for 1st class, they most likely embarked from C full$Embarkedc(62, 830) # Show row 1044 full1044, 这是从南安普敦(S)出发的三级乘客。 让所有其他人分享他们的班级和登机牌(n = 494)可视化票价。 ggplot(fullfull$Pclass = 3 & full$Embarked = S, , + aes(x = Fare) + geom_density(fill = #99d6ff, alpha=0.4) + + geom_vline(aes(xintercept=median(Fare, na.rm=T),+ colour=red, linetype=dashed, lwd=1) + scale_x_continuous(labels=dollar_format() + theme_few()从这个可视化的角度来看,将NA票价值替换为上课时间为8.05美元的中位数似乎是相当合理的。 # Replace missing fare value with median fare for class/embarkment full$Fare1044 # Show number of missing Age values sum(is.na(full$Age) # Make variables factors into factors factor_vars fullfactor_vars # Set a random seed set.seed(129) # Perform mice imputation, excluding certain less-than-useful variables: mice_mod # Save the complete output mice_output # Plot age distributions par(mfrow=c(1,2) hist(full$Age, freq=F, main=Age: Original Data, + col=darkgreen, ylim=c(0,0.04) hist(mice_output$Age, freq=F, main=Age: MICE Output, + col=lightgreen, ylim=c(0,0.04) # Show new number of missing Age values sum(is.na(full$Age)第二次特征工程现在我们知道每个人的年龄,我们可以创造几个新的年龄变量:儿童和母亲。 一个孩子只会是18岁以下的人,母亲是1)女性2)18岁以上3)有超过0个孩子4)没有头衔 Miss # First well look at the relationship between age & survival ggplot(full1:891, aes(Age, fill = factor(Survived) + + geom_histogram() + + # I include Sex since we know (a priori) its a significant predictor+ facet_grid(.Sex) + + theme_few() # Create the column child, and indicate whether child or adult full$Childfull$Age 18 full$Childfull$Age = 18 # Show counts table(full$Child, full$Survived) # Adding Mother variable full$Mother full$Motherfull$Sex = female & full$Parch 0 & full$Age 18 & full$Title != Miss # Show counts table(full$Mother, full$Survived) # Finish by factorizing our two new factor variables full$Child full$Mother md.pattern(full)预测最后,我们准备根据我们精心策划和处理缺失值的变量,预测在泰坦尼克号的乘客中谁能幸存下来。 为此,我们将依靠随机森林分类算法; 毕竟,我们花了所有的时间来进行数据处理。拆分测试与训练 # Split the data back into a train set and a test set train test # Set a random seed set.seed(754) # Build the model (note: not all possible variables are used) rf_model # Show model error plot(rf_model, ylim=c(0,0.36) legend(topright, colnames(rf_model$err.rate), col=1:3, fill=1:3)黑线显示总体错误率低于20。 红线和绿线分别显示“死亡”和“幸存”的错误率。 我们可以看到,现在我们比预测死亡更成功,而不是生存。变量重要性 # Get importance importance varImportance # Create a rank variable based on importance rankImportance %+ mutate(Rank = paste0(#,dense_rank(desc(Importance) # Use ggplot2 to visualize the relative importance of variables ggplot(rankImportance, aes(x = reorder(Variables, Importance), + y = Importance, fill = Importance) + geom_bar(stat=identity) + +

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论