Learn R Programming

mlr3

Package website: release | dev

Efficient, object-oriented programming on the building blocks of machine learning. Successor of mlr.

Resources (for users and developers)

Installation

Install the last release from CRAN:

install.packages("mlr3")

Install the development version from GitHub:

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

If you want to get started with mlr3, we recommend installing the mlr3verse meta-package which installs mlr3 and some of the most important extension packages:

install.packages("mlr3verse")

Example

Constructing Learners and Tasks

library(mlr3)

# create learning task
task_penguins = as_task_classif(species ~ ., data = palmerpenguins::penguins)
task_penguins
## <TaskClassif:palmerpenguins::penguins> (344 x 8)
## * Target: species
## * Properties: multiclass
## * Features (7):
##   - int (3): body_mass_g, flipper_length_mm, year
##   - dbl (2): bill_depth_mm, bill_length_mm
##   - fct (2): island, sex
# load learner and set hyperparameter
learner = lrn("classif.rpart", cp = .01)

Basic train + predict

# train/test split
split = partition(task_penguins, ratio = 0.67)

# train the model
learner$train(task_penguins, split$train_set)

# predict data
prediction = learner$predict(task_penguins, split$test_set)

# calculate performance
prediction$confusion
##            truth
## response    Adelie Chinstrap Gentoo
##   Adelie       146         5      0
##   Chinstrap      6        63      1
##   Gentoo         0         0    123
measure = msr("classif.acc")
prediction$score(measure)
## classif.acc 
##   0.9651163

Resample

# 3-fold cross validation
resampling = rsmp("cv", folds = 3L)

# run experiments
rr = resample(task_penguins, learner, resampling)

# access results
rr$score(measure)[, .(task_id, learner_id, iteration, classif.acc)]
##                     task_id    learner_id iteration classif.acc
## 1: palmerpenguins::penguins classif.rpart         1   0.8956522
## 2: palmerpenguins::penguins classif.rpart         2   0.9478261
## 3: palmerpenguins::penguins classif.rpart         3   0.9649123
rr$aggregate(measure)
## classif.acc 
##   0.9361302

Extension Packages

Consult the wiki for short descriptions and links to the respective repositories.

For beginners, we strongly recommend to install and load the mlr3verse package for a better user experience.

Why a rewrite?

mlr was first released to CRAN in 2013. Its core design and architecture date back even further. The addition of many features has led to a feature creep which makes mlr hard to maintain and hard to extend. We also think that while mlr was nicely extensible in some parts (learners, measures, etc.), other parts were less easy to extend from the outside. Also, many helpful R libraries did not exist at the time mlr was created, and their inclusion would result in non-trivial API changes.

