-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtree_delay_prob.R
More file actions
29 lines (28 loc) · 949 Bytes
/
tree_delay_prob.R
File metadata and controls
29 lines (28 loc) · 949 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
setwd("E:/deep_learing_R/Machine-Learning-with-R-datasets-master")
library(C50)
data(churn)
str(churnTrain)
churnTrain<-churnTrain[,!names(churnTrain) %in% c("state","area_code","account_length")]
set.seed(12345)
churnTrain<-churnTrain[order(runif(nrow(churnTrain))),]
trainset<-churnTrain[1:2666,]
testset<-churnTrain[2667:3333,]
library(rpart)
model<-rpart(churn~.,data = trainset)
summary(model)
plot(model,uniform = T)
text(model,all = T,use.n = T)
prediction<-predict(model,testset,type = "class")
table(prediction,testset$churn)
library(caret)
confusionMatrix(prediction,testset$churn)
#递归分割树剪枝
min(model$cptable[,"xerror"])
which.min(model$cptable[,"xerror"])
dim(model$cptable)
model_cp<-model$cptable[8,"CP"]
prune_tree<-prune(model,cp=model_cp)
plot(prune_tree,uniform = T)
text(prune_tree,all = T,use.n = T)
prediction_modified<-predict(prune_tree,testset,type = "class")
confusionMatrix(prediction_modified,testset$churn)