library(rpart) library(rpart.plot) pur<-read.table("D://Rdata//purchase.txt",header=T) pur$Income<-as.factor(pur$Income) pur$Gender<-as.factor(pur$Gender) pur$Purchase<-as.factor(pur$Purchase) a1<-subset(pur,Purchase==1);str(a1) fit.dt<-rpart(Purchase~.,data=pur,method="class",parms=list(split="gini")) # 依据 R 默认建立决策树 rpart.plot(fit.dt,type=4,branch=0,extra=2)# 可视化决策树 dt.pred<-predict(fit.dt,newdata=pur)