我试图在xgboost中实现一个增强的Poisson回归模型,但是我发现结果在低频率下是有偏差的。为了说明这一点,下面是一些我认为复制了这个问题的Python代码:
import numpy as np
import pandas as pd
import xgboost as xgb
def get_preds(mult):
# generate toy dataset for illustration
# 4 observations with linearly increasing frequencies
# the frequencies are scaled by `mult`
dmat = xgb.DMatrix(data=np.array([[0, 0], [0, 1], [1, 0], [1, 1]]),
label=[i*mult for i in [1, 2, 3, 4]],
weight=[1000, 1000, 1000, 1000])
# train a poisson booster on the toy data
bst = xgb.train(
params={"objective": "count:poisson"},
dtrain=dmat,
num_boost_round=100000,
early_stopping_rounds=5,
evals=[(dmat, "train")],
verbose_eval=False)
# return fitted frequencies after reversing scaling
return bst.predict(dmat)/mult
# test multipliers in the range [10**(-8), 10**1]
# display fitted frequencies
mults = [10**i for i in range(-8, 1)]
df = pd.DataFrame(np.round(np.vstack([get_preds(m) for m in mults]), 0))
df.index = mults
df.columns = ["(0, 0)", "(0, 1)", "(1, 0)", "(1, 1)"]
df
# --- result ---
# (0, 0) (0, 1) (1, 0) (1, 1)
#1.000000e-08 11598.0 11598.0 11598.0 11598.0
#1.000000e-07 1161.0 1161.0 1161.0 1161.0
#1.000000e-06 118.0 118.0 118.0 118.0
#1.000000e-05 12.0 12.0 12.0 12.0
#1.000000e-04 2.0 2.0 3.0 3.0
#1.000000e-03 1.0 2.0 3.0 4.0
#1.000000e-02 1.0 2.0 3.0 4.0
#1.000000e-01 1.0 2.0 3.0 4.0
#1.000000e+00 1.0 2.0 3.0 4.0
请注意,在低频率下,预测似乎会爆炸。这可能与Poisson-lambda*权重下降到1以下有关(事实上,将权重增加到1000以上确实会将“爆炸”转移到较低的频率),但我仍然希望预测接近平均训练频率(2.5)。另外(在上面的例子中没有显示),减少eta
似乎会增加预测中的偏差量。在
什么会导致这种情况发生?是否有一个可以减轻影响的参数?在
经过一番挖掘,我找到了解决办法。在这里记录,以防其他人遇到相同的问题。结果我需要加上一个偏移项,等于平均频率的(自然)对数。如果这还不是很明显,那是因为初始预测从0.5的频率开始,并且需要许多增强迭代来将预测重新调整到平均频率。在
请参阅下面的代码以获取对玩具示例的更新。正如我在最初的问题中建议的那样,现在的预测在较低的尺度上接近平均频率(2.5)。在
相关问题 更多 >
编程相关推荐