dandls / counterfactuals

counterfactuals: An R package for Counterfactual Explanation Methods
https://dandls.github.io/counterfactuals/
GNU Lesser General Public License v3.0
21 stars 4 forks source link

Error when running example in mlr3 book #42

Open larskotthoff opened 1 week ago

larskotthoff commented 1 week ago

I'm getting an error when I run this example from the mlr3 book:

Error in whatif$find_counterfactuals(Charlie, desired_class = "good",  : 
  `x_interested` is already predicted with `desired_prob` for `desired_class`.

traceback:

2: stop("`x_interested` is already predicted with `desired_prob` for `desired_class`.")
1: whatif$find_counterfactuals(Charlie, desired_class = "good", 
       desired_prob = c(0.75, 1))

Complete example to reproduce:

library(mlr3verse)
tsk_german = tsk("german_credit")$select(
  cols = c("duration", "amount", "age", "status", "savings", "purpose",
  "credit_history", "property", "employment_duration", "other_debtors"))
split = partition(tsk_german)
lrn_gbm = lrn("classif.gbm", predict_type = "prob")
lrn_gbm$train(tsk_german, row_ids = split$train)
# features in test data
credit_x = tsk_german$data(rows = split$test,
  cols = tsk_german$feature_names)
# target in test data
credit_y = tsk_german$data(rows = split$test,
  cols = tsk_german$target_names)

library(iml)
predictor = Predictor$new(lrn_gbm, data = credit_x, y = credit_y)
Charlie = credit_x[35, ]

library(counterfactuals)
whatif = WhatIfClassif$new(predictor, n_counterfactuals = 1L)
cfe = whatif$find_counterfactuals(Charlie,
  desired_class = "good", desired_prob = c(0.75, 1))
dandls commented 1 week ago

Thanks for reaching out! This error message appears because the model already predicts a probability > 75% for class good for Charlie, so computing a counterfactual with a prob > 75% with WhatIfClassif` (or any other method) makes no sense.

The reason why no error message appears in the book is that for the book we use a specific seed at the beginning of each chapter (https://mlr3book.mlr-org.com/chapters/chapter1/introduction_and_overview.html#reproducibility). If you use set.seed(123) and run the above code (thanks for providing it!), no error message should be thrown.

larskotthoff commented 1 week ago

Thanks -- I get the error with seed 123 set, and the build of the book is failing with this error message as well at the moment.