LearnerXgboost: R6 Class to construct a Xgboost learner

LearnerXgboostR Documentation

R6 Class to construct a Xgboost learner

Description

The LearnerXgboost class is the interface to the xgboost R package for use with the mlexperiments package.

Details

Optimization metric: needs to be specified with the learner parameter eval_metric. The following options can be set via options():

  • "mlexperiments.optim.xgb.nrounds" (default: 5000L)

  • "mlexperiments.optim.xgb.early_stopping_rounds" (default: 500L)

  • "mlexperiments.xgb.print_every_n" (default: 50L)

  • "mlexperiments.xgb.verbose" (default: FALSE)

LearnerXgboost can be used with

  • mlexperiments::MLTuneParameters

  • mlexperiments::MLCrossValidation

  • mlexperiments::MLNestedCV

Super class

mlexperiments::MLLearnerBase -> LearnerXgboost

Methods

Public methods

Inherited methods

Method new()

Create a new LearnerXgboost object.

Usage
LearnerXgboost$new(metric_optimization_higher_better)
Arguments
metric_optimization_higher_better

A logical. Defines the direction of the optimization metric used throughout the hyperparameter optimization.

Returns

A new LearnerXgboost R6 object.

Examples
if (requireNamespace("xgboost", quietly = TRUE)) {
  LearnerXgboost$new(metric_optimization_higher_better = FALSE)
}


Method clone()

The objects of this class are cloneable with this method.

Usage
LearnerXgboost$clone(deep = FALSE)
Arguments
deep

Whether to make a deep clone.

See Also

xgboost::xgb.train(), xgboost::xgb.cv()

Examples

if (requireNamespace("xgboost", quietly = TRUE) &&
requireNamespace("mlbench", quietly = TRUE) &&
requireNamespace("measures", quietly = TRUE)) {

  # binary classification
  Sys.setenv("OMP_THREAD_LIMIT" = 2)

  library(mlbench)
  data("PimaIndiansDiabetes2")
  dataset <- PimaIndiansDiabetes2 |>
    data.table::as.data.table() |>
    na.omit()

  seed <- 123
  feature_cols <- colnames(dataset)[1:8]

  param_list_xgboost <- expand.grid(
     subsample = seq(0.6, 1, .2),
     colsample_bytree = seq(0.6, 1, .2),
     min_child_weight = seq(1, 5, 4),
     learning_rate = seq(0.1, 0.2, 0.1),
     max_depth = seq(1, 5, 4),
     nthread = 2
  )

  train_x <- model.matrix(
    ~ -1 + .,
    dataset[, .SD, .SDcols = feature_cols]
  )
  train_y <- as.integer(dataset[, get("diabetes")]) - 1L

  fold_list <- splitTools::create_folds(
    y = train_y,
    k = 3,
    type = "stratified",
    seed = seed
  )
  xgboost_cv <- mlexperiments::MLCrossValidation$new(
    learner = mllrnrs::LearnerXgboost$new(
      metric_optimization_higher_better = FALSE
    ),
    fold_list = fold_list,
    ncores = 2L,
    seed = 123
  )
  xgboost_cv$learner_args <- c(
    as.list(
      data.table::data.table(
        param_list_xgboost[37, ],
        stringsAsFactors = FALSE
      ),
    ),
    list(
      objective = "binary:logistic",
      eval_metric = "logloss"
    ),
    nrounds = 45L
  )
  xgboost_cv$performance_metric_args <- list(positive = "1", negative = "0")
  xgboost_cv$performance_metric <- mlexperiments::metric("AUC")

  # set data
  xgboost_cv$set_data(
    x = train_x,
    y = train_y
  )

  xgboost_cv$execute()
}


## ------------------------------------------------
## Method `LearnerXgboost$new`
## ------------------------------------------------

if (requireNamespace("xgboost", quietly = TRUE)) {
  LearnerXgboost$new(metric_optimization_higher_better = FALSE)
}


mllrnrs documentation built on Jan. 17, 2026, 9:06 a.m.