Learn R Programming

treeshap (version 0.3.1)

plot_feature_importance: SHAP value based Feature Importance plot

Description

This function plots feature importance calculated as means of absolute values of SHAP values of variables (average impact on model output magnitude).

Usage

plot_feature_importance(
  treeshap,
  desc_sorting = TRUE,
  max_vars = ncol(shaps),
  title = "Feature Importance",
  subtitle = NULL
)

Value

a ggplot2 object

Arguments

treeshap

A treeshap object produced with the treeshap function. treeshap.object.

desc_sorting

logical. Should the bars be sorted descending? By default TRUE.

max_vars

maximum number of variables that shall be presented. By default all are presented.

title

the plot's title, by default 'Feature Importance'.

subtitle

the plot's subtitle. By default no subtitle.

See Also

treeshap for calculation of SHAP values

plot_contribution, plot_feature_dependence, plot_interaction

Examples

Run this code
# \donttest{
library(xgboost)
data <- fifa20$data[colnames(fifa20$data) != 'work_rate']
target <- fifa20$target
param <- list(objective = "reg:squarederror", max_depth = 3)
xgb_model <- xgboost::xgboost(as.matrix(data), params = param, label = target,
                              nrounds = 20, verbose = FALSE)
unified_model <- xgboost.unify(xgb_model, as.matrix(data))
shaps <- treeshap(unified_model, as.matrix(head(data, 3)))
plot_feature_importance(shaps, max_vars = 4)
# }

Run the code above in your browser using DataLab