R语言——一秒决策树分析

决策树分析 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

R语言——一秒决策树分析
文章图片

问题区:
  1. set.seed(12345)
说明:在此模型中,关于随机种子数的设置会导致实验数据出现异同。尽管随机种子在众多博客中只是标识性作用,但实质上在本分析中改变了模型的准确率。
  1. 联系方式: crays_1995@foxmail.com

    推荐阅读