首页 > 解决方案 > 如何在 python 的 lightgbm 中计算 split_gain

问题描述

我想知道 lightgbm 如何计算 split_gain。我split_gain = sum_grad / sum_hess这里看到的。

但是,我看到那不是真的。来源如下。

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from lightgbm import LGBMRegressor
from lightgbm.plotting import *

d = pd.DataFrame({"x1":[-2, -1, 0, 1, 2],"y":[4, 1, 0, 1, 4]})
def custom_asymmetric_train(y_true, y_pred):
    residual = (y_true - y_pred).astype("float")
    grad = np.where(residual<0, -2*10.0*residual, -2*residual)
    hess = np.where(residual<0, 2*10.0, 2)
    print(grad, hess, sum(grad), sum(hess))
    return grad, hess
l2 = LGBMRegressor(min_child_samples=1, min_child_weight=0, n_estimators=5, max_depth=1, learning_rate=1, min_gain_to_split=0, objective=custom_asymmetric_train)

l2.fit(d[["x1"]], d[["y"]].values.ravel())
create_tree_digraph(booster=l2, show_info=['split_gain', 'internal_value', 'internal_count', 'leaf_count'], tree_index=0)

输出是

[-8. -2. -0. -2. -8.] [2. 2. 2. 2. 2.] -20.0 10.0
[-4.66666667 13.33333333 33.33333333 30.         -3.        ] [ 2. 20. 20. 20.  2.] 68.99999999999999 64.0
[-5.45454544  5.4545456   4.60317521  1.26984188 -5.87301581] [ 2. 20. 20. 20.  2.] 1.4305114603985203e-06 64.0
[-5.67374426  3.26255743  2.41118704  5.45454549 -5.45454545] [ 2. 20. 20. 20.  2.] 2.3841856311435095e-07 64.0
[-5.45454547  5.45454533  1.26300278  4.30636123 -5.56936388] [ 2. 20. 20. 20.  2.] -1.509903313490213e-14 64.0

在此处输入图像描述

我尝试了另一种情况,但我不知道 lgbm 是如何计算分割增益的。

请告诉我。

标签: pythonscikit-learnlightgbm

解决方案


推荐阅读