如何调用xgboost python

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何调用xgboost python相关的知识,希望对你有一定的参考价值。

1
建议大家使用anoconda,里面集成了不少做挖掘、统计相关的包,省去了我们自己安装的麻烦。(主要是考虑到包与包之间有依赖关系,建议用anoconda,numpy、matplotlib这些基础包自动都安装上了)。
在安装完集成开发环境后, 下载xgboost-windows文件,链接如下:
xgboost-windows文件
打开xgboost目录下的windows文件夹,用vs2013以上版本打开xgboost.sln工程(一定要用以上版本,之前我用vs2010打开会出现各种问题),右键项目名称-点击配置管理器-将debug改为release,win32还是win64根据自己的电脑选。

然后右键重新生成解决方案,当在输出窗口出现成功字样后,就表示xgboost的C++版本安装成功了。

打开cmd,进入到xgboost的python-package目录下,我的是这个路径:F:\\Program Files\\annoconda\\xgboost-master\\python-package。cmd命令为:
f:回车
cd \\Program Files\\annoconda\\xgboost-master\\python-package
然后输入python setup.py install 回车
若上述步骤都没问题,此时系统会自动在python-package包里安装xgboost包。
判断xgboost是否成功安装:
import xgboost as xgb
成功导入后,基本就没有问题了。
参考技术A 安装
首先安装XGBoost的C++版本,然后进入源文件的根目录下的 wrappers文件夹执行如下脚本安装Python模块
python setup.py install1

安装完成后按照如下方式导入XGBoost的Python模块
import xgboost as xgb1

=
数据接口
XGBoost可以加载libsvm格式的文本数据,加载的数据格式可以为Numpy的二维数组和XGBoost的二进制的缓存文件。加载的数据存储在对象DMatrix中。
加载libsvm格式的数据和二进制的缓存文件时可以使用如下方式
dtrain = xgb.DMatrix('train.svm.txt')
dtest = xgb.DMatrix('test.svm.buffer')12

加载numpy的数组到DMatrix对象时,可以用如下方式
data = np.random.rand(5,10) # 5 entities, each contains 10 features
label = np.random.randint(2, size=5) # binary target
dtrain = xgb.DMatrix( data, label=label)123

将scipy.sparse格式的数据转化为 DMatrix格式时,可以使用如下方式
csr = scipy.sparse.csr_matrix( (dat, (row,col)) )
dtrain = xgb.DMatrix( csr )12

将 DMatrix 格式的数据保存成XGBoost的二进制格式,在下次加载时可以提高加载速度,使用方式如下
dtrain = xgb.DMatrix('train.svm.txt')
dtrain.save_binary("train.buffer")12

可以用如下方式处理 DMatrix中的缺失值:
dtrain = xgb.DMatrix( data, label=label, missing = -999.0)1

当需要给样本设置权重时,可以用如下方式
w = np.random.rand(5,1)
dtrain = xgb.DMatrix( data, label=label, missing = -999.0, weight=w)12本回答被提问者采纳

如何在 python / R 中访问 xgboost 模型的单个树

【中文标题】如何在 python / R 中访问 xgboost 模型的单个树【英文标题】:How to get access of individual trees of a xgboost model in python /R 【发布时间】:2016-10-07 05:53:57 【问题描述】:

如何在 python/R 中访问 xgboost 模型的单个树?

下面我来自sklearn的随机森林树。

estimator = RandomForestRegressor(
    oob_score=True, 
    n_estimators=10, 
    max_features='auto'
) 
estimator.fit(tarning_data,traning_target) 
tree1 = estimator.estimators_[0]
leftChild = tree1.tree_.children_left  
rightChild = tree1.tree_.children_right 

【问题讨论】:

我也想要一个答案,因为它是置信区间所必需的。我知道一旦你训练了提升模型bst,只需调用 bst.predict(data, pred_leaf=True) 输出将是(n_samples, n_estimators) 的矩阵,每条记录表示每棵树中每个样本的预测叶索引,但不知道如何恢复每棵树的实际预测。 你们搞清楚了吗? 【参考方案1】:

您要检查树木吗?

在 Python 中,您可以将树转储为字符串列表:

m = xgb.XGBClassifier(max_depth=2, n_estimators=3).fit(X, y)
m.get_booster().get_dump()

>

['0:[sincelastrun<23.2917] yes=1,no=2,missing=2\n\t1:[sincelastrun<18.0417] yes=3,no=4,missing=4\n\t\t3:leaf=-0.0965415\n\t\t4:leaf=-0.0679503\n\t2:[sincelastrun<695.025] yes=5,no=6,missing=6\n\t\t5:leaf=-0.0992546\n\t\t6:leaf=-0.0984374\n',
 '0:[sincelastrun<23.2917] yes=1,no=2,missing=2\n\t1:[sincelastrun<16.8917] yes=3,no=4,missing=4\n\t\t3:leaf=-0.0928132\n\t\t4:leaf=-0.0676056\n\t2:[sincelastrun<695.025] yes=5,no=6,missing=6\n\t\t5:leaf=-0.0945284\n\t\t6:leaf=-0.0937463\n',
 '0:[sincelastrun<23.2917] yes=1,no=2,missing=2\n\t1:[sincelastrun<18.175] yes=3,no=4,missing=4\n\t\t3:leaf=-0.0878571\n\t\t4:leaf=-0.0610089\n\t2:[sincelastrun<695.025] yes=5,no=6,missing=6\n\t\t5:leaf=-0.0904395\n\t\t6:leaf=-0.0896808\n']

或者将它们转储到一个文件中(格式很好):

m.get_booster().dump_model("out.txt")

>

booster[0]:
0:[sincelastrun<23.2917] yes=1,no=2,missing=2
    1:[sincelastrun<18.0417] yes=3,no=4,missing=4
        3:leaf=-0.0965415
        4:leaf=-0.0679503
    2:[sincelastrun<695.025] yes=5,no=6,missing=6
        5:leaf=-0.0992546
        6:leaf=-0.0984374
booster[1]:
0:[sincelastrun<23.2917] yes=1,no=2,missing=2
    1:[sincelastrun<16.8917] yes=3,no=4,missing=4
        3:leaf=-0.0928132
        4:leaf=-0.0676056
    2:[sincelastrun<695.025] yes=5,no=6,missing=6
        5:leaf=-0.0945284
        6:leaf=-0.0937463
booster[2]:
0:[sincelastrun<23.2917] yes=1,no=2,missing=2
    1:[sincelastrun<18.175] yes=3,no=4,missing=4
        3:leaf=-0.0878571
        4:leaf=-0.0610089
    2:[sincelastrun<695.025] yes=5,no=6,missing=6
        5:leaf=-0.0904395
        6:leaf=-0.0896808

【讨论】:

又如何分别使用每棵树进行分类和评估每棵树? 一个更容易阅读的东西是model.get_booster().trees_to_dataframe(),它将这个字符串输出到pandas DataFrame中。 为什么要使用单个树?树是按顺序生长的,同时减少了模型的整体误差。它们从来没有像在随机森林中那样单独使用。

以上是关于如何调用xgboost python的主要内容,如果未能解决你的问题,请参考以下文章

Python降低XGBoost 过度拟合的多种方法

Python XGBoost 分类器无法“预测”:“TypeError: Not supported type for data”

如何调用xgboost python

调用 XGBoost .fit 后的 Python sklearn NotFittedError

Visual C++:从 DLL 调用时 XGBoost 不起作用

XGBoost实战:sklearn机器学习调用示例