# 1 Quickstart

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)
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.41667 0.41667 0.16667)
##   2) Petal.Length< 2.45 50  0 setosa (1.00000 0.00000 0.00000) *
##   3) Petal.Length>=2.45 70 20 versicolor (0.00000 0.71429 0.28571)
##     6) Petal.Length< 4.95 49  1 versicolor (0.00000 0.97959 0.02041) *
##     7) Petal.Length>=4.95 21  2 virginica (0.00000 0.09524 0.90476) *

predictions = learner$predict(task, row_ids = 121:150) predictions ## <PredictionClassif> for 30 observations: ## row_id 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("classif.acc")
## Warning: 'Automatic object creation from strings in mlr3' is deprecated.
## Use 'msr' instead.
## See help("Deprecated")
## classif.acc
##      0.8333

The code examples in this book use a few additional packages that are not installed by default if you install mlr3. To run all examples in the book, install the mlr3book package using the remotes package:

remotes::install_github("mlr-org/mlr3book", dependencies = TRUE)

In most cases you do not want to run all code blocks of the book but just some parts of it. In these cases it is easiest to install the mlr3verse pkg (a wrapper package for the core packages of the mlr3 package framework) and install missing packages for specific sections as needed.

remotes::install_github("mlr-org/mlr3verse")
library(mlr3)

Sometimes certain packages of the mlr3verse are ahead of the code examples in the book. If you want to install the state of packages which correspond to the latest published version of the book, use

## [1] remotes::install_github(c('mlr-org/mlr3@d71ac72', 'mlr-org/mlr3db@04d84e2', 'mlr-org/mlr3filters@b288dac', 'mlr-org/mlr3learners@6dc9ef6', 'mlr-org/mlr3misc@dc460e6', 'mlr-org/mlr3ordinal@58ad848', 'mlr-org/mlr3pipelines@f78ea55', 'mlr-org/mlr3spatiotemporal@1d3ba2f', 'mlr-org/mlr3survival@03388f3', 'mlr-org/mlr3tuning@088b591', 'mlr-org/mlr3viz@a68cf1f', 'mlr-org/paradox@4bad616', 'R 3.6.0'), force = TRUE)