简体   繁体   中英

How to output leaf in pandas dataframe from XGBoost classifier

I have read in a csv file which contains 8 predictive features ( col_list ) and one target feature (Target variable is called " chd " -> 1= Heart Attack; 0 = No Heart Attack).

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

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

I have trained an XGBoost Classifier:

# 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)
    

I have then visualized the tree:

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

在此处输入图像描述

How can I create a column called " leaf " in the df dataframe that contains the values of the terminal leaves shown in the picture above?

You can use xgboost.Booster 's method trees_to_dataframe :

df = model.Booster.trees_to_dataframe()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM