R语言进行集成学习算法:随机森林
创始人
2024-12-27 01:35:58
0
# 10.4 集成学习及随机森林 # 导入car数据集 car <- read.table("data/car.data",sep = ",") # 对变量重命名 colnames(car) <- c("buy","main","doors","capacity",                    "lug_boot","safety","accept") # 随机选取75%的数据作为训练集建立模型,25%的数据作为测试集用来验证模型 library(caret) library(ggplot2) library(lattice) # 构建训练集的下标集 ind <- createDataPartition(car$accept,times=1,p=0.75,list=FALSE)  # 构建测试集数据好训练集数据 carTR <- car[ind,] carTE <- car[-ind,] carTR<- within(carTR,accept <- factor(accept,levels=c("unacc","acc","good","vgood"))) carTE<- within(carTE,accept <- factor(accept,levels=c("unacc","acc","good","vgood")))   # 使用adabag包中的bagging函数实现bagging算法 #install.packages("adabag") library(adabag) bagging.model <- bagging(accept~.,data=carTR)  # 使用adabag包中的boosting函数实现boosting算法 boosting.model <- boosting(accept~.,data=carTR)  # 使用randomForest包中的randomForest函数实现随机森林算法 #install.packages("randomForest") library(randomForest) randomForest.model <- randomForest(accept~.,data=carTR,ntree=500,mtry=3)  # 预测结果,并构建混淆矩阵,查看准确率 # 构建result,存放预测结果 result <- data.frame(arithmetic=c("bagging","boosting","随机森林"),                      errTR=rep(0,3),errTE=rep(0,3)) for(i in 1:3){   # 预测结果   carTR_predict <- predict(switch(i,bagging.model,boosting.model,randomForest.model),                            newdata=carTR) # 训练集数据   carTE_predict <- predict(switch(i,bagging.model,boosting.model,randomForest.model),                            newdata=carTE) # 测试集数据   # 构建混淆矩阵   tableTR <- table(actual=carTR$accept,                    predict=switch(i,carTR_predict$class,carTR_predict$class,carTR_predict))   tableTE <- table(actual=carTE$accept,                    predict=switch(i,carTE_predict$class,carTE_predict$class,carTE_predict))   # 计算误差率   result[i,2] <- paste0(round((sum(tableTR)-sum(diag(tableTR)))*100/sum(tableTR),                               2),"%")   result[i,3] <- paste0(round((sum(tableTE)-sum(diag(tableTE)))*100/sum(tableTE),                               2),"%") } # 查看结果 result 

相关内容

热门资讯

透视辅助!hhpoker是真的... 透视辅助!hhpoker是真的假的,hhpoker德州透视,2025新版(有挂插件);1、hhpok...
透视数据!有哪些免费的wpk作... 透视数据!有哪些免费的wpk作弊码,wpk私人辅助,必赢教程(一直是真的有挂)1、不需要AI权限,帮...
透视了解!hhpoker的辅助... 透视了解!hhpoker的辅助是真的吗,hhpoker可以控制吗,高科技教程(有挂脚本);运hhpo...
透视好友!aapoker怎么控... 透视好友!aapoker怎么控制牌,aapoker透视怎么用,安装教程(有挂介绍)aapoker透视...
透视线上!wpk辅助器是真的吗... 透视线上!wpk辅助器是真的吗,微扑克微乐辅助,可靠教程(总是真的有挂);1、打开软件启动之后找到中...
透视美元局!模拟器打开hhpo... 透视美元局!模拟器打开hhpoker,hhpoker有后台操作吗,2025新版教程(有挂解密)1、点...
透视苹果版!aa poker透... 透视苹果版!aa poker透视软件,aapoker怎么控制牌,教你教程(有挂攻略)1、用户打开应用...
透视模拟器!wpk有辅助器吗,... 透视模拟器!wpk有辅助器吗,wpk系统是否存在作弊行为,系统教程(原来是真的有挂);一、wpk系统...
透视有挂!hhpoker作弊码... 透视有挂!hhpoker作弊码怎么用,模拟器打开hhpoker,解密教程(有挂脚本)1、hhpoke...
透视脚本!aapoker透视脚... 透视脚本!aapoker透视脚本入口,aapoker怎么控制牌,必备教程(有挂辅助);暗藏猫腻,小编...