SeldonIO / alibi

Algorithms for explaining machine learning models
https://docs.seldon.io/projects/alibi/en/stable/
Other
2.41k stars 252 forks source link

Counterfactual instance out of predefined range for the function CounterfactualRLTabular" #549

Closed NikkiRoodenrijs closed 2 years ago

NikkiRoodenrijs commented 2 years ago

Hi!!

For the function CounterFactualRLTabular the ranges for age are defined in such a way that the age feature cannot decrease. However, I noticed, running the exact same notebook "Counterfactual with Reinforcement Learning (CFRL) on Adult Census" (https://github.com/SeldonIO/alibi/blob/master/examples/cfrl_adult.ipynb), with the only change being that I run the counterfactual instances for more than 5 examples (see line 18/19), that for some instances the age decreased. For example in line 9 the original instance the age is 47 and for the counterfactual instance it is 48.

Schermafbeelding 2021-12-04 om 16 36 33 Schermafbeelding 2021-12-04 om 16 36 52

Since I am not getting any errors or warnings, I think that there is a bug in the code. Can you confirm this to me or do you think there is another problem that causes the counterfactual instance to be out of the predefined range. I hope you can give me some advice as to how to cope with the generated counterfactuals being out of the predefined ranges.

Best, Nikki

RobertSamoilescu commented 2 years ago

Thanks @NikkiRoodenrijs for reporting this issue. The problem is due to some rounding error. This is because currently we apply the constraints in the standardized feature space and then project the features back to the original input space by the reverse transformation. For integer features, we cast them back to int and this can cause the projected feature to differ by 1 from the original value.

We will fix this problem shortly.

RobertSamoilescu commented 2 years ago

@NikkiRoodenrijs, PR #550 solves the issue you mentioned. It will be probably merged soon into the master branch, but if it is urgent, then you can use the implementation in the PR.

Thank you for reporting the issue and please let me know if this fixes the problem.

NikkiRoodenrijs commented 2 years ago

Unfortunately, this does not solve the issue. The following error appears when I fit the explainer.

Schermafbeelding 2021-12-05 om 12 11 24
RobertSamoilescu commented 2 years ago

@NikkiRoodenrijs, that is caused by your numpy version. When I tested it I used 1.20.3. I will soon modify the code to work for older versions too. Thanks!

NikkiRoodenrijs commented 2 years ago

It now works for me! Thanks!!

jklaise commented 2 years ago

Closed by #550.