XGBoost Plotting API以及GBDT组合特征实践
写在前面:
最近在深入学习一些树模型相关知识点,打算整理一下。刚好昨晚看到余音大神在Github上分享了一波 MachineLearningTrick,赶紧上车学习一波!大神这波节奏分享了xgboost相关的干货,还有一些内容未分享….总之值得关注!我主要看了:Xgboost的叶子节点位置生成新特征封装的函数。之前就看过相关博文,比如Byran大神的这篇:http://blog.csdn.net/bryan__/article/details/51769118,但是自己从未实践过。本文是基于bryan大神博客以及余音大神的代码对GBDT组合特征实践的理解和拓展,此外探索了一下XGBoost的Plotting API,学习为主!
官方API介绍:
http://xgboost.readthedocs.io/en/latest/python/python_api.html#module-xgboost.sklearn
1.利用GBDT构造组合特征原理介绍
从byran大神的博客以及这篇利用GBDT模型构造新特征中,可以比较好的理解GBDT组合特征:
论文的思想很简单,就是先用已有特征训练GBDT模型,然后利用GBDT模型学习到的树来构造新特征,最后把这些新特征加入原有特征一起训练模型。构造的新特征向量是取值0/1的,向量的每个元素对应于GBDT模型中树的叶子结点。当一个样本点通过某棵树最终落在这棵树的一个叶子结点上,那么在新特征向量中这个叶子结点对应的元素值为1,而这棵树的其他叶子结点对应的元素值为0。新特征向量的长度等于GBDT模型里所有树包含的叶子结点数之和。
举例说明。下面的图中的两棵树是GBDT学习到的,第一棵树有3个叶子结点,而第二棵树有2个叶子节点。对于一个输入样本点x,如果它在第一棵树最后落在其中的第二个叶子结点,而在第二棵树里最后落在其中的第一个叶子结点。那么通过GBDT获得的新特征向量为[0, 1, 0, 1, 0],其中向量中的前三位对应第一棵树的3个叶子结点,后两位对应第二棵树的2个叶子结点。
在实践中的关键点是如何获得每个样本在训练后树模型每棵树的哪个叶子结点上。之前知乎上看到过可以设置pre_leaf=True获得每个样本在每颗树上的leaf_Index,打开XGBoost官方文档查阅一下API:
原来这个参数是在predict里面,在对原始特征进行简单调参训练后,对原始数据以及测试数据进行new_feature= bst.predict(d_test, pred_leaf=True)
即可得到一个(nsample, ntrees) 的结果矩阵,即每个样本在每个树上的index。了解这个方法之后,我仔细学习了余音大神的代码,发现他并没有用到这个,如下:
可以看到他用的是apply()方法,这里就有点疑惑了,在XGBoost官方API并没有看到这个方法,于是我去SKlearn GBDT API看了下,果然有apply()方法可以获得leaf indices:
因为XGBoost有自带接口和Scikit-Learn接口,所以代码上有所差异。至此,基本了解了利用GBDT(XGBoost)构造组合特征的实现方法,接下去按两种接口实践一波。
2.利用GBDT构造组合特征实践
发车发车~
(1).包导入以及数据准备
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
label | |
---|---|
843 | 1 |
9450 | 0 |
7766 | 1 |
9802 | 1 |
8555 | 1 |
(2).XGBoost两种接口定义
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 1
- 2
- 1
- 2
(3).生成两组新特征
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 1
- 2
- 1
- 2
- 1
- 1
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | … | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 8 | 11 | 9 | 9 | 10 | 8 | 11 | 12 | 9 | 9 | … | 10 | 8 | 10 | 11 | 9 | 10 | 10 | 8 | 11 | 12 |
1 | 10 | 11 | 9 | 11 | 11 | 9 | 11 | 12 | 9 | 9 | … | 10 | 9 | 11 | 11 | 9 | 10 | 10 | 8 | 11 | 13 |
2 | 10 | 11 | 9 | 12 | 10 | 9 | 11 | 12 | 9 | 9 | … | 10 | 9 | 14 | 11 | 10 | 13 | 10 | 8 | 11 | 13 |
3 | 10 | 11 | 9 | 10 | 10 | 9 | 11 | 14 | 9 | 9 | … | 8 | 9 | 11 | 12 | 9 | 10 | 10 | 8 | 13 | 12 |
4 | 12 | 11 | 9 | 9 | 10 | 7 | 11 | 12 | 9 | 10 | … | 10 | 8 | 13 | 11 | 9 | 10 | 10 | 8 | 12 | 12 |
5 rows × 30 columns
(4).基于新特征训练、预测
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
3.Plotting API画图
因为获得的新特征是每棵树的叶子结点的Index,可以看下每棵树的结构。XGBoost Plotting API可以实现:
(1).安装导入相关包: XGBoost Plotting API需要用到graphviz 和pydot,我是Win10 环境+Anaconda3,pydot直接 pip install pydot
或者conda install pydot
即可。graphviz 稍微麻烦点,直接pip(conda)安装了以后导入没有问题,但是画图的时候就会报错,类似路径环境变量的问题。
网上找了一些解决方法,各种试不行,最后在stackoverflow上找到了解决方案:
http://stackoverflow.com/questions/35064304/runtimeerror-make-sure-the-graphviz-executables-are-on-your-systems-path-aft
需要先下载一个windows下的graphviz 安装包,安装完成后将安装路径和bin文件夹路径添加到系统环境变量,然后重启系统。重新pip(conda) install graphviz ,打开jupyter notebook(本次代码都在notebook中测试完成)或者Python环境运行以下代码:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
(2).两种接口的model画图: 上面两种接口的模型分别保存下来,自带接口的参数设置更方便一些。没有深入研究功能,画出来的图效果还不是很好。
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
4.完
余音大神已经把它的代码封装好了,可以直接下载调用,点赞。
在实践中可以根据自己的需求实现特征构造,也不是很麻烦,主要就是保存每个样本在每棵树的叶子索引。然后可以根据情况适当调整参数,得到的新特征再融合到原始特征中,最终是否有提升还是要看场景吧,下次比赛打算尝试一下!
此外,XGBboost Plotting API 之前没用过,感觉很nice,把每个树的样子画出来可以非常直观的观察模型的学习过程,不过本文中的代码画出的图并不是很清晰,还需进一步实践!
参考资料:文中已列出,这里再次感谢!
http://blog.csdn.net/bryan__/article/details/51769118
https://breezedeus.github.io/2014/11/19/breezedeus-feature-mining-gbdt.html#fn:fbgbdt
https://github.com/lytforgood/MachineLearningTrick
http://xgboost.readthedocs.io/en/latest/python/python_api.html#module-xgboost.core