Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Grid Search Cross Validation for XGBoost in R
#' Grid Search
#'
#' @param params data frame of parameters. Each row consists of the usual list of parameters
#' @param data takes an `xgb.DMatrix` or `Matrix` as input.
#' @param nrounds the max number of iterations
#' @param nfold the original dataset is randomly partitioned into `nfold` equal size subsamples
#' @param seed integer
#' @... other parameters to be passed to `xgb.cv`
#'
#' @return
#' @export
#'
#' @import purrr
#' @import tidyr
#'
#' @examples
xgb.gridsearch <- function(params = list(), data, nrounds, nfold, seed = NULL, ...) {
if (!is.null(seed)) {set.seed(seed)}
val <- purrr:::by_row(params, function(x) {xgb.cv(x, data, nrounds, nfold, ...)})
tidyr::unnest(val)
}
@yulijia

This comment has been minimized.

Copy link

@yulijia yulijia commented May 16, 2017

Use purrrlyr instead of purrr:

xgb.gridsearch <- function(params = list(), data, nrounds, nfold, seed = NULL, ...) {
  if (!is.null(seed)) {set.seed(seed)}
  val <- purrrlyr:::by_row(params, function(x) {xgb.cv(x, data, nrounds, nfold, ...)})
  tidyr::unnest(val)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment