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

"feature_weights" not working #443

Open
DarthZonke opened this issue Aug 24, 2024 · 1 comment
Open

"feature_weights" not working #443

DarthZonke opened this issue Aug 24, 2024 · 1 comment

Comments

@DarthZonke
Copy link

DarthZonke commented Aug 24, 2024

I cannot use "feature_weights" on DICE, I am getting the error below, please assist:

Code:
query_instance = denied_loans.drop(columns="predicted")
dice_exp = exp.generate_counterfactuals(query_instance,
total_CFs=1,
desired_class="opposite",
features_to_vary=vary_feature,
proximity_weight=1,
diversity_weight=0,
categorical_penalty=1, .
feature_weights=feature_weights,
random_seed=42,
verbose=True,
method="genetic")

dice_exp.visualize_as_dataframe()

Error:


TypeError Traceback (most recent call last)
in <cell line: 2>()
1 query_instance = denied_loans.drop(columns="predicted")
----> 2 dice_exp = exp.generate_counterfactuals(query_instance,
3 total_CFs=1,
4 desired_class="opposite",
5 features_to_vary=vary_feature,

/usr/local/lib/python3.10/dist-packages/dice_ml/explainer_interfaces/explainer_base.py in generate_counterfactuals(self, query_instances, total_CFs, desired_class, desired_range, permitted_range, features_to_vary, stopping_threshold, posthoc_sparsity_param, proximity_weight, sparsity_weight, diversity_weight, categorical_penalty, posthoc_sparsity_algorithm, verbose, **kwargs)
184 for query_instance in tqdm(query_instances_list):
185 self.data_interface.set_continuous_feature_indexes(query_instance)
--> 186 res = self._generate_counterfactuals(
187 query_instance, total_CFs,
188 desired_class=desired_class,

TypeError: DiceRandom._generate_counterfactuals() got an unexpected keyword argument 'feature_weights

@mpereda
Copy link

mpereda commented Feb 13, 2025

Hi!

I have experienced the same problem. The source code regarding the method for generating CFs shows that the argument 'feature_weights' is not implemented. It just uses the default option which is the MADs https://interpret.ml/DiCE/_modules/dice_ml/explainer_interfaces/dice_random.html

However, user-defined feature_weights can used with the genetic algorithm method. Following the example in https://interpret.ml/DiCE/notebooks/DiCE_with_advanced_options.html, you can have:

exp2= dice_ml.Dice(d, m, method="genetic")
dice_exp2 = exp2.generate_counterfactuals(query_df, total_CFs=4, desired_class="opposite", feature_weights=feature_weights)
dice_exp2.visualize_as_dataframe(show_only_changes=True)

and the output will be:
age workclass education marital_status occupation race gender hours_per_week income
0 - - Doctorate Married White-Collar - Male - 1
0 - - Bachelors Married Blue-Collar - Male - 1
0 - - Some-college Married White-Collar - Male - 1
0 26 - Bachelors Married White-Collar - - - 1

I hope this helps!
Best,
María

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

No branches or pull requests

2 participants