python建模 决策_如何从python3的xgboost模型中提取决策规则(特征分割)?

这是可能的,但并不容易。我建议您使用来自scikit-learn的GradientBoostingClassifier,它与xgboost相似,但可以本地访问已构建的树。在

{然而,{可以用cd3}来解析它:from sklearn.datasets import load_iris

from xgboost import XGBClassifier

# build a very simple model

X, y = load_iris(return_X_y=True)

model = XGBClassifier(max_depth=2, n_estimators=2)

model.fit(X, y);

# dump it to a text file

model.get_booster().dump_model('xgb_model.txt', with_stats=True)

# read the contents of the file

with open('xgb_model.txt', 'r') as f:

txt_model = f.read()

print(txt_model)

它将打印6棵树的文本描述(2个估计器,每个树由3棵树组成,每个类一棵),其开头如下:

^{pr2}$

例如,现在可以从该描述中提取所有拆分:import re

# trying to extract all patterns like "[f2<2.45]"

splits = re.findall('\[f([0-9]+)

splits

它将打印元组列表(feature_id,split_value),如[('2', '2.45'),

('2', '2.45'),

('3', '1.75'),

('3', '1.65'),

('2', '4.95'),

('2', '2.45'),

('2', '2.45'),

('3', '1.75'),

('3', '1.65'),

('2', '4.95')]

您可以根据需要进一步处理此列表。在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值