决策树分析 1.文档读取
data <- read.csv(file.choose(),header=TRUE)
View(data)
2.建立训练数据与测试数据
set.seed(12345)
select <- sample(1:nrow(data),nrow(data)*0.7)
train <- data[select,]
test <- data[-select,]
【R语言——一秒决策树分析】3、建立模型
library(rpart)
library(rpart.plot)CART.tree <- rpart(Class ~ ., data=https://www.it610.com/article/train, control=rpart.control(minsplit=2, cp=0))
rpart.plot(CART.tree)
3、模型检验及评价
CART.Prediction <- predict(CART.tree, newdata=https://www.it610.com/article/test, type='class')Results <- table(Prediction=CART.Prediction, Actual=test$Class)
ResultsCorrect_Rate <- sum(diag(Results)) / sum(Results)
Correct_Rate
4、查找CP并重组检测模型
CART.tree <- prune(CART.tree, cp=0.03)
rpart.plot(CART.tree)CART.Prediction <- predict(CART.tree, newdata=https://www.it610.com/article/test, type='class')results <- table(Prediction=CART.Prediction, Actual=test$Class)
resultsCorrect_Rate <- sum(diag(results)) / sum(results)
Correct_Rate
文章图片
问题区:
- set.seed(12345)
- 联系方式: crays_1995@foxmail.com