Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when running example in mlr3 book #42

Open
larskotthoff opened this issue Sep 5, 2024 · 2 comments
Open

Error when running example in mlr3 book #42

larskotthoff opened this issue Sep 5, 2024 · 2 comments

Comments

@larskotthoff
Copy link

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
Copy link
Owner

dandls commented Sep 6, 2024

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
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants