R/random_search.R

#' Perform random search using a paramset.
#' @param resamples A data.frame with columns `splits` and `id`, created using the `rsample` package.
#' @param recipe The recipe to use. See package `recipes`.
#' @param param_set Param set created by calling ParamHelpers::makeParamset.
#' @param n Number of parameter combinations to generate.
#' @param scoring_func Your custom train/predict/score function. 
#' Must take as parameters: 
#' \itemize{
#'     \item a training dataframe
#'     \item the name of the target variable in the training dataframe
#'     \item a list of parameters (these are the hyperparameters we are tuning)
#'     \item an evaluation dataframe
#'     \item dots. These are additional non-tunable parameters that could be passed to the function.
#' }
#' @param ... Optional params passed to train_predict_func.
#' @details `scoring_func` can return a single score as a numeric vector, 
#' or multiple scores in a data.frame. 
#' @return A tidy data.frame, with one column per parameter, columns to identify the
#' paramset and the fold, a column giving the row indices of the evaluation dataset,
#' and columns for the performance scores (these are taken from the scoring function if
#' it returned a data.frame, otherwise it will just be a _score_ column).
#' @export
#' @importFrom ParamHelpers generateRandomDesign dfRowsToList
random_search <- 
  function(resamples, 
           recipe, 
           param_set, 
           n,
           scoring_func, 
           ...,
           verbosity = TRUE){
    
    param_grid_df <- generateRandomDesign(n, param_set, trafo = TRUE)
    
    grid_search(
      resamples = resamples, 
      recipe = recipe, 
      param_grid = param_grid_df, 
      scoring_func = scoring_func, 
      ...,
      verbosity = verbosity
    )
}
artichaud1/tidygrid documentation built on May 10, 2019, 9:28 a.m.