The following classes provide a unified interface to all popular machine learning methods in R: (cost-sensitive) classification, regression, survival analysis, and clustering. Many are already integrated in mlr, others are not, but the package is specifically designed to make extensions simple.

Section integrated learners shows the already implemented machine learning methods and their properties. If your favorite method is missing, either open an issue or take a look at how to integrate a learning method yourself. This basic introduction demonstrates how to use already implemented learners.

Constructing a learner

A learner in mlr is generated by calling makeLearner(). In the constructor you need to specify which learning method you want to use. Moreover, you can:

  • Set hyperparameters.
  • Control the output for later prediction, e.g., for classification whether you want a factor of predicted class labels or probabilities.
  • Set an ID to name the object (some methods will later use this ID to name results or annotate plots).

The first argument specifies which algorithm to use. The naming convention is classif.<R_method_name> for classification methods, regr.<R_method_name> for regression methods, surv.<R_method_name> for survival analysis, cluster.<R_method_name> for clustering methods, and multilabel.<R_method_name> for multilabel classification.

Hyperparameter values can be specified either via the ... argument or as a list via par.vals.

Occasionally, factor features may cause problems when fewer levels are present in the test data set than in the training data. By setting fix.factors.prediction = TRUE these are avoided by adding a factor level for missing data in the test data set.

Let’s have a look at two of the learners created above.

All generated learners are objects of class Learner (makeLearner()). This class contains the properties of the method, e.g., which types of features it can handle, what kind of output is possible during prediction, and whether multi-class problems, observations weights or missing values are supported.

As you might have noticed, there is currently no special learner class for cost-sensitive classification. For ordinary misclassification costs you can use standard classification methods. For example-dependent costs there are several ways to generate cost-sensitive learners from ordinary regression and classification learners. This is explained in greater detail in the section about cost-sensitive classification.

Accessing a learner

The Learner (makeLearner()) object is a list and the following elements contain information regarding the hyperparameters and the type of prediction.

Slot $par.set is an object of class ParamSet (ParamHelpers::makeParamSet()). It contains, among others, the type of hyperparameters (e.g., numeric, logical), potential default values and the range of allowed values.

Moreover, mlr provides function getHyperPars() or its alternative getLearnerParVals() to access the current hyperparameter setting of a Learner (makeLearner()) and getParamSet() to get a description of all possible settings. These are particularly useful in case of wrapped Learner (makeLearner())s, for example if a learner is fused with a feature selection strategy, and both, the learner as well the feature selection method, have hyperparameters. For details see the section on wrapped learners.

We can also use getParamSet() or its alias getLearnerParamSet() to get a quick overview about the available hyperparameters and defaults of a learning method without explicitly constructing it (by calling makeLearner()).

Functions for accessing a Learner’s meta information are available in mlr. We can use getLearnerId(), getLearnerShortName() and getLearnerType() to get Learner’s ID, short name and type, respectively. Moreover, in order to show the required packages for the Learner, one can call getLearnerPackages().

Listing learners

A list of all learners integrated in mlr and their respective properties is shown in the Appendix.

If you would like a list of available learners, maybe only with certain properties or suitable for a certain learning Task() use function listLearners().

# List everything in mlr
lrns = listLearners()
head(lrns[c("class", "package")])
##                 class      package
## 1         classif.ada    ada,rpart
## 2  classif.adaboostm1        RWeka
## 3 classif.bartMachine  bartMachine
## 4    classif.binomial        stats
## 5    classif.boosting adabag,rpart
## 6         classif.bst    bst,rpart

# List classifiers that can output probabilities
lrns = listLearners("classif", properties = "prob")
head(lrns[c("class", "package")])
##                 class      package
## 1         classif.ada    ada,rpart
## 2  classif.adaboostm1        RWeka
## 3 classif.bartMachine  bartMachine
## 4    classif.binomial        stats
## 5    classif.boosting adabag,rpart
## 6         classif.C50          C50

# List classifiers that can be applied to iris (i.e., multiclass) and output probabilities
lrns = listLearners(iris.task, properties = "prob")
head(lrns[c("class", "package")])
##                class      package
## 1 classif.adaboostm1        RWeka
## 2   classif.boosting adabag,rpart
## 3        classif.C50          C50
## 4    classif.cforest        party
## 5      classif.ctree        party
## 6   classif.cvglmnet       glmnet

# The calls above return character vectors, but you can also create learner objects
head(listLearners("cluster", create = TRUE), 2)
## [[1]]
## Learner cluster.cmeans from package e1071,clue
## Type: cluster
## Name: Fuzzy C-Means Clustering; Short name: cmeans
## Class: cluster.cmeans
## Properties: numerics,prob
## Predict-Type: response
## Hyperparameters: centers=2
## 
## 
## [[2]]
## Learner cluster.Cobweb from package RWeka
## Type: cluster
## Name: Cobweb Clustering Algorithm; Short name: cobweb
## Class: cluster.Cobweb
## Properties: numerics
## Predict-Type: response
## Hyperparameters: