Skip to content
Permalink
Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
95 lines (76 sloc) 3.38 KB
---
title: "Learners"
author: "mlr-org team"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Learners}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r child = 'include/options.Rmd'}
```
## Prerequisites
```{r}
library(mlr3)
```
Objects of class `mlr3::Learner` provide a unified interface to many popular machine learning algorithms in R.
They consist of methods to train and predict on a `mlr3::Task`, and additionally provide meta information about the algorithms.
The package ships with only a rather minimal set of classification and regression learners, more are implemented in the [mlr3learners](https://mlr3learners.mlr-org.com) package.
Furthermore, [mlr3learners](https://mlr3learners.mlr-org.com) has some documentation on creating custom learners.
## Predefined Learners
Analogously to `mlr3::mlr_tasks`, the `mlr3::Dictionary` `mlr3::mlr_learners` can be queried for available learners:
```{r}
mlr_learners
as.data.table(mlr_learners)
```
As listed in the output, each learner comes with the following annotations:
* `feature_types`: what kind of features can be processed.
* `packages`: which packages are required to run `train()` and `predict()`.
* `properties`: additional properties and capabilities. E.g., a learner has the property "missings" if it is able to
handle missing values natively, and "importance" if it is possible to extract feature importance values.
* `predict_types`: what predict types are possible. E.g., a classification learner can predict labels ("response") or probabilities ("prob")
To extract a specific learner, use the corresponding "id":
```{r}
learner = mlr_learners$get("classif.rpart")
learner
```
As the printer shows, all information from the previous table is also accessible via public fields (`id`, `feature_types`, `packages`, `properties`, `predict_types`)
Additionally, `predict_type` returns the currently selected predict type of the learner.
The field `param_set` stores a description of hyperparameter settings:
```{r}
learner$param_set
```
The set of hyperparamter values is stored inside the parameter set in the field `values`.
By assigning a named list to this field, we change the active hyperparameters of the learner:
```{r}
learner$param_set$values = list(cp = 0.01)
learner
```
The field `model` stores the result of the training step.
As we have not yet learned a model, this is `NULL`:
```{r}
learner$model
```
### Train and Predict
Is recommended to train the learner via the `mlr3::Experiment` class, we only train the learner here directly to showcase more of its API.
First, we retrieve the "iris" task from the `mlr3::mlr_tasks` dictionary, and then apply the train method on the complete task.
```{r}
task = mlr_tasks$get("iris")
learner$train(task)
```
The learner returns itself, the fitted model is stored in the field "model":
```{r}
learner$model
```
Next, we generate predictions on the complete iris data set:
```{r}
predictions = learner$predict(task)
```
The returned `mlr3::PredictionClassif` object stores the predicted labels ("response") as well as the true labels ("truth").
By simply counting the number of correct predictions and dividing by the number of observations, we can calculate the accuracy:
```{r}
tab = as.data.table(predictions)
mean(tab$response == tab$truth)
```
Note that this measure is over-optimistic.
As we did not used an independent test set, this is the re-substitution error.
You can’t perform that action at this time.