这是可能的,但并不容易。我建议您使用来自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')]
您可以根据需要进一步处理此列表。在