【问题标题】:xgboost summation of model.apply(x) results is not equal to model.predict(x)model.apply(x) 结果的 xgboost 总和不等于 model.predict(x)
【发布时间】:2021-10-31 13:12:00
【问题描述】:

我是 xgboost 的新手,我想知道它的预测过程的细节。

这是我的测试用例。

我发现model.apply函数可以得到xgb模型中每一棵树的预测。而model.get_booster().get_dump()函数可以找到详细的决策规则以及每棵树的每个叶子节点的得分。

所以我编写了一个解析器来解析所有叶节点 ID 及其相关的预测分数。给定一个输入数据,我首先使用model.apply(x) 得到每棵树的预测,并将它们相加,得到score1。然后我使用model.predict(x) 得到score2。据我了解,score1 和 score2 不应该相等吗?

为什么我会得到如此不同的结果?

以下是我的测试代码。

import xgboost as xgb
model = xgb.XGBRegressor(max_depth=5, learning_rate=0.1, n_estimators=500, silent=False, objective='reg:gamma')
model.load_model('my_checkpoint_file')
x = pandas_data.values
x0 = x[0:1,:]
dict_all_leaf_score = ParseLeafScore(model.get_booster().get_dump())
sum_leaf_score = 0.0
for idx, row in enumerate(model.apply(x0)[0]):
    leaf_score = dict_all_leaf_score[idx][row]
    sum_leaf_score += leaf_score
score = model.predict(x0)
print(score) # 0.32
print(sum_leaf_score) # -0.11

谢谢大家帮助我!!

【问题讨论】:

    标签: python xgboost


    【解决方案1】:

    model = xgb.XGBRegressor(max_depth=5, learning_rate=0.1, n_estimators=500,silent=False,objective='reg:gamma')

    您的 XGBoost 回归模型使用非线性目标函数 (reg:gamma),因此您必须将 exp() 函数应用于您的 sum_leaf_score 值。另外,不要忘记添加基本分数(也称为截距)。

    score = exp(base_score + sum_leaf_score)
    

    【讨论】:

    • 另外,一些 XGBoost 助推器算法 (DART) 使用加权求和而不是求和。
    • 你的意思是在这种情况下, score1 = m.predict(x0) 应该等于 exp(sum_leaf_score) 吗?我试过了,但它不相等。 base_score 是多少?我发现模型有一个名为 intercept 的属性,但我调用了它并且程序告诉我它在这种情况下没有定义。谢谢。
    • score 应该等于exp(sum_leaf_scores + base_score)。如果你不知道base_score 代表什么,那么你应该问谷歌“XGBoost base_score”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-10
    • 1970-01-01
    • 2021-09-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多