Extract the active features from a tree
Source:R/C5.0.R, R/ObliqueForest.R, R/bart.R, and 10 more
active_predictors.RdIf a tree does not use a predictor in the training set in any of its splits it is functionally independent of the prediction function. This generic returns a data frame containing character vector of predictor names that were used in at least one split.
Usage
# S3 method for class 'C5.0'
active_predictors(x, tree = 1L, ...)
# S3 method for class 'ObliqueForest'
active_predictors(x, tree = 1L, ...)
# S3 method for class 'bart'
active_predictors(x, tree = 1L, ...)
# S3 method for class 'cforest'
active_predictors(x, tree = 1L, ...)
# S3 method for class 'cubist'
active_predictors(x, ...)
active_predictors(x, ...)
# S3 method for class 'grf'
active_predictors(x, tree = 1L, ...)
# S3 method for class 'lgb.Booster'
active_predictors(x, tree = 1L, ...)
# S3 method for class 'party'
active_predictors(x, ...)
# S3 method for class 'randomForest'
active_predictors(x, tree = 1L, ...)
# S3 method for class 'ranger'
active_predictors(x, tree = 1L, ...)
# S3 method for class 'rpart'
active_predictors(x, ...)
# S3 method for class 'xgb.Booster'
active_predictors(x, tree = 1L, ...)Examples
if (rlang::is_installed(c("rpart", "palmerpenguins"))) {
data(penguins, package = "palmerpenguins")
penguins <- na.omit(penguins)
# Fit a tree
tree <- rpart::rpart(species ~ ., data = penguins)
tree
# Extract active predictors
active_predictors(tree)
# Only primary splits are included - competing and surrogate splits
# are excluded since they don't affect predictions
}
#> # A tibble: 1 × 1
#> active_predictors
#> <list>
#> 1 <chr [3]>
# C5.0 single tree
if (rlang::is_installed(c("C50", "palmerpenguins"))) {
data(penguins, package = "palmerpenguins")
penguins <- na.omit(penguins)
# Tree-based model
c5_tree <- C50::C5.0(species ~ ., data = penguins)
active_predictors(c5_tree)
# Boosted model - extract from multiple trials
c5_boost <- C50::C5.0(species ~ ., data = penguins, trials = 5)
active_predictors(c5_boost, tree = 1:3)
# Rule-based model
c5_rules <- C50::C5.0(species ~ ., data = penguins, rules = TRUE)
active_predictors(c5_rules)
}
#> Registered S3 method overwritten by 'C50':
#> method from
#> as.party.C5.0 lorax
#> # A tibble: 1 × 1
#> active_predictors
#> <list>
#> 1 <chr [4]>