View source: R/pull_importances.R
| pull_importances | R Documentation |
'pull_importances' is a generic function to extract feature importance scores or coefficients from a parsnip 'model_fit' object and return them as a tibble with a 'feature' and 'importance' column. This is designed to support the 'step_importance' recipe step.
pull_importances(object, scaled = TRUE, ...)
object |
A 'model_fit' object. |
scaled |
A logical indicating whether to rescale the importances between 0 and 1. Default is TRUE. |
... |
A list of other parameters passed to the feature importance method. |
Most of the basic models within the parsnip package that support feature importances are implemented (call 'methods(pull_importances)' to list models that are currently implemented). If need to pull the feature importance scores from a model that is not currently supported in this package, then you can add a class to the pull_importances generic function which returns a two-column tibble:
tibble
library(parsnip)
# pull feature importances from a model_fit object
model <- boost_tree(mode = "classification") %>%
set_engine("xgboost")
model_fit <- model %>% fit(Species ~., iris)
pull_importances(model_fit)
# create a new pull_importances method
pull_importances._ranger <- function(object, scaled = FALSE, ...) {
# create a call to the ranger::importance function avoiding having to use
# ranger as a dependency
call <- rlang::call2(.fn = "importance", .ns = "ranger", x = object$fit)
scores <- rlang::eval_tidy(call)
# create a tibble with 'feature' and 'importance' columns
scores <- tibble::tibble(
feature = names(scores),
importance = as.numeric(scores)
)
# optionally rescale the importance scores
if (isTRUE(scaled))
scores$importance <- rescale(scores$importance)
scores
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.