关于AdaBoost算法计算变量重要度的理解

在看AdaBoost算法在R中的实现函数boosting时,发现该函数可以计算变量重要度(importance),不仅感慨这个函数好强大,不但可以轻松调用AdaBoost这种集成学习算法,还提供了计算变量importance的功能。但是,importance究竟是如何算出来的,这个问题需要理解。

在R中查找boosting函数的帮助文档,发现了一篇关于AdaBoost算法的开发资料[1],里面提到了“The package adabag 3.2, available from de Comprehesive R Archive Network at http://CRAN.R-project.org/package=adabag, is the current update of adabag that changes the measure of relative importance of the predictor variables using the gain of the Gini index given by a variable in a tree and, in the case of the boosting function, the weight of this tree. For this goal, the varImp function of the caret package (Kuhn 2008, 2012) is used to get the gain of the Gini index of the variables in each tree.”简言之,各变量的importance是通过每棵树中各变量的基尼指数与每棵树的权重计算得来的,在R中varImp函数可以计算每棵树中各变量的基尼指数。

查找到上述信息后,自己写了几行代码,意思是先利用varImp函数计算每棵树中各变量的基尼指数,然后将各变量的基尼指数与当前树的权重相乘得到各个变量在当前树中的importance,之后将各个树中各变量的importance相加得到各个变量的importance的总和,最后一步归一化,使得各个变量的importance总和为1。按照上述思路写完代码,运行发现与boosting函数计算出来的各变量importance差异很大。

找到差异原因需要深入了解boosting函数计算变量importance的方法。于是,下载了boosting函数的源码来看,发现调用varImp函数计算importance时设置了两个参数varImp(a$trees[[1]], surrogates = FALSE, competes = FALSE),说明文档关于这两个参数为FALSE时的介绍是将代表和竞争忽略,在此不做深入阐述。是否设置这两个参数得到的计算结果是不同的。
在这里插入图片描述 在这里插入图片描述
找到了这个差异,再顺着代码继续看,发现boosting函数源码中关于如何计算各变量的importance与自己的想法是一致的,主要差别还是varImp函数中关于surrogates与competes参数的设置。

贴上自己写的计算变量importance的代码,结果与boosting函数的一致:

# train AdaBoost model
a<-boosting(class~.,data_train,coeflearn = 'Zhu',boos=F)   ##method is different,boos if different
# trees amount
mfinal <- 100
# parameters amount
nvar <- 6
imp <- array(0, c(mfinal, nvar))
pond <- a$weights
for(i in 1:mfinal){
  k <- varImp(a$trees[[i]], surrogates = FALSE, competes = FALSE)
  imp[i, ] <- k[sort(row.names(k)), ]
}
imppond <- as.vector(as.vector(pond) %*% imp)
imppond <- imppond/sum(imppond) * 100
names(imppond) <- sort(row.names(k))
importance <- imppond

参考文献:
Alfaro, E., Gamez, M. and Garcia, N. (2013): “adabag: An R Package for Classification with Boosting and Bagging”. Journal of Statistical Software, Vol 54, 2, pp. 1–35.

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值