这场灾难以拯救“妇女和儿童第一”而闻名,所以让我们来看看性别和年龄变量。我们将从乘客的性别开始。将数据重新加载到R后,请查看此变量的摘要:
summary(train$Sex)\n\nfemale male\n\n314 577"}" style="margin-top: 0px; margin-bottom: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"> > summary(train$Sex) female male 314 577
need-to-insert-img
所以我们看到大多数乘客都是男性。对幸存的男性和女性进行双向比较:
prop.table(table(train$Sex, train$Survived))\n\n0 1\n\nfemale 0.09090909 0.26150393\n\nmale 0.52525253 0.12233446"}" style="margin-top: 0px; margin-bottom: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"> > prop.table(table(train$Sex, train$Survived)) 0 1 female 0.09090909 0.26150393 male 0.52525253 0.12233446
need-to-insert-img
我们希望看到的是行数比例,即存活的每个性别的比例。
prop.table(table(train$Sex, train$Survived),1)\n\n0 1\n\nfemale 0.2579618 0.7420382\n\nmale 0.8110919 0.1889081"}" style="margin-top: 0px; margin-bottom: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"> > prop.table(table(train$Sex, train$Survived),1) 0 1 female 0.2579618 0.7420382 male 0.8110919 0.1889081
need-to-insert-img
我们现在可以看到大多数女性幸存下来,并且男性的比例非常低。
test$Survived <- 0\n\n> test$Survived[test$Sex == 'female'] <- 1"}" style="margin-top: 0px; margin-bottom: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"> > test$Survived <- 0 > test$Survived[test$Sex == 'female'] <- 1
need-to-insert-img
在这里,我们开始像以前一样添加“everyone dies”预测列,除了我们将抛弃rep命令并将零指定给整个列。然后我们改变了相同的列,其中1为乘客的变量“Sex”等于“女性”。
现在让我们写一个新的提交并发送给Kaggle
need-to-insert-img
现在让我们开始深入研究年龄变量:
summary(train$Age)\n\nMin. 1st Qu. Median Mean 3rd Qu. Max. NA's\n\n0.42 20.12 28.00 29.70 38.00 80.00 177"}" style="margin-top: 0px; margin-bottom: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"> > summary(train$Age) Min. 1st Qu. Median Mean 3rd Qu. Max. NA's 0.42 20.12 28.00 29.70 38.00 80.00 177
need-to-insert-img
数据分析中可能缺少值,这可能会导致现实世界中出现的各种问题,而这些问题有时很难处理。目前我们可以假设177个缺失值是其余乘客的平均年龄。
现在我们有一个连续的变量,我们创建一个新的变量“Child”来表明乘客是否低于18岁:
train$Child <- 0\n\n> train$Child[train$Age < 18] <- 1"}" style="margin-top: 0px; margin-bottom: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"> > train$Child <- 0 > train$Child[train$Age < 18] <- 1
need-to-insert-img
现在我们要创建一个包含性别和年龄的表,以查看不同子集的生存比例。首先让我们尝试找出不同子集的幸存者数量:
aggregate(Survived ~ Child + Sex, data=train, FUN=sum)\n\nChild Sex Survived\n\n1 0 female 195\n\n2 1 female 38\n\n3 0 male 86\n\n4 1 male 23"}" style="margin-top: 0px; margin-bottom: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"> > aggregate(Survived ~ Child + Sex, data=train, FUN=sum) Child Sex Survived 1 0 female 195 2 1 female 38 3 0 male 86 4 1 male 23
need-to-insert-img
但我们不知道每个子集中的总人数; 让我们来看看:
aggregate(Survived ~ Child + Sex, data=train, FUN=length)\n\nChild Sex Survived\n\n1 0 female 259\n\n2 1 female 55\n\n3 0 male 519\n\n4 1 male 58"}" style="margin-top: 0px; margin-bottom: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"> > aggregate(Survived ~ Child + Sex, data=train, FUN=length) Child Sex Survived 1 0 female 259 2 1 female 55 3 0 male 519 4 1 male 58
need-to-insert-img
我们需要创建一个函数,它将子集向量作为输入,并将sum和length命令应用于它,然后进行除法以给出一个比例。
aggregate(Survived ~ Child + Sex, data=train, FUN=function(x) {sum(x)/length(x)})\n\nChild Sex Survived\n\n1 0 female 0.7528958\n\n2 1 female 0.6909091\n\n3 0 male 0.1657033\n\n4 1 male 0.3965517"}" style="margin-top: 0px; margin-bottom: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"> > aggregate(Survived ~ Child + Sex, data=train, FUN=function(x) {sum(x)/length(x)}) Child Sex Survived 1 0 female 0.7528958 2 1 female 0.6909091 3 0 male 0.1657033 4 1 male 0.3965517
need-to-insert-img
虽票价是一个连续变量,需要将其简化为可以轻松制表的内容。我们将票价收入不到10美元,10美元到20美元,20美元到30美元以及30美元以上,并将其存储到一个新变量中:
train$Fare2 <- '30+'\n\n> train$Fare2[train$Fare < 30 & train$Fare >= 20] <- '20-30'\n\n> train$Fare2[train$Fare < 20 & train$Fare >= 10] <- '10-20'\n\n> train$Fare2[train$Fare < 10] <- '<10'"}" style="margin-top: 0px; margin-bottom: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"> > train$Fare2 <- '30+' > train$Fare2[train$Fare < 30 & train$Fare >= 20] <- '20-30' > train$Fare2[train$Fare < 20 & train$Fare >= 10] <- '10-20' > train$Fare2[train$Fare < 10] <- '<10'
need-to-insert-img
现在让我们运行一个更长的聚合函数,看看这里有什么有趣的东西:
aggregate(Survived ~ Fare2 + Pclass + Sex, data=train, FUN=function(x) {sum(x)/length(x)})\n\nFare2 Pclass Sex Survived\n\n1 20-30 1 female 0.8333333\n\n2 30+ 1 female 0.9772727\n\n3 10-20 2 female 0.9142857\n\n4 20-30 2 female 0.9000000\n\n5 30+ 2 female 1.0000000\n\n6 <10 3 female 0.5937500\n\n7 10-20 3 female 0.5813953\n\n8 20-30 3 female 0.3333333 **\n\n9 30+ 3 female 0.1250000 **\n\n10 <10 1 male 0.0000000\n\n11 20-30 1 male 0.4000000\n\n12 30+ 1 male 0.3837209\n\n13 <10 2 male 0.0000000\n\n14 10-20 2 male 0.1587302\n\n15 20-30 2 male 0.1600000\n\n16 30+ 2 male 0.2142857\n\n17 <10 3 male 0.1115385\n\n18 10-20 3 male 0.2368421\n\n19 20-30 3 male 0.1250000\n\n20 30+ 3 male 0.2400000"}" style="margin-top: 0px; margin-bottom: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"> > aggregate(Survived ~ Fare2 + Pclass + Sex, data=train, FUN=function(x) {sum(x)/length(x)}) Fare2 Pclass Sex Survived 1 20-30 1 female 0.8333333 2 30+ 1 female 0.9772727 3 10-20 2 female 0.9142857 4 20-30 2 female 0.9000000 5 30+ 2 female 1.0000000 6 <10 3 female 0.5937500 7 10-20 3 female 0.5813953 8 20-30 3 female 0.3333333 ** 9 30+ 3 female 0.1250000 ** 10 <10 1 male 0.0000000 11 20-30 1 male 0.4000000 12 30+ 1 male 0.3837209 13 <10 2 male 0.0000000 14 10-20 2 male 0.1587302 15 20-30 2 male 0.1600000 16 30+ 2 male 0.2142857 17 <10 3 male 0.1115385 18 10-20 3 male 0.2368421 19 20-30 3 male 0.1250000 20 30+ 3 male 0.2400000
need-to-insert-img
虽然大多数男性,无论是班级还是票价仍然做得不好。
让我们根据新的见解做出新的预测。
test$Survived <- 0\n\n> test$Survived[test$Sex == 'female'] <- 1\n\n> test$Survived[test$Sex == 'female' & test$Pclass == 3 & test$Fare >= 20] <- 0"}" style="margin-top: 0px; margin-bottom: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word;"> > test$Survived <- 0 > test$Survived[test$Sex == 'female'] <- 1 > test$Survived[test$Sex == 'female' & test$Pclass == 3 & test$Fare >= 20] <- 0
need-to-insert-img
好的,让我们创建输出文件,看看我们是否做得更好!
need-to-insert-img
有问题欢迎下方留言!
网友评论