pull_importances: Pull feature importances from a parsnip fitted model

View source: R/pull_importances.R

pull_importancesR Documentation

Pull feature importances from a parsnip fitted model

Description

'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.

Usage

pull_importances(object, scaled = TRUE, ...)

Arguments

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.

Details

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:

Value

tibble

Examples

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
}

stevenpawley/recipesSelection documentation built on Aug. 7, 2022, 1:17 a.m.