This task specializes Task and TaskSupervised for classification problems. The target column is assumed to be a factor. The task_type is set to "classif".

Additional task properties include:

  • "twoclass": The task is a binary classification problem.

  • "multiclass": The task is a multiclass classification problem.

Predefined tasks are stored in the dictionary mlr_tasks.

See also

Super classes

mlr3::Task -> mlr3::TaskSupervised -> TaskClassif

Active bindings

class_names

(character())
Returns all class labels of the target column.

positive

(character(1))
Stores the positive class for binary classification tasks, and NA for multiclass tasks. To switch the positive class, assign a level to this field.

negative

(character(1))
Stores the negative class for binary classification tasks, and NA for multiclass tasks.

Methods

Public methods

Inherited methods

Method new()

Creates a new instance of this R6 class.

Usage

TaskClassif$new(id, backend, target, positive = NULL, extra_args = list())

Arguments

id

(character(1))
Identifier for the new instance.

backend

(DataBackend)
Either a DataBackend, or any object which is convertible to a DataBackend with as_data_backend(). E.g., a data.frame() will be converted to a DataBackendDataTable.

target

(character(1))
Name of the target column.

positive

(character(1))
Only for binary classification: Name of the positive class. The levels of the target columns are reordered accordingly, so that the first element of $class_names is the positive class, and the second element is the negative class.

extra_args

(named list())
Named list of constructor arguments, required for converting task types via convert_task().


Method data()

Calls $data from parent class Task and ensures that levels of the target column are in the right order.

Usage

TaskClassif$data(
  rows = NULL,
  cols = NULL,
  data_format = "data.table",
  ordered = TRUE
)

Arguments

rows

integer()
Row indices.

cols

character()
Column names.

data_format

(character(1))
Desired data format, e.g. "data.table" or "Matrix".

ordered

(logical(1))
If TRUE (default), data is ordered according to the columns with column role "order".

Returns

Depending on the DataBackend, but usually a data.table::data.table().


Method truth()

True response for specified row_ids. Format depends on the task type. Defaults to all rows with role "use".

Usage

TaskClassif$truth(rows = NULL)

Arguments

rows

integer()
Row indices.

Returns

factor().


Method droplevels()

Updates the cache of stored factor levels, removing all levels not present in the current set of active rows. cols defaults to all columns with storage type "factor" or "ordered". Also updates the task property "twoclass"/"multiclass".

Usage

TaskClassif$droplevels(cols = NULL)

Arguments

cols

character()
Column names.

Returns

Modified self.


Method clone()

The objects of this class are cloneable with this method.

Usage

TaskClassif$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.

Examples

data("Sonar", package = "mlbench") task = TaskClassif$new("sonar", backend = Sonar, target = "Class", positive = "M") task$task_type
#> [1] "classif"
task$formula()
#> Class ~ . #> NULL
task$truth()
#> [1] R R R R R R R R R R R R R R R R R R R R R R R R R R R R R R R R R R R R R #> [38] R R R R R R R R R R R R R R R R R R R R R R R R R R R R R R R R R R R R R #> [75] R R R R R R R R R R R R R R R R R R R R R R R M M M M M M M M M M M M M M #> [112] M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M #> [149] M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M #> [186] M M M M M M M M M M M M M M M M M M M M M M M #> Levels: M R
task$class_names
#> [1] "M" "R"
task$positive
#> [1] "M"
# possible properties: mlr_reflections$task_properties$classif
#> [1] "strata" "groups" "weights" "twoclass" "multiclass"