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, randomForest
#> * Predict Types: [response], prob
#> * Feature Types: logical, integer, numeric, factor, ordered
#> * Properties: importance, multiclass, oob_error, twoclass, weights
# 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.86%
#> Confusion matrix:
#> M R class.error
#> M 68 9 0.1168831
#> R 20 42 0.3225806
print(learner$importance())
#> V9 V11 V12 V10 V49
#> 2.917475e-02 2.236524e-02 1.631477e-02 1.283972e-02 1.239122e-02
#> V48 V47 V36 V16 V4
#> 8.972802e-03 7.227179e-03 6.389857e-03 5.972599e-03 5.271394e-03
#> V31 V17 V45 V13 V51
#> 4.899074e-03 4.782425e-03 4.330378e-03 4.149344e-03 4.036351e-03
#> V20 V8 V37 V21 V46
#> 3.605309e-03 3.494013e-03 3.295737e-03 3.075456e-03 3.062617e-03
#> V28 V30 V59 V6 V44
#> 2.808179e-03 2.805633e-03 2.742479e-03 2.740594e-03 2.582705e-03
#> V50 V39 V5 V52 V18
#> 2.415963e-03 2.401929e-03 2.359473e-03 2.254632e-03 2.216812e-03
#> V14 V26 V35 V32 V15
#> 2.149727e-03 2.144001e-03 2.123356e-03 1.882355e-03 1.788891e-03
#> V58 V23 V3 V29 V25
#> 1.694806e-03 1.373189e-03 1.146878e-03 1.105613e-03 1.082671e-03
#> V22 V43 V1 V53 V42
#> 1.036162e-03 9.982038e-04 9.738504e-04 8.843141e-04 8.452774e-04
#> V56 V27 V2 V19 V38
#> 7.030434e-04 6.513843e-04 5.463449e-04 5.450852e-04 4.731585e-04
#> V40 V54 V34 V24 V33
#> 4.385642e-04 4.266878e-04 4.173558e-04 2.625243e-04 6.028029e-05
#> V55 V57 V41 V7 V60
#> -1.564957e-04 -3.910790e-04 -4.230614e-04 -6.654719e-04 -6.951574e-04
# Make predictions for the test rows
predictions = learner$predict(task, row_ids = ids$test)
# Score the predictions
predictions$score()
#> classif.ce
#> 0.173913