`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, ...)

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.

Value

tibble

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:

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)
#> # A tibble: 4 × 2
#>   feature      importance
#>   <chr>             <dbl>
#> 1 Petal.Length    100    
#> 2 Petal.Width      46.0  
#> 3 Sepal.Width       0.809
#> 4 Sepal.Length      0    

# 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
}