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]
#>                     [,1] [,2] [,3]
#> Mazda RX4             14   14   14
#> Mazda RX4 Wag         14   14   14
#> Datsun 710             7    7    7
#> Hornet 4 Drive        14   14   14
#> Hornet Sportabout     16   18   11
#> Valiant               10   16   16
#> Duster 360            12   19   11
#> Merc 240D              7    7    7
#> Merc 230               7    7    7
#> Merc 280              10   15   15
#> Merc 280C             10   16   16
#> Merc 450SE            15   17   11
#> Merc 450SL            15   17   11
#> Merc 450SLC           15   17   11
#> Cadillac Fleetwood    12   19   12
#> Lincoln Continental   12   19   12
#> Chrysler Imperial     12   19   11
#> Fiat 128               3    3    3
#> Honda Civic            3    3    3
#> Toyota Corolla         3    3    3
#> Toyota Corona          8    8    8
#> Dodge Challenger      15   17   11
#> AMC Javelin           15   17   11
#> Camaro Z28            12   19   11
#> Pontiac Firebird      16   18   11
#> Fiat X1-9              3    3    3
#> Porsche 914-2          3    7    7
#> Lotus Europa           3    3    3
#> Ford Pantera L        12   20   11
#> Ferrari Dino          13   13   13
#> Maserati Bora         12   19   11
#> Volvo 142E             8    8    8

# The new model has only those rounds, so
# a full prediction from it is equivalent
predict(model_slice, x, predleaf = TRUE)
#>                     [,1] [,2] [,3]
#> Mazda RX4             14   14   14
#> Mazda RX4 Wag         14   14   14
#> Datsun 710             7    7    7
#> Hornet 4 Drive        14   14   14
#> Hornet Sportabout     16   18   11
#> Valiant               10   16   16
#> Duster 360            12   19   11
#> Merc 240D              7    7    7
#> Merc 230               7    7    7
#> Merc 280              10   15   15
#> Merc 280C             10   16   16
#> Merc 450SE            15   17   11
#> Merc 450SL            15   17   11
#> Merc 450SLC           15   17   11
#> Cadillac Fleetwood    12   19   12
#> Lincoln Continental   12   19   12
#> Chrysler Imperial     12   19   11
#> Fiat 128               3    3    3
#> Honda Civic            3    3    3
#> Toyota Corolla         3    3    3
#> Toyota Corona          8    8    8
#> Dodge Challenger      15   17   11
#> AMC Javelin           15   17   11
#> Camaro Z28            12   19   11
#> Pontiac Firebird      16   18   11
#> Fiat X1-9              3    3    3
#> Porsche 914-2          3    7    7
#> Lotus Europa           3    3    3
#> Ford Pantera L        12   20   11
#> Ferrari Dino          13   13   13
#> Maserati Bora         12   19   11
#> Volvo 142E             8    8    8