跳到内容

创建一个新的 booster,其中只包含现有 booster 中由序列 seq(start, end, step) 给定的选定轮次/迭代范围。

用法

xgb.slice.Booster(
  model,
  start,
  end = xgb.get.num.boosted.rounds(model),
  step = 1L
)

# S3 method for class 'xgb.Booster'
x[i]

参数

model, x

一个已拟合的 xgb.Booster 对象,将通过仅截取其轮次/迭代的一个子集来切片。

start

切片的起始(从 1 开始且包含,类似于 R 的 seq())。

end

切片的结束(从 1 开始且包含,类似于 R 的 seq())。此处传递零值等同于传递 booster 对象中的全部轮次。

step

切片的步长。传递“1”将取 (start, end) 定义的序列中的每个轮次,而传递“2”将取每隔一个值,以此类推。

i

索引——必须是例如由 seq(...) 生成的递增序列。

一个只包含请求轮次的切片 booster 对象。

详细信息

请注意,booster 可能拥有的任何 R 属性都不会复制到结果对象中。

示例

data(mtcars)

y <- mtcars$mpg
x <- as.matrix(mtcars[, -1])

dm <- xgb.DMatrix(x, label = y, nthread = 1)
model <- xgb.train(data = dm, params = xgb.params(nthread = 1), nrounds = 5)
model_slice <- xgb.slice.Booster(model, 1, 3)
# Prediction for first three rounds
predict(model, x, predleaf = TRUE)[, 1:3]

# The new model has only those rounds, so
# a full prediction from it is equivalent
predict(model_slice, x, predleaf = TRUE)