簡體   English   中英

如何從 XGBoost 分類器中的 pandas dataframe 中的 output 葉

[英]How to output leaf in pandas dataframe from XGBoost classifier

我已閱讀 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'] 

我訓練了一個 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()

在此處輸入圖像描述

如何在df dataframe 中創建一個名為“ leaf ”的列,其中包含上圖中顯示的終端葉子的值?

您可以使用xgboost.Booster的方法trees_to_dataframe

df = model.Booster.trees_to_dataframe()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM