thomasp85 / lime

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

error explain function #125

Closed JLB9791 closed 5 years ago

JLB9791 commented 6 years ago

Hi Thomas,

great package! Thanks for that. I get a weird error that I can't seem to solve on the explain function. I followed all the steps from https://shiring.github.io/machine_learning/2017/04/23/lime on my data with the necessary changes of course to align with my work. I've build a classifier model with CARET and xgbtree

### --- try to explaining complex algorithms with lime function
explainer <- lime(df_train, mod_fit, bin_continuous = TRUE, n_bins = 5, n_permutations = 1000)

# select randomly 3 cases where the prediction was correct and 3 cases where the prediction was wrong
pred <- data.frame(sample_id = 1:nrow(df_test),
                   predict(mod_fit, df_test, type = "prob"),
                   actual = df_test[, y])
  pred$prediction <- colnames(pred)[2:4][apply(pred[, 2:4], 1, which.max)]
  pred$correct <- ifelse(pred$actual == pred$prediction, "correct", "wrong")

# Beware that we need to give our test-set data table row names with the sample names 
# or IDs to be displayed in the header of our explanatory plots below.
pred_cor <- filter(pred, correct == "correct")
pred_wrong <- filter(pred, correct == "wrong")

df_test_cor <- df_test %>%
  mutate(sample_id = 1:nrow(df_test)) %>%
  filter(sample_id %in% pred_cor$sample_id) %>%
  sample_n(size = 3) %>%
  remove_rownames() %>%
  tibble::column_to_rownames(var = "sample_id") %>%
  select(-bought_highbrow_wines)

df_test_wrong <- df_test %>%
  mutate(sample_id = 1:nrow(df_test)) %>%
  filter(sample_id %in% pred_wrong$sample_id) %>%
  sample_n(size = 3) %>%
  remove_rownames() %>%
  tibble::column_to_rownames(var = "sample_id") %>%
  select(-bought_highbrow_wines)

# to keep it readable select top x features for graphical display. Features are selected with highest weight, however other options are possible.
explanation_cor <- lime::explain(df_test_cor, explainer, n_labels=2, n_features = 5) 

the last line throws me the following error: Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE, : arguments imply differing number of rows: 15000, 0

I any suggestions?

thx JL

thomasp85 commented 6 years ago

Not really... if you provide a reproducible example I can look into it

JLB9791 commented 5 years ago

Hi Thomas, the issue is solved. It was due to not finding the correct version of udunits2 on linux redhat server. Nothing to do with your package. Sorry for the inconvenience.