Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save renanxcortes/7715859bfb86aa46fde10cb3cf22534e to your computer and use it in GitHub Desktop.
Save renanxcortes/7715859bfb86aa46fde10cb3cf22534e to your computer and use it in GitHub Desktop.
snippet2_blogpost
# SHAP values: http://docs.h2o.ai/h2o/latest-stable/h2o-r/docs/reference/predict_contributions.H2OModel.html
SHAP_values <- predict_contributions.H2OModel(aml_leader, df_frame_split[[2]])
# Wrangling inspired here: https://bradleyboehmke.github.io/HOML/iml.html
shap_df <- SHAP_values %>%
as.data.frame() %>%
select(-BiasTerm) %>%
gather(feature, shap_value) %>%
group_by(feature) %>%
mutate(shap_importance = mean(abs(shap_value)),
shap_force = mean(shap_value)) %>%
ungroup()
# SHAP contribution plot
p1 <- ggplot(shap_df, aes(x = shap_value, y = reorder(feature, shap_importance))) +
ggbeeswarm::geom_quasirandom(groupOnX = FALSE, varwidth = TRUE, size = 0.9, alpha = 0.5, width = 0.15) +
xlab("SHAP value") +
ylab(NULL) +
theme_minimal(base_size = 15)
# SHAP importance plot
p2 <- shap_df %>%
select(feature, shap_importance) %>%
distinct() %>%
ggplot(aes(x = reorder(feature, shap_importance),
y = shap_importance)) +
geom_col(fill = 'black') +
coord_flip() +
xlab(NULL) +
ylab("mean(|SHAP value|)") +
theme_minimal(base_size = 15)
# Combine plots
gridExtra::grid.arrange(p1, p2, nrow = 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment