Can the R version of lime explain xgboost models with count:poisson objective function?

拟墨画扇 提交于 2021-02-07 19:14:20

问题


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)

来源:https://stackoverflow.com/questions/49280345/can-the-r-version-of-lime-explain-xgboost-models-with-countpoisson-objective-fu

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!