thomasp85 / lime

Local Interpretable Model-Agnostic Explanations (R port of original Python package)
https://lime.data-imaginist.com/
Other
481 stars 109 forks source link

Compatibility with tidymodels #189

Closed GitHunter0 closed 2 years ago

GitHunter0 commented 2 years ago

I really like how lime integrates well with H2O, do you have plans to also add support for tidymodels? It would be very helpful since it is becoming the default modeling ecosystem for R. Thank you

mattwarkentin commented 2 years ago

Late to the party here, @GitHunter0 - but {lime} already has support for {parsnip} models via the predict_model.model_fit method: https://github.com/thomasp85/lime/blob/0281c56e6da697c686e2d7761dfc7a658decb3ca/R/models.R#L117-L127

Here is a simple example:

library(tidymodels)
library(lime)

model <- fit(
  object = rand_forest('regression'),
  data = iris,
  formula = Sepal.Length ~ .
)

explainer <- lime(iris, model)

explanation <- explain(
  x = slice(iris, 1:6),
  explainer = explainer,
  n_features = 4
)

plot_features(explanation)

If you're using {workflows} objects instead of {parsnip} objects, then you just need to extract the {parsnip} model object with extract_fit_parsnip(workflow). For example:

wflow <- 
  workflow(
    preprocessor = Sepal.Length ~ .,
    spec = rand_forest('regression')
  ) %>% 
  fit(data = iris)

explainer <- lime(iris, extract_fit_parsnip(wflow))
GitHunter0 commented 2 years ago

I appreciate the clarification, @mattwarkentin .