2.2 Tasks

Tasks are objects that contain the data and additional meta-data for a machine learning problem. The meta-data is for example the name of the target variable (the prediction) for supervised machine learning problems, or the type of the dataset (e.g. a spatial or survival). This information is used for specific operations that can be performed on a task.

2.2.1 Task Types

To create a task from a data.frame() or data.table() object, the task type needs to be specified:

Classification Task: The target is a label (stored as character()orfactor()) with only few distinct values.

Regression Task: The target is a numeric quantity (stored as integer() or double()).

Survival Task: The target is the (right-censored) time to an event.
mlr3proba::TaskSurv in add-on package mlr3proba

Ordinal Regression Task: The target is ordinal.
mlr3ordinal::TaskOrdinal in add-on package mlr3ordinal

Cluster Task: An unsupervised task type; there is no target and the aim is to identify similar groups within the feature space.
→ Not yet implemented

Spatial Task: Observations in the task have spatio-temporal information (e.g. coordinates).
→ Not yet implemented, but started in add-on package mlr3spatiotemporal

2.2.2 Task Creation

As an example, we will create a regression task using the mtcars data set from the package datasets and predict the target "mpg" (miles per gallon). We only consider the first two features in the dataset for brevity.

First, we load and prepare the data.

Next, we create the task using the constructor for a regression task object (TaskRegr$new) and give the following information:

  1. id: An arbitrary identifier for the task, used in plots and summaries.
  2. backend: This parameter allows fine-grained control over how data is accessed. Here, we simply provide the dataset which is automatically converted to a DataBackendDataTable. Alternatively, we could also construct a DataBackend manually.
  3. target: The name of the target column for the regression problem.

The print() method gives a short summary of the task: It has 32 observations and 3 columns, of which 2 are features.

We can also plot the task using the mlr3viz package, which gives a graphical summary of its properties:

2.2.3 Predefined tasks

mlr3 ships with a few predefined machine learning tasks. All tasks are stored in an R6 Dictionary (a key-value store) named mlr_tasks. Printing it gives the keys (the names of the datasets):

We can get a more informative summary of the example tasks by converting the dictionary to a data.table() object:

To get a task from the dictionary, one can use the $get() method from the mlr_tasks class and assign the return value to a new object. For example, to use the iris data set for classification:

Alternatively, you can also use the convenience function tsk(), which also constructs a task from the dictionary.

2.2.4 Task API

All task properties and characteristics can be queried using the task’s public fields and methods (see Task). Methods are also used to change the behavior of the task. Retrieving Data

The data stored in a task can be retrieved directly from fields, for example:

More information can be obtained through methods of the object, for example:

In mlr3, each row (observation) has a unique identifier, stored as an intger(). These can be passed as arguments to the $data() method to select specific rows:

Note that the method $data() only allows to read the data and does not modify it.

Similarly, each column has an identifier or name. These names are stored in the public slots feature_names and target_names. Here “target” refers to the variable we want to predict and “feature” to the predictors for the task.

The row_ids and column names can be combined when selecting a subset of the data:

To extract the complete data from the task, one can simply convert it to a data.table: Roles (Rows and Columns)

It is possible to assign roles to rows and columns. These roles affect the behavior of the task for different operations. Furthermore, these roles provide additional meta-data for it.

For example, the previously-constructed mtcars task has the following column roles:

To add the row names of mtcars as an additional feature, we first add them to the data table and then recreate the task.

The row names are now a feature whose values are stored in the column “rn”. We include this column here for educational purposes only. Generally speaking, there is no point in having a feature that uniquely identifies each row. Furthermore, the character data type will cause problems with many types of machine learning algorithms. The identifier may be useful to label points in plots and identify outliers however. To use the new column for only this purpose, we will change the role of the “rn” column and remove it from the set of active features.

Changing the role does not change the underlying data. Changing the role only changes the view on it. The data is not copied in the code above. The view is changed in-place though, i.e. the task object itself is modified.

Just like columns, it is also possible to assign different roles to rows. Rows can have two different roles:

  1. Role use: Rows that are generally available for model fitting (although they may also be used as test set in resampling). This role is the default role.

  2. Role validation: Rows that are not used for training. Rows that have missing values in the target column during task creation are automatically set to the validation role.

There are several reasons to hold some observations back or treat them differently:

  1. It is often good practice to validate the final model on an external validation set to identify possible overfitting.
  2. Some observations may be unlabeled, e.g. in competitions like Kaggle.

These observations cannot be used for training a model, but can be used to get predictions. Task Mutators

As shown above, modifying $col_roles or $row_roles changes the view on the data. The additional convenience method $filter() subsets the current view based on row ids and $select() subsets the view based on feature names.

While the methods discussed above allow to subset the data, the methods $rbind() and $cbind() allow to add extra rows and columns to a task. Again, the original data is not changed. The additional rows or columns are only added to the view of the data.

2.2.5 Plotting Tasks

The mlr3viz package provides plotting facilities for many classes implemented in mlr3. The available plot types depend on the inherited class, but all plots are returned as ggplot2 objects which can be easily customized.

For classification tasks (inheriting from TaskClassif), see the documentation of mlr3viz::autoplot.TaskClassif for the implemented plot types. Here are some examples to get an impression:

Of course, you can do the same for regression tasks (inheriting from TaskRegr) as documented in mlr3viz::autoplot.TaskRegr: