4.5 Predicting

After the training step, we can use the experiment to predict on observations of the task (note that you may alternatively also pass new data here as data.frame):

e$predict(row_ids = test_set)
## INFO [mlr3] Predicting with model of learner 'classif.rpart' on task 'iris' ...
## <Experiment> [predicted]:
##  + Task: iris
##  + Learner: classif.rpart
##  + Model: [rpart]
##  + Predictions: [PredictionClassif]
##  - Performance: [missing]
print(e)
## <Experiment> [predicted]:
##  + Task: iris
##  + Learner: classif.rpart
##  + Model: [rpart]
##  + Predictions: [PredictionClassif]
##  - Performance: [missing]

The predictions can be retrieved as a simple data.table.

library(data.table)
head(as.data.table(e$prediction))
##    row_id  truth response
## 1:      2 setosa   setosa
## 2:      5 setosa   setosa
## 3:     11 setosa   setosa
## 4:     16 setosa   setosa
## 5:     19 setosa   setosa
## 6:     35 setosa   setosa

4.5.1 Performance assessment

The last step of the experiment is quantifying the performance of the model by comparing the predicted labels with the true labels using a performance measure. The default measure for the iris classification task is the mean misclassification error, which is used here by default:

task$measures[[1L]]$id
## [1] "classif.ce"
e$score()
## INFO [mlr3] Scoring predictions of learner 'classif.rpart' on task 'iris' ...
## <Experiment> [scored]:
##  + Task: iris
##  + Learner: classif.rpart
##  + Model: [rpart]
##  + Predictions: [PredictionClassif]
##  + Performance: classif.ce=0.06667
print(e)
## <Experiment> [scored]:
##  + Task: iris
##  + Learner: classif.rpart
##  + Model: [rpart]
##  + Predictions: [PredictionClassif]
##  + Performance: classif.ce=0.06667
e$performance["classif.ce"]
## classif.ce 
##    0.06667

The experiment is now “complete” which means we can access all of its methods.

4.5.2 Chaining methods

Instead of calling the methods $train(), $predict() and $score() one after each other, it is also possible to chain these commands:

Experiment$new(task = task, learner = learner)$train(train_set)$predict(test_set)$score()
## INFO [mlr3] Training learner 'classif.rpart' on task 'iris' ...
## INFO [mlr3] Predicting with model of learner 'classif.rpart' on task 'iris' ...
## INFO [mlr3] Scoring predictions of learner 'classif.rpart' on task 'iris' ...
## <Experiment> [scored]:
##  + Task: iris
##  + Learner: classif.rpart
##  + Model: [rpart]
##  + Predictions: [PredictionClassif]
##  + Performance: classif.ce=0.06667