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 67 8 0.1066667
#> R 21 43 0.3281250
print(learner$importance())
#> V11 V12 V9 V10 V36
#> 2.717515e-02 2.395888e-02 1.388048e-02 8.406069e-03 8.273901e-03
#> V47 V13 V5 V49 V51
#> 8.096311e-03 8.048201e-03 7.729568e-03 7.654984e-03 7.590147e-03
#> V48 V52 V37 V16 V27
#> 6.312205e-03 5.972657e-03 5.653645e-03 4.712117e-03 4.364364e-03
#> V45 V15 V4 V46 V21
#> 3.798688e-03 3.748484e-03 3.618038e-03 3.462182e-03 3.343985e-03
#> V39 V44 V14 V50 V54
#> 2.863128e-03 2.737147e-03 2.512185e-03 2.502525e-03 2.379943e-03
#> V17 V26 V6 V58 V22
#> 2.332428e-03 2.131952e-03 2.074112e-03 2.049535e-03 2.022364e-03
#> V2 V23 V28 V25 V20
#> 2.007287e-03 2.000708e-03 1.969641e-03 1.955514e-03 1.923627e-03
#> V60 V42 V19 V43 V1
#> 1.863758e-03 1.827722e-03 1.676401e-03 1.651613e-03 1.586040e-03
#> V35 V32 V53 V38 V24
#> 1.478282e-03 1.450080e-03 1.331703e-03 1.286426e-03 1.230393e-03
#> V29 V18 V59 V40 V55
#> 1.115236e-03 1.114981e-03 1.073292e-03 1.004972e-03 9.535023e-04
#> V33 V56 V31 V8 V34
#> 9.453580e-04 8.407759e-04 6.083788e-04 5.536828e-04 4.306289e-04
#> V57 V30 V3 V7 V41
#> 3.916661e-04 3.283200e-04 1.758428e-04 -8.456304e-05 -4.921452e-04
# Make predictions for the test rows
predictions = learner$predict(task, row_ids = ids$test)
# Score the predictions
predictions$score()
#> classif.ce
#> 0.1594203