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

如何在python/R中访问xgboost模型的各个树,python,r,machine-learning,scikit-learn,xgboost,Python,R,Machine Learning,Scikit Learn,Xgboost,如何在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

如何在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 

你想检查一下这些树吗

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

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

>

booster[0]:

0:[sincelastrunI也希望得到这个问题的答案,因为它对于置信区间是必要的。我知道,一旦你训练了增强模型
bst
,只需调用bst.predict(data,pred\u leaf=True),输出将是
矩阵(n\u样本,n\u估计器)
每个记录都表示每棵树中每个样本的预测叶指数,但不知道如何恢复每棵树的实际预测。你们知道了吗?如何分别使用每棵树对每棵树进行分类和评估?
['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