Multilabel classification is a classification problem where multiple target labels can be assigned to each observation instead of only one like in multiclass classification.

Two different approaches exist for multilabel classification. Problem transformation methods try to transform the multilabel classification into binary or multiclass classification problems. Algorithm adaptation methods adapt multiclass algorithms so they can be applied directly to the problem.

Creating a task

The first thing you have to do for multilabel classification in mlr is to get your data in the right format. You need a data.frame which consists of the features and a logical vector for each label which indicates if the label is present in the observation or not. After that you can create a MultilabelTask (Task()) like a normal ClassifTask (Task()). Instead of one target name you have to specify a vector of targets which correspond to the names of logical variables in the data.frame. In the following example we get the yeast data frame from the already existing yeast.task(), extract the 14 label names and create the task again.

Constructing a learner

Multilabel classification in mlr can currently be done in two ways:

  • Algorithm adaptation methods: Treat the whole problem with a specific algorithm.

  • Problem transformation methods: Transform the problem, so that simple binary classification algorithms can be applied.

Algorithm adaptation methods

Currently the available algorithm adaptation methods in R are the multivariate random forest in the [%randomForestSRC] package and the random ferns multilabel algorithm in the [%rFerns] package. You can create the learner for these algorithms like in multiclass classification problems.

Problem transformation methods

For generating a wrapped multilabel learner first create a binary (or multiclass) classification learner with makeLearner(). Afterwards apply a function like makeMultilabelBinaryRelevanceWrapper(), makeMultilabelClassifierChainsWrapper(), makeMultilabelNestedStackingWrapper(), makeMultilabelDBRWrapper() or makeMultilabelStackingWrapper() on the learner to convert it to a learner that uses the respective problem transformation method.

You can also generate a binary relevance learner directly, as you can see in the example.

The different methods are shortly described in the following.

Binary relevance

This problem transformation method converts the multilabel problem to binary classification problems for each label and applies a simple binary classificator on these. In mlr this can be done by converting your binary learner to a wrapped binary relevance multilabel learner.

Classifier chains

Trains consecutively the labels with the input data. The input data in each step is augmented by the already trained labels (with the real observed values). Therefore an order of the labels has to be specified. At prediction time the labels are predicted in the same order as while training. The required labels in the input data are given by the previous done prediction of the respective label.

Nested stacking

Same as classifier chains, but the labels in the input data are not the real ones, but estimations of the labels obtained by the already trained learners.

Dependent binary relevance

Each label is trained with the real observed values of all other labels. In prediction phase for a label the other necessary labels are obtained in a previous step by a base learner like the binary relevance method.

Stacking

Same as the dependent binary relevance method, but in the training phase the labels used as input for each label are obtained by the binary relevance method.

Train

You can train() a model as usual with a multilabel learner and a multilabel task as input. You can also pass subset and weights arguments if the learner supports this.

Predict

Prediction can be done as usual in mlr with predict (predict.WrappedModel()) and by passing a trained model and either the task to the task argument or some new data to the newdata argument. As always you can specify a subset of the data which should be predicted.

pred = predict(mod, task = yeast.task, subset = 1:10)
pred = predict(mod, newdata = yeast[1501:1600,])
names(as.data.frame(pred))
##  [1] "truth.label1"     "truth.label2"     "truth.label3"    
##  [4] "truth.label4"     "truth.label5"     "truth.label6"    
##  [7] "truth.label7"     "truth.label8"     "truth.label9"    
## [10] "truth.label10"    "truth.label11"    "truth.label12"   
## [13] "truth.label13"    "truth.label14"    "prob.label1"     
## [16] "prob.label2"      "prob.label3"      "prob.label4"     
## [19] "prob.label5"      "prob.label6"      "prob.label7"     
## [22] "prob.label8"      "prob.label9"      "prob.label10"    
## [25] "prob.label11"     "prob.label12"     "prob.label13"    
## [28] "prob.label14"     "response.label1"  "response.label2" 
## [31] "response.label3"  "response.label4"  "response.label5" 
## [34] "response.label6"  "response.label7"  "response.label8" 
## [37] "response.label9"  "response.label10" "response.label11"
## [40] "response.label12" "response.label13" "response.label14"

pred2 = predict(mod2, task = yeast.task)
names(as.data.frame(pred2))
##  [1] "id"               "truth.label1"     "truth.label2"    
##  [4] "truth.label3"     "truth.label4"     "truth.label5"    
##  [7] "truth.label6"     "truth.label7"     "truth.label8"    
## [10] "truth.label9"     "truth.label10"    "truth.label11"   
## [13] "truth.label12"    "truth.label13"    "truth.label14"   
## [16] "response.label1"  "response.label2"  "response.label3" 
## [19] "response.label4"  "response.label5"  "response.label6" 
## [22] "response.label7"  "response.label8"  "response.label9" 
## [25] "response.label10" "response.label11" "response.label12"
## [28] "response.label13" "response.label14"

Depending on the chosen predict.type of the learner you get true and predicted values and possibly probabilities for each class label. These can be extracted by the usual accessor functions getPredictionTruth(), getPredictionResponse() and getPredictionProbabilities().

Performance

The performance of your prediction can be assessed via function performance(). You can specify via the measures argument which measure(s) to calculate. The default measure for multilabel classification is the Hamming loss multilabel.hamloss. All available measures for multilabel classification can be shown by listMeasures() and found in the table of performance measures and the ?measures() documentation page.

Resampling

For evaluating the overall performance of the learning algorithm you can do some resampling. As usual you have to define a resampling strategy, either via makeResampleDesc() or makeResampleInstance(). After that you can run the resample() function. Below the default measure Hamming loss is calculated.