0

我读过一个 csv 文件,其中包含 8 个预测特征col_listchd

df = pd.read_csv(loc+'HeartDisease.csv', index_col=0)

Y = df['chd']
col_list = ['sbp','tobacco','ldl','adiposity','typea','obesity','alcohol','age'] 

我训练了一个 XGBoost 分类器:

# fit model no training data
model = XGBClassifier(
    base_score=0.1, 
    booster='gbtree', 
    colsample_bylevel=1,
    colsample_bynode=1, 
    colsample_bytree=0.6,
    enable_categorical=False, 
    gamma=0.1, 
    gpu_id=-1,
    importance_type=None, 
    interaction_constraints='',
    learning_rate=0.1, 
    max_delta_step=0,
    max_depth=8,
    min_child_weight=1, 
    monotone_constraints='(1,1,1,1,1,1,1,1)',#,"(1,-1)"
    n_estimators=4, n_jobs=1, 
    nthread=1, 
    num_parallel_tree=1,
    predictor='auto',
    random_state=0, 
    reg_alpha=0, 
    reg_lambda=1,
    scale_pos_weight=1, 
    silent=True, 
    subsample=0.6,
    tree_method='exact',
    validate_parameters=1, 
    verbosity=None)
    

然后我将树可视化:

fig, ax = plt.subplots(figsize=(30, 30))
plot_tree(model,ax=ax)
plt.show()

在此处输入图像描述

如何在数据框中创建一个名为“ leaf”的列df,其中包含上图中显示的终端叶子的值?

4

1 回答 1

1

您可以使用xgboost.Booster's 方法trees_to_dataframe

df = model.Booster.trees_to_dataframe()
于 2022-02-03T21:28:17.060 回答