這是我目前用R寫決策樹的部分,但卻不知如何進行10-fold來相互驗證,想請問各方高手該如何撰寫~
library(rpart)
library(rpart.plot)
dat = read.csv("test.csv")
dat=dat[,c(-12,-13,-15,-14)]
require(rpart)
set.seed(5566)
train=dat[(as.numeric(0.1*nrow(dat))+1):as.numeric(0.9*nrow(dat)),]
test= dat[(as.numeric(0.9*nrow(dat))+1):as.numeric(1*nrow(dat)),]
cart.model<- rpart(cluster ~. ,dat=train)
require(rpart.plot)
prp(cart.model,fallen.leaves=TRUE,shadow.col="gray")
pred = predict(cart.model, newdat=test, type="class")
table(real=test$cluster, predict=pred)
confus.matrix <- table(real=test$cluster, predict=pred)
sum(diag(confus.matrix))/sum(confus.matrix)