Learners

To keep the dependencies on other packages reasonable, the base package mlr3 only ships with with regression and classification trees from the rpart package and some learners for debugging. A subjective selection of implementations for essential ML algorithms can be found in mlr3learners package. Survival learners are provided by mlr3proba, cluster learners via mlr3cluster. Additional learners, including some learners which are not yet to be considered stable or which are not available on CRAN, are connected via the mlr3extralearners package. For neural networks, see the mlr3torch extension.

Example Usage

Fit a classification tree on the Wisconsin Breast Cancer Data Set and predict on left-out observations.

library("mlr3verse")

# retrieve the task
task = tsk("breast_cancer")

# split into two partitions
split = partition(task)

# retrieve a learner
learner = lrn("classif.rpart", keep_model = TRUE, predict_type = "prob")

# fit decision tree
learner$train(task, split$train)

# access learned model
learner$model
n= 458 

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

 1) root 458 167 benign (0.364628821 0.635371179)  
   2) cell_size=3,4,5,6,7,8,9,10 181  26 malignant (0.856353591 0.143646409)  
     4) cell_shape=3,4,5,6,7,8,9,10 166  14 malignant (0.915662651 0.084337349)  
       8) cell_size=5,6,7,8,9,10 117   1 malignant (0.991452991 0.008547009) *
       9) cell_size=1,2,3,4 49  13 malignant (0.734693878 0.265306122)  
        18) bare_nuclei=3,4,5,6,7,8,9,10 38   5 malignant (0.868421053 0.131578947) *
        19) bare_nuclei=1,2 11   3 benign (0.272727273 0.727272727) *
     5) cell_shape=1,2 15   3 benign (0.200000000 0.800000000) *
   3) cell_size=1,2 277  12 benign (0.043321300 0.956678700)  
     6) bare_nuclei=6,7,8,9,10 7   0 malignant (1.000000000 0.000000000) *
     7) bare_nuclei=1,2,3,4,5 270   5 benign (0.018518519 0.981481481) *
# predict on data frame with new data
predictions = learner$predict_newdata(task$data(split$test))

# predict on subset of the task
predictions = learner$predict(task, split$test)

# inspect predictions
predictions

── <PredictionClassif> for 225 observations: ───────────────────────────────────
 row_ids     truth  response prob.malignant prob.benign
       3    benign    benign     0.01851852 0.981481481
       4    benign malignant     0.99145299 0.008547009
       5    benign    benign     0.01851852 0.981481481
     ---       ---       ---            ---         ---
     680    benign    benign     0.01851852 0.981481481
     681 malignant malignant     0.99145299 0.008547009
     683 malignant malignant     0.99145299 0.008547009
predictions$score(msr("classif.auc"))
classif.auc 
  0.9825708 
autoplot(predictions, type = "roc")