mlr3 book

Authors

Marc Becker

Martin Binder

Bernd Bischl

Natalie Foss

Lars Kotthoff

Michel Lang

Florian Pfisterer

Nicholas G. Reich

Jakob Richter

Patrick Schratz

Raphael Sonabend

Damir Pulatovoo

Quickstart

Note

A collection of use cases and examples can be found in the mlr3gallery on our main website https://mlr-org.com.

As a 30-second introductory example, we will train a decision tree model on the first 120 rows of iris data set and make predictions on the final 30, measuring the accuracy of the trained model.

library("mlr3")
task = tsk("iris")
learner = lrn("classif.rpart")

# train a model of this learner for a subset of the task
learner$train(task, row_ids = 1:120)
# this is what the decision tree looks like
learner$model
n= 120 

node), split, n, loss, yval, (yprob)
      * denotes terminal node

1) root 120 70 setosa (0.41666667 0.41666667 0.16666667)  
  2) Petal.Length< 2.45 50  0 setosa (1.00000000 0.00000000 0.00000000) *
  3) Petal.Length>=2.45 70 20 versicolor (0.00000000 0.71428571 0.28571429)  
    6) Petal.Length< 4.95 49  1 versicolor (0.00000000 0.97959184 0.02040816) *
    7) Petal.Length>=4.95 21  2 virginica (0.00000000 0.09523810 0.90476190) *
predictions = learner$predict(task, row_ids = 121:150)
predictions
<PredictionClassif> for 30 observations:
    row_ids     truth   response
        121 virginica  virginica
        122 virginica versicolor
        123 virginica  virginica
---                             
        148 virginica  virginica
        149 virginica  virginica
        150 virginica  virginica
# accuracy of our model on the test set of the final 30 rows
predictions$score(msr("classif.acc"))
classif.acc 
  0.8333333 
Tip

We highly recommend to take a look at our cheatsheets while diving deeper into mlr3.