hfshr / distill_blog

Blog and things
https://hfshr.xyz
9 stars 2 forks source link

Data, Code & Coffee: Opening the black box: Exploring xgboost models with {fastshap} in R #4

Open utterances-bot opened 3 years ago

utterances-bot commented 3 years ago

Data, Code & Coffee: Opening the black box: Exploring xgboost models with {fastshap} in R

https://hfshr.xyz/posts/2020-06-07-variable-inportance-with-fastshap/

ornscar commented 3 years ago

Have you ever used the {fastshap} package (and the force_plot function) in the Shiny app? I'm having problems with it in the web environment.

hfshr commented 3 years ago

Hey @ornscar - I hadn't tried this before, but managed to get it sort of working using htmlOutput() and renderText() (although it doesn't seem to work 100% of the time and I'm not sure why...) Try the code below and see if it works for you?

library(shiny)
library(fastshap)
# requires python shap 0.38.1
# reticulate::conda_install(packages = c("shap==0.38.1"))

shinyApp(
  fluidPage(
    h3("Basic shap"),
    htmlOutput("basic_shap"),
    br(),
    h3("Interactive shap"),
    htmlOutput("multi_shap")
  ),
  server = function(input, output, session) {
    data(mtcars)

    # Fit a projection pursuit regression model
    mtcars.ppr <- ppr(mpg ~ ., data = mtcars, nterms = 1)

    set.seed(101) # for reproducibility
    shap <- explain(mtcars.ppr,
      X = subset(mtcars, select = -mpg),
      nsim = 10,
      pred_wrapper = predict,
      adjust = TRUE)

    preds <- predict(mtcars.ppr, newdata = mtcars)

    x <- subset(mtcars, select = -mpg)

    output$basic_shap <- renderText({
      force_plot(
        object = shap[1L, ],
        baseline = mean(preds),
        feature_values = x[1L, ],
        display = "html",
        link = "logit"
      )
    })

    output$multi_shap <- renderText({
      HTML(
        force_plot(
          object = shap[c(1:3), ],
          baseline = mean(preds),
          feature_values = x[c(1:3), ],
          display = "html",
          link = "logit"
        )
      )
    })
  }
)
ornscar commented 3 years ago

Thanks, @hfshr! But even his code didn't work for me (due to the following error: 'Error in py_call_impl: TypeError: save_html() got an unexpected keyword argument 'plot_html''). I was using

reticulate::virtualenv_create(envname = "python_environment", python = "python3") reticulate::virtualenv_install("python_environment", packages = "shap") reticulate::use_virtualenv("python_environment", required = TRUE)

to create the Python environment and the renderUI( ) function to render the SHAP plot. But, this way, Shiny doesn't identify the {shap} package (in the web environment). So, I tried to use his way to install and load the {shap} package and the renderText( ) function to render the plot, but I still couldn’t do it. I'm a begginer in Shinny app, so perhaps I am just spacing out, haha. You gave me more alternatives to think anyway, so... thank you so much again!

hfshr commented 3 years ago

Try changing this line:

reticulate::virtualenv_install("python_environment", packages = "shap")

to this

reticulate::virtualenv_install("python_environment", packages = "shap==0.38.1")

You need to make sure you use an older version of the python shap package as newer versions don't work with fastshap::force_plot() - see https://github.com/bgreenwell/fastshap/issues/12. You may also need the dev version of fastshap remotes::install_github("bgreenwell/fastshap"). It is a bit fiddly to get working but keep persevering and you'll get there!

ornscar commented 3 years ago

Thanks for this tip and also for the support, @hfshr! I'll let you know when things go well. (: