R/autoDummy.R

Defines functions autoDummy

Documented in autoDummy

#' @title Automatically replace factors with dummy features.
#'
#' @description
#' Checks a task to see if it contains factors or ordered factors, then checks a
#' learner to see if it is unable to handle factors. If both are true, returns
#' the learner wrapped with \code{\link[mlr]{makeDummyFeaturesWrapper}} which
#' perfors one-hot encoding, i.e. creates a separate column for each factor
#' level with a 1 indicating that observation contained the factor level in
#' question, and a zero indicating otherwise.
#'
#' @param wl (`WrappedLearner`) An ezmlr WrappedLearner object.
#' @param task (`Task`) An mlr Task.
#' @param ... Additional arguments to pass to \code{\link[mlr]{makeDummyFeaturesWrapper}}
#'
#' @export
autoDummy = function(wl, task, ...) {

  td = getTaskDesc(task)

  if ((td$n.feat[2] > 0 & !("factors" %in% getLearnerProperties(wl))) |
      (td$n.feat[3] > 0 & !("ordered" %in% getLearnerProperties(wl)))) {
    wl = makeDummyFeaturesWrapper(wl, ...)
  }

  return(wl)
}
Prometheus77/ezmlr documentation built on May 17, 2019, 6:34 p.m.