r - 构建决策树分类
问题描述
我有两个数据集,partb_data1
和partb_data2
。给定反映客户特征的银行客户样本以及银行是否继续与他们合作(流失)。退出:流失(如果他已经离开银行,则为 1,如果他继续与银行合作,则为 0)。我partb_data1
用作训练集和partb_data2
测试集。
这是我的数据:
> dput(head(partb_data1))
structure(list(RowNumber = 1:6, CustomerId = c(15634602L, 15647311L,
15619304L, 15701354L, 15737888L, 15574012L), Surname = c("Hargrave",
"Hill", "Onio", "Boni", "Mitchell", "Chu"), CreditScore = c(619L,
608L, 502L, 699L, 850L, 645L), Geography = c("France", "Spain",
"France", "France", "Spain", "Spain"), Gender = c("Female", "Female",
"Female", "Female", "Female", "Male"), Age = c(42L, 41L, 42L,
39L, 43L, 44L), Tenure = c(2L, 1L, 8L, 1L, 2L, 8L), Balance = c(0,
83807.86, 159660.8, 0, 125510.82, 113755.78), NumOfProducts = c(1L,
1L, 3L, 2L, 1L, 2L), HasCrCard = c(1L, 0L, 1L, 0L, 1L, 1L), IsActiveMember = c(1L,
1L, 0L, 0L, 1L, 0L), EstimatedSalary = c(101348.88, 112542.58,
113931.57, 93826.63, 79084.1, 149756.71), Exited = c(1L, 0L,
1L, 0L, 0L, 1L)), row.names = c(NA, 6L), class = "data.frame")
> dput(head(partb_data2))
structure(list(RowNumber = 8001:8006, CustomerId = c(15629002L,
15798053L, 15753895L, 15595426L, 15645815L, 15632848L), Surname = c("Hamilton",
"Nnachetam", "Blue", "Madukwe", "Mills", "Ferrari"), CreditScore = c(747L,
707L, 590L, 603L, 615L, 634L), Geography = c("Germany", "Spain",
"Spain", "Spain", "France", "France"), Gender = c("Male", "Male",
"Male", "Male", "Male", "Female"), Age = c(36L, 32L, 37L, 57L,
45L, 36L), Tenure = c(8L, 9L, 1L, 6L, 5L, 1L), Balance = c(102603.3,
0, 0, 105000.85, 0, 69518.95), NumOfProducts = c(2L, 2L, 2L,
2L, 2L, 1L), HasCrCard = c(1L, 1L, 0L, 1L, 1L, 1L), IsActiveMember = c(1L,
0L, 0L, 1L, 1L, 0L), EstimatedSalary = c(180693.61, 126475.79,
133535.99, 87412.24, 164886.64, 116238.39), Exited = c(0L, 0L,
0L, 1L, 0L, 0L)), row.names = c(NA, 6L), class = "data.frame")
我创建了分类树以预测 churn 。下面是代码:
library(tidyverse)
library(caret)
library(rpart)
library(rpart.plot)
# Split the data into training and test set
train.data <- head(partb_data1, 500)
test.data <- tail(partb_data2, 150)
# Build the model
modelb <- rpart(Exited ~., data = train.data, method = "class")
# Visualize the decision tree with rpart.plot
rpart.plot(modelb)
# Make predictions on the test data
predicted.classes <- modelb %>%
predict(test.data, type = "class")
head(predicted.classes)
# Compute model accuracy rate on test data
mean(predicted.classes == test.data$Exited)
### Pruning the tree :
# Fit the model on the training set
modelb2 <- train(
Exited ~., data = train.data, method = "rpart",
trControl = trainControl("cv", number = 10),
tuneLength = 10
)
# Plot model accuracy vs different values of
# cp (complexity parameter)
plot(modelb2)
# Print the best tuning parameter cp that
# maximizes the model accuracy
modelb2$bestTune
# Plot the final tree model
plot(modelb2$finalModel)
# Make predictions on the test data
predicted.classes <- modelb2 %>% predict(test.data)
# Compute model accuracy rate on test data
mean(predicted.classes == test.data$Exited)
注意:我已经从partb_data2
.
我遵循的程序正确吗?我必须进行任何更改才能完成我的目标,即分类树?非常欢迎您的帮助!
已编辑!
解决方案
您的 head(partb_data1$Exited, 500) 不是 data.frame。因为$
你需要你的 partb_data1 数据的一个子集。它只是一个整数向量,所以不能工作。
类(头(partb_data1$Exited,500))[1]“整数”
推荐阅读
- c++ - c++ 中有没有办法使用点运算符而不是 -> 来表示 pSomePointer->pValue->nSubvalue
- go - 如何在 golang 中实例化函数类型?
- laravel - 无法使用带有 Laravel 5.4 的 sendgrid 发送电子邮件并发现错误
- c# - 以低角度旋转图像并清晰保存(C#)
- google-maps - 如何使用下拉菜单在谷歌地图上设置标记?
- ios - IOS 媒体播放器 - 跳到特定时间
- laravel - Laravel 队列超时问题
- php - 如何在自定义 wordpress 插件中显示编辑表单?
- scala - 如何解决 Complete-Akka HTTP 中的 Either[Error, User]
- go - 无法正确读取 CLI 标志