3.2 Tasks

Tasks are objects for 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.

3.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.
    mlr3survival::TaskSurv in add-on package mlr3survival
  • 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

3.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. 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:

3.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.

3.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 is either an integer or character. These can be passed as arguments to the $data() method to select specific rows.

The iris task uses integer row_ids:

The mtcars task on the other hand uses names for its row_ids, encoded as character:

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 and 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 here for educational purposes only; in general, there is no point in having a feature that uniquely identifies each row. Further, 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, but only 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 is the default role.
  2. Role validation: Rows 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. 1. 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.