用R语言复现https://www.kaggle.com/lelexy/titanic-data-science-solutions/edit的数据处理,可视化与建模过程
时间: 2023-12-08 08:06:36 浏览: 111
好的,我会尽力为您解答。首先,您需要先下载 Titanic 数据集,可以在 Kaggle 上下载。然后,我们按照以下步骤进行数据处理,可视化与建模:
1. 导入所需的 R 包:
```
library(tidyverse)
library(survival)
library(rpart)
library(rpart.plot)
library(ranger)
```
2. 读取数据集,并将训练集和测试集合并:
```
train <- read.csv("train.csv", stringsAsFactors = F)
test <- read.csv("test.csv", stringsAsFactors = F)
full <- bind_rows(train, test)
```
3. 数据预处理:
```
# 用平均值填充 Age 中的缺失值
full$Age[is.na(full$Age)] <- mean(full$Age, na.rm = T)
# 用众数填充 Embarked 中的缺失值
full$Embarked[is.na(full$Embarked)] <- mode(full$Embarked, na.rm = T)
# 用中位数填充 Fare 中的缺失值
full$Fare[is.na(full$Fare)] <- median(full$Fare, na.rm = T)
# 将 Cabin 中的缺失值替换为 "Unknown"
full$Cabin[is.na(full$Cabin)] <- "Unknown"
# 创建新的变量 FamilySize 和 Alone
full$FamilySize <- full$SibSp + full$Parch + 1
full$Alone <- ifelse(full$FamilySize == 1, "Alone", "Not Alone")
# 将 Name 中的称谓提取出来
full$Title <- gsub('(.*, )|(\\..*)', '', full$Name)
full$Title[full$Title %in% c('Mlle', 'Ms')] <- 'Miss'
full$Title[full$Title == 'Mme'] <- 'Mrs'
full$Title[full$Title %in% c('Capt', 'Don', 'Major', 'Sir')] <- 'Sir'
full$Title[full$Title %in% c('Dona', 'Lady', 'the Countess', 'Jonkheer')] <- 'Lady'
```
4. 可视化:
```
# 柱状图:Survived 和 Sex
full %>%
filter(!is.na(Survived)) %>%
ggplot(aes(x = factor(Survived), fill = Sex)) +
geom_bar(position = "dodge") +
labs(x = "Survived", y = "Count", fill = "Sex")
# 箱线图:Survived 和 Age
full %>%
filter(!is.na(Survived)) %>%
ggplot(aes(x = factor(Survived), y = Age, fill = factor(Survived))) +
geom_boxplot() +
labs(x = "Survived", y = "Age", fill = "Survived")
# 散点图:Survived 和 Fare
full %>%
filter(!is.na(Survived)) %>%
ggplot(aes(x = factor(Survived), y = Fare, color = factor(Survived))) +
geom_jitter(alpha = 0.4) +
labs(x = "Survived", y = "Fare", color = "Survived")
# 热力图:特征之间的相关性
corr <- full %>%
select(-PassengerId, -Survived) %>%
cor()
corrplot::corrplot(corr, method = "color", type = "upper", order = "hclust")
```
5. 建模:
```
# 划分数据集
train <- full[1:891, ]
test <- full[892:1309, ]
# 决策树模型
tree <- rpart(Survived ~ Pclass + Sex + Age + SibSp + Parch + Fare + Embarked + Title, data = train, method = "class")
rpart.plot::rpart.plot(tree)
# 随机森林模型
rf <- ranger(Survived ~ Pclass + Sex + Age + SibSp + Parch + Fare + Embarked + Title, data = train, num.trees = 1000)
importance <- importance(rf)
varImpPlot(importance)
# 提交结果
test$Survived <- predict(rf, test)$predictions
submission <- test %>%
select(PassengerId, Survived) %>%
write.csv("submission.csv", row.names = F)
```
以上就是复现 Titanic 数据处理、可视化与建模过程的 R 代码,希望能对您有所帮助。
阅读全文