Skip to contents

Creates a new booster including only a selected range of rounds / iterations from an existing booster, as given by the sequence seq(start, end, step).

Usage

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

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

Arguments

model, x

A fitted xgb.Booster object, which is to be sliced by taking only a subset of its rounds / iterations.

start

Start of the slice (base-1 and inclusive, like R's seq()).

end

End of the slice (base-1 and inclusive, like R's seq()). Passing a value of zero here is equivalent to passing the full number of rounds in the booster object.

step

Step size of the slice. Passing '1' will take every round in the sequence defined by (start, end), while passing '2' will take every second value, and so on.

i

The indices - must be an increasing sequence as generated by e.g. seq(...).

Value

A sliced booster object containing only the requested rounds.

Details

Note that any R attributes that the booster might have, will not be copied into the resulting object.

Examples

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)