【发布时间】:2019-10-30 11:23:24
【问题描述】:
首先我运行一个非常简单的 xgb 回归模型,它只包含 2 棵树,每棵树都有 1 个叶子。数据可用here。 (我知道这是一个分类数据集,但我只是强制回归来证明这里的问题):
import numpy as np
from numpy import loadtxt
from xgboost import XGBClassifier,XGBRegressor
from xgboost import plot_tree
import matplotlib.pyplot as plt
plt.rc('figure', figsize=[10,7])
# load data
dataset = loadtxt('pima-indians-diabetes.csv', delimiter=",")
# split data into X and y
X = dataset[:,0:8]
y = dataset[:,8]
# fit model no training data
model = XGBRegressor(max_depth=0, learning_rate=0.1, n_estimators=2,random_state=123)
model.fit(X, y)
绘制树,我们看到 2 棵树给出的预测值为 -0.0150845 和 -0.013578
plot_tree(model, num_trees=0) # 1ST tree, gives -0.0150845
plot_tree(model, num_trees=1) # 2ND tree, gives -0.013578
但如果我们对第一棵树和两棵树进行预测,它们会给出合理的值:
print(X[0])
print(model.predict(X[0,None],ntree_limit=1)) # 1st tree only
print(model.predict(X[0,None],ntree_limit=0)) # ntree_limit=0: use all trees
# output:
#[ 6. 148. 72. 35. 0. 33.6 0.627 50. ]
#[0.48491547]
#[0.47133744]
所以这里有两个问题:
- 树的预测“-0.0150845”和“-0.013578”与最终输出“0.48491547”和“0.48491547”有何关系?显然这里正在发生一些转变。
- 如果树只有一片叶子,为了最小化平方误差(XGBRegressor 的默认目标),第一棵树不应该只预测约 0.3 的 y 样本均值吗?
更新:
我发现 Q1:XGBRegressor 中有一个 base_score=0.5 默认参数,它改变了预测(这仅在二进制分类问题中才有意义)。
但是对于 Q2,即使在我设置 base_score=0 之后,第一片叶子的值也接近 y 样本均值,但并不准确。所以这里仍然缺少一些东西。
【问题讨论】:
标签: python machine-learning regression prediction xgboost