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

Can lime explain xgboost models with count:poisson objective function? #84

Open SimonCoulombe opened 6 years ago

SimonCoulombe commented 6 years ago

note: I first posted this issue on stackoverflow, then realised this was probably a better place to ask. (https://stackoverflow.com/questions/49280345/can-the-r-version-of-lime-explain-xgboost-models-with-countpoisson-objective-fu)

I generated a model using xgb.train with the "count:poisson" objective function and I get the following error when trying to create the explainer:

Error: Unsupported model type Lime works when I replace the objective by something else such as reg:logistic. Is there a way to explain count:poisson in lime? thanks

reproducible example:


library(xgboost)
library(dplyr)
library(caret)
library(insuranceData) # example dataset https://cran.r-project.org/web/packages/insuranceData/insuranceData.pdf
library(lime) # Local Interpretable Model-Agnostic Explanations
set.seed(123)
data(dataCar)
mydb <- dataCar %>% select(clm, exposure, veh_value, veh_body,
                           veh_age, gender, area, agecat)

label_var <- "clm"  
offset_var <- "exposure"
feature_vars <- mydb %>% 
  select(-one_of(c(label_var, offset_var))) %>% 
  colnames()

#preparing data for xgboost (one hot encoding of categorical (factor) data
myformula <- paste0( "~", paste0( feature_vars, collapse = " + ") ) %>% as.formula()
dummyFier <- caret::dummyVars(myformula, data=mydb, fullRank = TRUE)
dummyVars.df <- predict(dummyFier,newdata = mydb)
mydb_dummy <- cbind(mydb %>% select(one_of(c(label_var, offset_var))), 
                    dummyVars.df)
rm(myformula, dummyFier, dummyVars.df)

feature_vars_dummy <-  mydb_dummy  %>% select(-one_of(c(label_var, offset_var))) %>% colnames()

xgbMatrix <- xgb.DMatrix(
  data = mydb_dummy %>% select(feature_vars_dummy) %>% as.matrix, 
  label = mydb_dummy %>% pull(label_var),
  missing = "NAN")

#model 1: this does not
myParam <- list(max.depth = 2,
                eta = .01,
                gamma = 0.001,
                objective = 'count:poisson',
                eval_metric = "poisson-nloglik")

booster <- xgb.train(
  params = myParam, 
  data = xgbMatrix, 
  nround = 50)

explainer <- lime(mydb_dummy %>% select(feature_vars_dummy), 
                  model = booster)

explanation <- explain(mydb_dummy %>% select(feature_vars_dummy) %>% head,
                       explainer,
                       n_labels = 1, 
                       n_features = 2)
#Error: Unsupported model type
#model 2 : this works
myParam2 <- list(max.depth = 2,
                eta = .01,
                gamma = 0.001,
                objective = 'reg:logistic',
                eval_metric = "logloss")

booster2 <- xgb.train(
  params = myParam2, 
  data = xgbMatrix, 
  nround = 50)

explainer <- lime(mydb_dummy %>% select(feature_vars_dummy), 
                  model = booster)

explanation <- explain(mydb_dummy %>% select(feature_vars_dummy) %>% head,
                       explainer,
                       n_features = 2)

plot_features(explanation)
thomasp85 commented 6 years ago

@pommedeterresautee can you look into if this can be fixed?

blah-crusader commented 5 years ago

Did you find a solution for your problem? I'm struggling with the same issue!

ckineza commented 5 years ago

Hello, I would like to know the status of this issue. It would be great having this feature included. please let us know. Thank you for your time and for creating such a great tool.