2019-01-31
阅读量:
1356
R语言构建分类决策树
分类决策树:用于预测定性数据,给定观测值被预测为它所属的叶节点内训练集中最常见的类别(多数表决)。
构造过程:特征选择→决策树的生成→决策树的剪枝
分类决策树以分类错误率作为确定分割点的准则
rm(list=ls())
library(tree) #加载tree包以建立分类树和回归树
data("Carseats")
attach(Carseats)
High=ifelse(Sales<=8,"No","Yes") #创建高销量变量
Carseats=data.frame(Carseats,High)
dim(Carseats) #400 12
tree.carseats=tree(High~.-Sales,data=Carseats)
summary(tree.carseats)
###生成####
set.seed(2)
train=sample(1:nrow(Carseats),200) #200个训练集 200个测试集
Carseats.test=Carseats[-train,] #测试数据
High.test=High[-train]
tree.carseats=tree(High~.-Sales,Carseats,subset = train)
tree.carseats
par(mfrow=c(1,1))
plot(tree.carseats)
text(tree.carseats,pretty=0)
tree.pred=predict(tree.carseats,Carseats.test,type="class")
table(tree.pred,High.test)(86+57)/200 #测试集预测的准确率为(86+57)/200=0.715
###剪枝####
set.seed(3)
cv.carseats=cv.tree(tree.carseats,FUN=prune.misclass)
names(cv.carseats)
par(mfrow=c(1,2))
plot(cv.carseatssize,cv.carseatsdev,type="b") #错误率对树规模size的函数
plot(cv.carseatsk,cv.carseatsdev,type="b") #错误率对复杂度k的函数
par(mfrow=c(1,1))
prune.carseats=prune.misclass(tree.carseats,best=9)
plot(prune.carseats)
text(prune.carseats,pretty=0)
tree.pred=predict(prune.carseats,Carseats.test,type="class")
table(tree.pred,High.test)(96+60)/200 #测试集预测的准确率为(96+60)/200=0.78
0.0000
0
5
关注作者
收藏
评论(0)
发表评论
暂无数据
推荐帖子
0条评论
0条评论
1条评论