模型

模型切片

booster 设置为 gbtreedart 时,XGBoost 构建树模型,它是一个树的列表,可以被切片成多个子模型。

from sklearn.datasets import make_classification
num_classes = 3
X, y = make_classification(n_samples=1000, n_informative=5,
                           n_classes=num_classes)
dtrain = xgb.DMatrix(data=X, label=y)
num_parallel_tree = 4
num_boost_round = 16
# total number of built trees is num_parallel_tree * num_classes * num_boost_round

# We build a boosted random forest for classification here.
booster = xgb.train({
    'num_parallel_tree': 4, 'subsample': 0.5, 'num_class': 3},
                    num_boost_round=num_boost_round, dtrain=dtrain)

# This is the sliced model, containing [3, 7) forests
# step is also supported with some limitations like negative step is invalid.
sliced: xgb.Booster = booster[3:7]

# Access individual tree layer
trees = [_ for _ in booster]
assert len(trees) == num_boost_round

切片后的模型是选定树的副本,这意味着模型本身在切片过程中是不可变的。此功能是早期停止回调中 save_best 选项的基础。有关如何结合使用预测与切片树的示例,请参见 使用单独树和模型切片进行预测的演示

注意

返回的模型切片不包含诸如 best_iterationbest_score 的属性。