mayer79 / flashlight

Machine learning explanations
https://mayer79.github.io/flashlight/
GNU General Public License v2.0
22 stars 4 forks source link

[question] any way of using flashlight with tidymodels #47

Closed verajosemanuel closed 3 years ago

verajosemanuel commented 3 years ago

I use tidymodels framework for my models, and I guess if flashlight could be suitable for model explanation.

I have been searching for code examples but no success so far

any hint? (xgboost classification model)

thanks in advance

mayer79 commented 3 years ago

Good point! There is no vignette yet on how to use flashlight with tidymodels. I shall add one in the next release.

In general, classification is only supported if the prediction function returns exactly one numeric value per observation.

For regression and a lm model, a working example is:

library(tidymodels)

lm_mod <- 
  linear_reg() %>% 
  set_engine("lm")

lm_fit <- 
  lm_mod %>% 
  fit(Sepal.Width ~ Sepal.Length * Species + Petal.Width,
      data = iris)
lm_fit

predict(lm_fit, iris)

library(flashlight)
library(MetricsWeighted)

fl <- flashlight(
  model = lm_fit,
  label = "a tidy model",
  y = "Sepal.Width",
  data = iris,
  metrics = list(RMSE = rmse, MAE = mae),
  predict_function = function(model, data) predict(model, data)$.pred
)

light_performance(fl) %>% 
  plot(fill = "orange") +
  xlab(element_blank())

light_importance(fl, m_repetitions = 4) %>% 
  plot(fill = "orange")

inter <- light_interaction(fl, pairwise = TRUE)
plot(inter, fill = "orange")

light_profile(fl, v = "Petal.Width", type = "ale") %>% 
  plot()

light_profile(fl, v = "Petal.Width", type = "ale",
              by = "Species") %>% 
  plot()
verajosemanuel commented 3 years ago

Great!