interpretml / DiCE

Generate Diverse Counterfactual Explanations for any machine learning model.
https://interpretml.github.io/DiCE/
MIT License
1.37k stars 188 forks source link

Cannot perform DataFrame operations on generated counterfactuals #388

Open LakshmanKishore opened 1 year ago

LakshmanKishore commented 1 year ago

I'm using dice-ml - 0.6.1 version. I wanted to perform some DataFrame operations on the generated counterfactuals. image Here I have the results of the generated counterfactuals.

image This is the DataFrame that was generated, and I wanted to replace the target column with the original value (unencoded value), but I failed to replace the value, after some search I found a workaround.

image This is the way I found by which I can modify the DataFrame and which will affect in the cfs.to_json() method.

In Regression Problem, the counterfactuals generated contained too many decimals which should be rounded, so I used the same way i.e, modifying the final_cfs_df dataframe to round the decimals.

Can we have a method which can perform all the DataFrame operations that directly affects the to_json() method?

gaugup commented 1 year ago

@LakshmanKishore, you could save the generated counterfactuals as a .csv file on the disk like following:-

# Save generated counterfactual examples to disk
dice_exp.cf_examples_list[0].final_cfs_df.to_csv(path_or_buf='counterfactuals.csv', index=False)

You could then consume the counterfactuals for whichever query point by reading the .csv file.

import pandas as pd
pd.read_csv('counterfactuals.csv')

Let us know if that can address your concern.

LakshmanKishore commented 1 year ago

This is a workaround that I already have found. As you can see from the screenshots above. I don't want to save the generated counterfactuals as csv file.