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: 21.58%
#> Confusion matrix:
#> M R class.error
#> M 71 8 0.1012658
#> R 22 38 0.3666667
print(learner$importance())
#> V11 V12 V49 V10 V48 V47
#> 3.141615e-02 2.059075e-02 1.553557e-02 1.373584e-02 1.021993e-02 7.409149e-03
#> V9 V37 V45 V52 V21 V13
#> 6.851922e-03 6.819531e-03 6.358177e-03 5.911165e-03 5.235409e-03 5.172277e-03
#> V44 V23 V51 V36 V8 V34
#> 4.976401e-03 4.830731e-03 4.560495e-03 4.264481e-03 4.218096e-03 4.168760e-03
#> V19 V20 V5 V27 V43 V18
#> 3.507162e-03 3.390688e-03 3.262520e-03 3.111309e-03 2.953992e-03 2.952221e-03
#> V17 V4 V28 V16 V14 V33
#> 2.942026e-03 2.660873e-03 2.565565e-03 2.471871e-03 2.105247e-03 2.066788e-03
#> V6 V22 V31 V46 V32 V38
#> 1.938467e-03 1.898716e-03 1.840324e-03 1.708950e-03 1.585245e-03 1.506568e-03
#> V7 V40 V55 V59 V1 V60
#> 1.490747e-03 1.465199e-03 1.389956e-03 1.343599e-03 1.343137e-03 1.192643e-03
#> V54 V25 V15 V26 V30 V42
#> 1.184769e-03 1.092707e-03 1.056038e-03 1.050757e-03 1.041535e-03 1.040271e-03
#> V35 V56 V2 V3 V58 V39
#> 9.223719e-04 8.995145e-04 8.516036e-04 7.193312e-04 6.616488e-04 6.206022e-04
#> V57 V29 V53 V41 V50 V24
#> 5.575594e-04 3.828462e-04 1.965573e-04 1.708466e-04 1.636299e-04 1.929717e-05
# Make predictions for the test rows
predictions = learner$predict(task, row_ids = ids$test)
# Score the predictions
predictions$score()
#> classif.ce
#> 0.2173913