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

How to get access of individual trees of a xgboost model in python/R ?

Below I'm getting from Random Forest trees from 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

解决方案

Do you want to inspect the trees?

In Python, you can dump the trees as a list of strings:

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']

Or dump them to a file (with nice formatting):

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

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值