3.2 Learners

Objects of class mlr3::Learner provide a unified interface to many popular machine learning algorithms in R. They consist of methods to train and predict on a mlr3::Task, and additionally provide meta information about the algorithms.

The package ships with only a rather minimal set of classification and regression learners, more are implemented in the mlr3learners package. Furthermore, mlr3learners has some documentation on creating custom learners.

3.2.1 Predefined Learners

Analogously to mlr3::mlr_tasks, the mlr3::Dictionary mlr3::mlr_learners can be queried for available learners:

mlr_learners
## <DictionaryLearner> with 5 stored values
## Keys: classif.debug, classif.featureless,
##   classif.rpart, regr.featureless, regr.rpart
as.data.table(mlr_learners)
##                    key
## 1:       classif.debug
## 2: classif.featureless
## 3:       classif.rpart
## 4:    regr.featureless
## 5:          regr.rpart
##                                       feature_types
## 1: logical,integer,numeric,character,factor,ordered
## 2: logical,integer,numeric,character,factor,ordered
## 3: logical,integer,numeric,character,factor,ordered
## 4: logical,integer,numeric,character,factor,ordered
## 5: logical,integer,numeric,character,factor,ordered
##    packages
## 1:         
## 2:         
## 3:    rpart
## 4:    stats
## 5:    rpart
##                                                           properties
## 1:                                            missings,updates_model
## 2:         importance,missings,multiclass,selected_features,twoclass
## 3: importance,missings,multiclass,selected_features,twoclass,weights
## 4:                             importance,missings,selected_features
## 5:                     importance,missings,selected_features,weights
##    predict_types
## 1: response,prob
## 2: response,prob
## 3: response,prob
## 4:   response,se
## 5:      response

As listed in the output, each learner comes with the following annotations:

  • feature_types: what kind of features can be processed.
  • packages: which packages are required to run train() and predict().
  • properties: additional properties and capabilities. E.g., a learner has the property “missings” if it is able to handle missing values natively, and “importance” if it is possible to extract feature importance values.
  • predict_types: what predict types are possible. E.g., a classification learner can predict labels (“response”) or probabilities (“prob”)

To extract a specific learner, use the corresponding “id”:

learner = mlr_learners$get("classif.rpart")
learner
## <LearnerClassifRpart:classif.rpart>
## Parameters: list()
## Packages: rpart
## Predict Type: response
## Feature types: logical, integer, numeric,
##   character, factor, ordered
## Properties: importance, missings, multiclass,
##   selected_features, twoclass, weights

As the printer shows, all information from the previous table is also accessible via public fields (id, feature_types, packages, properties, predict_types) Additionally, predict_type returns the currently selected predict type of the learner.

The field param_set stores a description of hyperparameter settings:

learner$param_set
## ParamSet: paramset
##              id    class lower upper levels default
## 1:     minsplit ParamInt     1   Inf             20
## 2:           cp ParamDbl     0     1           0.01
## 3:   maxcompete ParamInt     0   Inf              4
## 4: maxsurrogate ParamInt     0   Inf              5
## 5:     maxdepth ParamInt     1    30             30
## 6:         xval ParamInt     0   Inf             10
##    value
## 1:      
## 2:      
## 3:      
## 4:      
## 5:      
## 6:

The set of hyperparamter values is stored inside the parameter set in the field values. By assigning a named list to this field, we change the active hyperparameters of the learner:

learner$param_set$values = list(cp = 0.01)
learner
## <LearnerClassifRpart:classif.rpart>
## Parameters: cp=0.01
## Packages: rpart
## Predict Type: response
## Feature types: logical, integer, numeric,
##   character, factor, ordered
## Properties: importance, missings, multiclass,
##   selected_features, twoclass, weights

The field model stores the result of the training step. As we have not yet learned a model, this is NULL:

learner$model
## NULL

3.2.2 Train and Predict

To train the learner on a task, the packages uses the mlr3::Experiment class.