Design principles

  • Only the basic building blocks for machine learning are implemented in this package.
  • Focus on computation here. No visualization or other stuff. That can go in extra packages.
  • Overcome the limitations of R’s S3 classes with the help of R6.
  • Embrace R6 for a clean OO-design, object state-changes and reference semantics. This might be less “traditional R”, but seems to fit mlr nicely.
  • Embrace data.table for fast and convenient data frame computations.
  • Combine data.table and R6, for this we will make heavy use of list columns in data.tables.
  • Defensive programming and type safety. All user input is checked with checkmate. Return types are documented, and mechanisms popular in base R which “simplify” the result unpredictably (e.g., sapply() or drop argument in [.data.frame) are avoided.
  • Be light on dependencies. mlr3 requires the following packages at runtime:
    • parallelly: Helper functions for parallelization. No extra recursive dependencies.
    • future.apply: Resampling and benchmarking is parallelized with the future abstraction interfacing many parallel backends.
    • backports: Ensures backward compatibility with older R releases. Developed by members of the mlr team. No recursive dependencies.
    • checkmate: Fast argument checks. Developed by members of the mlr team. No extra recursive dependencies.
    • mlr3misc: Miscellaneous functions used in multiple mlr3 extension packages. Developed by the mlr team.
    • paradox: Descriptions for parameters and parameter sets. Developed by the mlr team. No extra recursive dependencies.
    • R6: Reference class objects. No recursive dependencies.
    • data.table: Extension of R’s data.frame. No recursive dependencies.
    • digest (via mlr3misc): Hash digests. No recursive dependencies.
    • uuid: Create unique string identifiers. No recursive dependencies.
    • lgr: Logging facility. No extra recursive dependencies.
    • mlr3measures: Performance measures. No extra recursive dependencies.
    • mlbench: A collection of machine learning data sets. No dependencies.
    • palmerpenguins: A classification data set about penguins, used on examples and provided as a toy task. No dependencies.
  • Reflections: Objects are queryable for properties and capabilities, allowing you to program on them.
  • Additional functionality that comes with extra dependencies:
    • To capture output, warnings and exceptions, evaluate and callr can be used.

Contributing to mlr3

This R package is licensed under the LGPL-3. If you encounter problems using this software (lack of documentation, misleading or wrong documentation, unexpected behavior, bugs, …) or just want to suggest features, please open an issue in the issue tracker. Pull requests are welcome and will be included at the discretion of the maintainers.

Please consult the wiki for a style guide, a roxygen guide and a pull request guide.

Citing mlr3

If you use mlr3, please cite our JOSS article:

@Article{mlr3,
  title = {{mlr3}: A modern object-oriented machine learning framework in {R}},
  author = {Michel Lang and Martin Binder and Jakob Richter and Patrick Schratz and Florian Pfisterer and Stefan Coors and Quay Au and Giuseppe Casalicchio and Lars Kotthoff and Bernd Bischl},
  journal = {Journal of Open Source Software},
  year = {2019},
  month = {dec},
  doi = {10.21105/joss.01903},
  url = {https://joss.theoj.org/papers/10.21105/joss.01903},
}

Copy Link

Version

Install

install.packages('mlr3')

Monthly Downloads

10,758

Version

0.23.0

License

LGPL-3

Issues

Pull Requests

Stars

Forks

Maintainer

Marc Becker

Last Published

March 12th, 2025

Functions in mlr3 (0.23.0)

LearnerClassif

Classification Learner
BenchmarkResult

Container for Benchmarking Results
LearnerRegr

Regression Learner
ContextResample

Resample Context
DataBackend

DataBackend
HotstartStack

Stack for Hot Start Learners
DataBackendMatrix

DataBackend for Matrix
Learner

Learner Class
CallbackResample

Resample Callback
DataBackendDataTable

DataBackend for data.table
Measure

Measure Class
PredictionData

Convert to PredictionData
MeasureClassif

Classification Measure
MeasureRegr

Regression Measure
Resampling

Resampling Class
PredictionClassif

Prediction Object for Classification
ResampleResult

Container for Results of resample()
MeasureSimilarity

Similarity Measure
PredictionRegr

Prediction Object for Regression
Prediction

Abstract Prediction Object
TaskGenerator

TaskGenerator Class
as_benchmark_result

Convert to BenchmarkResult
ResultData

ResultData
as_data_backend.Matrix

Create a Data Backend
Task

Task Class
TaskClassif

Classification Task
as_learner

Convert to a Learner
TaskRegr

Regression Task
TaskSupervised

Supervised Task
TaskUnsupervised

Unsupervised Task
as_result_data

Convert to ResultData
as_prediction

Convert to a Prediction
as_prediction_regr

Convert to a Regression Prediction
as_task_classif

Convert to a Classification Task
as_task

Convert to a Task
as_prediction_classif

Convert to a Classification Prediction
as_resample_result

Convert to ResampleResult
as_measure

Convert to a Measure
as_prediction_data

PredictionData
as_resampling

Convert to a Resampling
callback_resample

Create Evaluation Callback
as_task_regr

Convert to a Regression Task
col_info

Column Information for Backend
assert_resample_callback

Assertions for Callbacks
assert_empty_ellipsis

Assert Empty Ellipsis
as_task_unsupervised

Convert to an Unsupervised Task
auto_convert

Column Auto-Converter
benchmark

Benchmark Multiple Learners on Multiple Tasks
mlr_assertions

Assertion for mlr3 Objects
mlr3.model_extractor

Model Extractor Callback
default_fallback

Create a Fallback Learner
benchmark_grid

Generate a Benchmark Grid Design
convert_task

Convert a Task from One Type to Another
california_housing

Median House Value in California
marshaling

(Un)marshal a Learner
install_pkgs

Install (Missing) Packages
mlr3-package

mlr3: Machine Learning in R - Next Generation
mlr3.holdout_task

Callback Holdout Task
default_measures

Get the Default Measure
deprecated_binding

Create an Active Binding that Generates a Deprecation Warning
mlr_learners_classif.featureless

Featureless Classification Learner
mlr_measures_bic

Bayesian Information Criterion Measure
mlr_measures

Dictionary of Performance Measures
mlr_learners_regr.featureless

Featureless Regression Learner
mlr_learners_regr.debug

Regression Learner for Debugging
mlr_learners_classif.debug

Classification Learner for Debugging
mlr_learners_regr.rpart

Regression Tree Learner
mlr_measures_aic

Akaike Information Criterion Measure
mlr_learners

Dictionary of Learners
mlr_learners_classif.rpart

Classification Tree Learner
mlr_measures_classif.fbeta

F-beta Score
mlr_measures_classif.ce

Classification Error
mlr_measures_classif.acc

Classification Accuracy
mlr_measures_classif.dor

Diagnostic Odds Ratio
mlr_measures_classif.bbrier

Binary Brier Score
mlr_measures_classif.fn

False Negatives
mlr_measures_classif.fdr

False Discovery Rate
mlr_measures_classif.bacc

Balanced Accuracy
mlr_measures_classif.auc

Area Under the ROC Curve
mlr_measures_classif.costs

Cost-sensitive Classification Measure
mlr_measures_classif.fnr

False Negative Rate
mlr_measures_classif.mauc_aunp

Multiclass AUC Scores
mlr_measures_classif.mauc_mu

Multiclass AUC Scores
mlr_measures_classif.logloss

Log Loss
mlr_measures_classif.mauc_aunu

Multiclass AUC Scores
mlr_measures_classif.mauc_au1p

Multiclass AUC Scores
mlr_measures_classif.fomr

False Omission Rate
mlr_measures_classif.fpr

False Positive Rate
mlr_measures_classif.fp

False Positives
mlr_measures_classif.mauc_au1u

Multiclass AUC Scores
mlr_measures_classif.recall

True Positive Rate
mlr_measures_classif.precision

Positive Predictive Value
mlr_measures_classif.npv

Negative Predictive Value
mlr_measures_classif.prauc

Area Under the Precision-Recall Curve
mlr_measures_classif.specificity

True Negative Rate
mlr_measures_classif.sensitivity

True Positive Rate
mlr_measures_classif.ppv

Positive Predictive Value
mlr_measures_classif.tn

True Negatives
mlr_measures_classif.mbrier

Multiclass Brier Score
mlr_measures_classif.mcc

Matthews Correlation Coefficient
mlr_measures_elapsed_time

Elapsed Time Measure
mlr_measures_regr.ktau

Kendall's tau
mlr_measures_classif.tp

True Positives
mlr_measures_regr.bias

Bias
mlr_measures_regr.mae

Mean Absolute Error
mlr_measures_internal_valid_score

Measure Internal Validation Score
mlr_measures_classif.tnr

True Negative Rate
mlr_measures_debug_classif

Debug Measure for Classification
mlr_measures_classif.tpr

True Positive Rate
mlr_measures_oob_error

Out-of-bag Error Measure
mlr_measures_regr.rmse

Root Mean Squared Error
mlr_measures_regr.medse

Median Squared Error
mlr_measures_regr.pbias

Percent Bias
mlr_measures_regr.medae

Median Absolute Error
mlr_measures_regr.mape

Mean Absolute Percent Error
mlr_measures_regr.maxae

Max Absolute Error
mlr_measures_regr.rae

Relative Absolute Error
mlr_measures_regr.mse

Mean Squared Error
mlr_measures_regr.msle

Mean Squared Log Error
mlr_measures_regr.srho

Spearman's rho
mlr_measures_regr.rmsle

Root Mean Squared Log Error
mlr_measures_selected_features

Selected Features Measure
mlr_measures_regr.sae

Sum of Absolute Errors
mlr_measures_regr.sse

Sum of Squared Errors
mlr_measures_regr.pinball

Average Pinball Loss
mlr_measures_regr.smape

Symmetric Mean Absolute Percent Error
mlr_measures_regr.rrse

Root Relative Squared Error
mlr_measures_regr.rse

Relative Squared Error
mlr_measures_sim.jaccard

Jaccard Similarity Index
mlr_measures_regr.rsq

R-Squared
mlr_resamplings_custom

Custom Resampling
mlr_resamplings_holdout

Holdout Resampling
mlr_resamplings_cv

Cross-Validation Resampling
mlr_resamplings_custom_cv

Custom Cross-Validation
mlr_resamplings_bootstrap

Bootstrap Resampling
mlr_resamplings

Dictionary of Resampling Strategies
mlr_reflections

Reflections for mlr3
mlr_measures_sim.phi

Phi Coefficient Similarity
mlr_resamplings_loo

Leave-One-Out Cross-Validation
mlr_resamplings_insample

Insample Resampling
mlr_task_generators_2dnormals

2D Normals Classification Task Generator
mlr_task_generators_circle

Circle Classification Task Generator
mlr_resamplings_repeated_cv

Repeated Cross-Validation Resampling
mlr_task_generators

Dictionary of Task Generators
mlr_task_generators_cassini

Cassini Classification Task Generator
mlr_sugar

Syntactic Sugar for Object Construction
mlr_task_generators_moons

Moons Classification Task Generator
mlr_resamplings_subsampling

Subsampling Resampling
mlr_task_generators_friedman1

Friedman1 Regression Task Generator
mlr_task_generators_simplex

Simplex Classification Task Generator
mlr_task_generators_xor

XOR Classification Task Generator
mlr_tasks_mtcars

Motor Trend Regression Task
mlr_tasks_iris

Iris Classification Task
mlr_tasks_breast_cancer

Wisconsin Breast Cancer Classification Task
mlr_tasks_german_credit

German Credit Classification Task
mlr_tasks_penguins

Palmer Penguins Data Set
mlr_task_generators_smiley

Smiley Classification Task Generator
mlr_tasks_pima

Pima Indian Diabetes Classification Task
mlr_tasks

Dictionary of Tasks
mlr_task_generators_spirals

Spiral Classification Task Generator
resample

Resample a Learner on a Task
reexports

Objects exported from other packages
predict.Learner

Predict Method for Learners
mlr_tasks_sonar

Sonar Classification Task
mlr_tasks_wine

Wine Classification Task
set_threads

Set the Number of Threads
mlr_tasks_spam

Spam Classification Task
warn_deprecated

Give a Warning about a Deprecated Function, Argument, or Active Binding
partition

Manually Partition into Training, Test and Validation Set
mlr_test_helpers

Documentation of mlr3 test helpers
mlr_tasks_zoo

Zoo Classification Task
task_check_col_roles

Check Column Roles