如何在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()
>
['0:[sincelastrun<23.2917] yes=1,no=2,missing=2nt1:[sincelastrun<18.0417] yes=3,no=4,missing=4ntt3:leaf=-0.0965415ntt4:leaf=-0.0679503nt2:[sincelastrun<695.025] yes=5,no=6,missing=6ntt5:leaf=-0.0992546ntt6:leaf=-0.0984374n',
'0:[sincelastrun<23.2917] yes=1,no=2,missing=2nt1:[sincelastrun<16.8917] yes=3,no=4,missing=4ntt3:leaf=-0.0928132ntt4:leaf=-0.0676056nt2:[sincelastrun<695.025] yes=5,no=6,missing=6ntt5:leaf=-0.0945284ntt6:leaf=-0.0937463n',
'0:[sincelastrun<23.2917] yes=1,no=2,missing=2nt1:[sincelastrun<18.175] yes=3,no=4,missing=4ntt3:leaf=-0.0878571ntt4:leaf=-0.0610089nt2:[sincelastrun<695.025] yes=5,no=6,missing=6ntt5:leaf=-0.0904395ntt6:leaf=-0.0896808n']
或者将它们转储到一个文件中(格式很好):
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