Extract Node List from RandomForest Model
Source:R/nodelist.randomForest.R
nodelist.randomForest.RdExtracts node-level attributes from every tree in a
randomForest model.
Node IDs match the from/to indices produced by
edgelist.randomForest, so the two outputs can be
passed directly to igraph::graph_from_data_frame() (after
filtering to a single treenum).
Usage
# S3 method for class 'randomForest'
nodelist(input_object, treenum = NULL, ...)Value
A data.frame with one row per node (across all trees) and the following columns:
- name
Integer node ID within the tree (matches edgelist from/to)
- is_leaf
Logical:
TRUEfor terminal nodes- split_var
Numeric index of the split variable (
NAfor leaves)- split_var_name
Name of the split variable (
NAfor leaves)- split_point
Split threshold (
NAfor leaves)- prediction
Predicted value (numeric for regression, integer class index for classification)
- treenum
Integer identifying which tree the node belongs to
- label
Display label:
"<var>\n< <threshold>"for internal nodes, predicted class name (classification) or numeric value (regression) for leaves
Examples
if (requireNamespace("randomForest", quietly = TRUE)) {
set.seed(12)
rf <- randomForest::randomForest(Species ~ ., data = iris, ntree = 3)
nodes <- nodelist(rf)
head(nodes)
# Extract a single tree
nodes1 <- nodelist(rf, treenum = 1)
}