Cook R数据科学与R语言R. python新手日记

R 集成算法① adaboost

2018-01-09  本文已影响55人  柳叶刀与小鼠标

集成算法

集成算法(Ensemble method)

是目前数据科学家工具箱的一种重要补充。这个工具非常流行,目前是许多机器学习比赛参赛者的选择策略。通常是通过一系列分类器,一般是决策树,然后对预测值进行投票。总而言之,集成算法不是挑选一个模型,而是通过一定的方式将多个模型组合起来。其中两个重要方法是套袋法(bagging)和提升法(boosting)。

套袋法和提升法

从未加权的数据开始,第一个分类器尝试对结果建模。预测正确的样本出现在下一个分类器的训练集中的可能性较小,相反,难以分类的样本将会出现的更频繁。当下一轮的弱分类器被添加后,他们用来训练后面更难的样本。该过程会持续进行,知道达到要求的总误差或者性能不能提高。这时每个分类器的票数会按照他们在建模数据集上的准确度进行加权。


setwd("E:\\Rwork")
library(rpart)
library(caret)
library(adabag)
library(ggplot2)
library(ggthemes)
set.seed(123)
data(iris)
index <- sample(nrow(iris),0.75*nrow(iris))
train <- iris[index,]
test <- iris[index,]
error <- as.numeric()



library(tcltk)  
u <- 1:20  
pb <- tkProgressBar("进度","已完成 %", 0, 100)  

for(i in u){
  info<- sprintf("已完成 %d%%", round(i*100/length(u)))  
  setTkProgressBar(pb, i*100/length(u), sprintf("进度 (%s)", info),info)  
  data.adaboost <- boosting(Species ~ ., data=train, mfinal=i)
  data.pred <- predict.boosting(data.adaboost,newdata = test)
  error[i] <- data.pred$error
  }
close(pb)  


##################################################

##################################################

error <- as.data.frame(error)
p <- ggplot(error,aes(x=1:20,y=error))+
     geom_line(colour="red", linetype="dashed",size = 1)+
     geom_point(size=3, shape=18)+
     ylim(0,0.05) +
     xlab("Classifiers Numbers")

p1 <- p + theme_base()+
      theme(panel.grid = element_blank())+
      theme(axis.title = element_text(face = "bold"))

p1

iris.adaboost <- boosting(Species ~ ., data=train, mfinal=10)
importanceplot(iris.adaboost)

上一篇 下一篇

猜你喜欢

热点阅读