简体   繁体   English

如何从 XGBoost 分类器中的 pandas dataframe 中的 output 叶

[英]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).我已阅读 csv 文件,其中包含 8 个预测特征( col_list )和一个目标特征(目标变量称为“ chd ”-> 1 = 心脏病发作;0 = 无心脏病发作)。

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:我训练了一个 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)
    

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?如何在df dataframe 中创建一个名为“ leaf ”的列,其中包含上图中显示的终端叶子的值?

You can use xgboost.Booster 's method trees_to_dataframe :您可以使用xgboost.Booster的方法trees_to_dataframe

df = model.Booster.trees_to_dataframe()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 ipywidgets output 返回 pandas dataframe - How to return a pandas dataframe from an ipywidgets output 使用来自 Pandas DataFrame 的数据拟合 sklearn 的 SVM 分类器 - Fitting sklearn's SVM classifier with data from a pandas DataFrame 在熊猫中,如何将groupby转换的输出发送到原始数据帧? - In Pandas, how to send the output from groupby transform to the original dataframe? 如何从Pandas DataFrame输出带有合并单元格的html表 - How to output html table with merged cells from pandas DataFrame 如何将 pandas dataframe output 从 ZC1C425268E68385D1AB5074FZ9 保存到工作区C17A - How to save pandas dataframe output from function to workspace? 如何将 output 从打印 function 重定向到 pandas Z6A8064B5DF47945050DZ5377C7C4 - How to redirect output from print function into a pandas dataframe? 如何将 output 从 for 循环写入 dataframe pandas 中的列 - How to write output from a for loop to a column in a dataframe pandas 如何从熊猫数据框上的groupby提取特定列的输出 - How to extract output of specific columns from groupby on pandas dataframe Pandas:如何将 cProfile 输出存储在 Pandas DataFrame 中? - Pandas: How to store cProfile output in a pandas DataFrame? 使用分类器列过滤熊猫中的数据框 - Using a classifier column to filter dataframe in pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM