R/as_task_unsupervised.R

Defines functions as_tasks_unsupervised.Task as_tasks_unsupervised.list as_tasks_unsupervised as_task_unsupervised.DataBackend as_task_unsupervised.data.frame as_task_unsupervised.Task as_task_unsupervised

Documented in as_tasks_unsupervised as_tasks_unsupervised.list as_tasks_unsupervised.Task as_task_unsupervised as_task_unsupervised.DataBackend as_task_unsupervised.data.frame as_task_unsupervised.Task

#' @title Convert to an Unsupervised Task
#'
#' @description
#' Convert object to a [TaskUnsupervised] or a list of [TaskUnsupervised].
#'
#' @inheritParams as_task
#'
#' @export
as_task_unsupervised = function(x, ...) {
  UseMethod("as_task_unsupervised")
}

#' @rdname as_task_unsupervised
#' @export
as_task_unsupervised.Task = function(x, clone = FALSE, ...) { # nolint
  if (clone) x$clone() else x
}

#' @rdname as_task_unsupervised
#' @param id (`character(1)`)\cr
#'   Id for the new task.
#'   Defaults to the (deparsed and substituted) name of the data argument.
#' @template param_label
#' @export
as_task_unsupervised.data.frame = function(x, id = deparse(substitute(x)), label = NA_character_, ...) { # nolint
  force(id)

  ii = which(map_lgl(keep(x, is.double), anyInfinite))
  if (length(ii)) {
    warningf("Detected columns with unsupported Inf values in data: %s", str_collapse(names(ii)))
  }

  TaskUnsupervised$new(id = id, backend = x, label = label)
}

#' @rdname as_task_unsupervised
#' @export
as_task_unsupervised.DataBackend = function(x, id = deparse(substitute(x)), label = NA_character_, ...) { # nolint
  force(id)

  TaskUnsupervised$new(id = id, backend = x, label = label)
}

#' @rdname as_task_unsupervised
#' @export
as_tasks_unsupervised = function(x, ...) {
  UseMethod("as_tasks")
}

#' @rdname as_task_unsupervised
#' @param clone (`logical(1)`)\cr
#'   If `TRUE`, ensures that the returned object is not the same as the input `x`.
#' @export
as_tasks_unsupervised.list = function(x, clone = FALSE, ...) { # nolint
  lapply(x, as_task, clone = clone, ...)
}

#' @rdname as_task_unsupervised
#' @export
as_tasks_unsupervised.Task = function(x, clone = FALSE, ...) { # nolint
  list(if (clone) x$clone() else x)
}

Try the mlr3 package in your browser

Any scripts or data that you put into this service are public.

mlr3 documentation built on Nov. 17, 2023, 5:07 p.m.