Classification Random Forest Learner
mlr_learners_classif.randomForest.Rd
Random forest for classification.
Calls randomForest::randomForest()
from randomForest.
Meta Information
Task type: “classif”
Predict Types: “response”, “prob”
Feature Types: “logical”, “integer”, “numeric”, “factor”, “ordered”
Required Packages: mlr3, mlr3extralearners, randomForest
Parameters
Id | Type | Default | Levels | Range |
ntree | integer | 500 | \([1, \infty)\) | |
mtry | integer | - | \([1, \infty)\) | |
replace | logical | TRUE | TRUE, FALSE | - |
classwt | untyped | NULL | - | |
cutoff | untyped | - | - | |
strata | untyped | - | - | |
sampsize | untyped | - | - | |
nodesize | integer | 1 | \([1, \infty)\) | |
maxnodes | integer | - | \([1, \infty)\) | |
importance | character | FALSE | accuracy, gini, none | - |
localImp | logical | FALSE | TRUE, FALSE | - |
proximity | logical | FALSE | TRUE, FALSE | - |
oob.prox | logical | - | TRUE, FALSE | - |
norm.votes | logical | TRUE | TRUE, FALSE | - |
do.trace | logical | FALSE | TRUE, FALSE | - |
keep.forest | logical | TRUE | TRUE, FALSE | - |
keep.inbag | logical | FALSE | TRUE, FALSE | - |
predict.all | logical | FALSE | TRUE, FALSE | - |
nodes | logical | FALSE | TRUE, FALSE | - |
References
Breiman, Leo (2001). “Random Forests.” Machine Learning, 45(1), 5–32. ISSN 1573-0565, doi:10.1023/A:1010933404324 .
See also
as.data.table(mlr_learners)
for a table of available Learners in the running session (depending on the loaded packages).Chapter in the mlr3book: https://mlr3book.mlr-org.com/basics.html#learners
mlr3learners for a selection of recommended learners.
mlr3cluster for unsupervised clustering learners.
mlr3pipelines to combine learners with pre- and postprocessing steps.
mlr3tuning for tuning of hyperparameters, mlr3tuningspaces for established default tuning spaces.
Super classes
mlr3::Learner
-> mlr3::LearnerClassif
-> LearnerClassifRandomForest
Methods
Method importance()
The importance scores are extracted from the slot importance
.
Parameter 'importance' must be set to either "accuracy"
or "gini"
.
Returns
Named numeric()
.
Examples
# Define the Learner
learner = mlr3::lrn("classif.randomForest", importance = "accuracy")
print(learner)
#>
#> ── <LearnerClassifRandomForest> (classif.randomForest): Random Forest ──────────
#> • Model: -
#> • Parameters: importance=accuracy
#> • Packages: mlr3, mlr3extralearners, and randomForest
#> • Predict Types: [response] and prob
#> • Feature Types: logical, integer, numeric, factor, and ordered
#> • Encapsulation: none (fallback: -)
#> • Properties: importance, multiclass, oob_error, twoclass, and weights
#> • Other settings: use_weights = 'use'
# Define a Task
task = mlr3::tsk("sonar")
# Create train and test set
ids = mlr3::partition(task)
# Train the learner on the training ids
learner$train(task, row_ids = ids$train)
print(learner$model)
#>
#> Call:
#> randomForest(formula = formula, data = data, classwt = classwt, cutoff = cutoff, importance = TRUE)
#> Type of random forest: classification
#> Number of trees: 500
#> No. of variables tried at each split: 7
#>
#> OOB estimate of error rate: 20.14%
#> Confusion matrix:
#> M R class.error
#> M 62 11 0.1506849
#> R 17 49 0.2575758
print(learner$importance())
#> V12 V11 V48 V49 V10
#> 1.954012e-02 1.823393e-02 1.810986e-02 1.478098e-02 1.195677e-02
#> V9 V36 V47 V13 V51
#> 1.140873e-02 9.079952e-03 8.809801e-03 7.725510e-03 5.737290e-03
#> V21 V45 V37 V22 V28
#> 5.632227e-03 5.467520e-03 5.311507e-03 4.869905e-03 4.794957e-03
#> V44 V35 V23 V5 V8
#> 4.652545e-03 3.404877e-03 3.227543e-03 3.074009e-03 2.877529e-03
#> V18 V46 V20 V6 V27
#> 2.836130e-03 2.413177e-03 2.402010e-03 2.329209e-03 2.325164e-03
#> V43 V52 V15 V2 V16
#> 2.290321e-03 2.196867e-03 2.184775e-03 2.043311e-03 1.991092e-03
#> V1 V14 V31 V7 V42
#> 1.827775e-03 1.602281e-03 1.586132e-03 1.521835e-03 1.510376e-03
#> V17 V3 V26 V56 V60
#> 1.482003e-03 1.471900e-03 1.436512e-03 1.410681e-03 1.376443e-03
#> V38 V59 V19 V33 V41
#> 1.321945e-03 1.278806e-03 1.270419e-03 1.055345e-03 1.008573e-03
#> V58 V25 V4 V50 V53
#> 9.385504e-04 9.325867e-04 9.145518e-04 8.907383e-04 7.172904e-04
#> V32 V30 V39 V24 V40
#> 5.990286e-04 5.888455e-04 5.590209e-04 5.561615e-04 4.824768e-04
#> V55 V57 V54 V34 V29
#> 4.032725e-04 1.810352e-04 3.046178e-05 -3.321111e-04 -4.656126e-04
# Make predictions for the test rows
predictions = learner$predict(task, row_ids = ids$test)
# Score the predictions
predictions$score()
#> classif.ce
#> 0.1449275