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

TypeError: _generate_counterfactuals() got an unexpected keyword argument 'feature_weights' #406

Open Siki-cloud opened 1 year ago

Siki-cloud commented 1 year ago

When running the below code in DICE_with_advanced_option.ipynb, it dumps due to the the below error: code:

generate counterfactuals

dice_exp = exp.generate_counterfactuals(query_instance, total_CFs=4, desired_class="opposite", feature_weights=feature_weights)

error: TypeError Traceback (most recent call last) Cell In[18], line 2 1 # generate counterfactuals ----> 2 dice_exp = exp.generate_counterfactuals(query_instance, total_CFs=4, desired_class="opposite", 3 feature_weights=feature_weights)

..\dice_ml\explainer_interfaces\explainer_base.py:161, in ExplainerBase.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) 159 for query_instance in tqdm(query_instances_list): 160 self.data_interface.set_continuous_feature_indexes(query_instance) --> 161 res = self._generate_counterfactuals( 162 query_instance, total_CFs, 163 desired_class=desired_class, 164 desired_range=desired_range, 165 permitted_range=permitted_range, 166 features_to_vary=features_to_vary, 167 stopping_threshold=stopping_threshold, 168 posthoc_sparsity_param=posthoc_sparsity_param, 169 posthoc_sparsity_algorithm=posthoc_sparsity_algorithm, 170 verbose=verbose, 171 kwargs) 172 cf_examples_arr.append(res) 173 self._check_any_counterfactuals_computed(cf_examples_arr=cf_examples_arr)

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

gaugup commented 1 year ago

@Siki-cloud The API generate_counterfacutals() doesn't support feature_weights https://github.com/interpretml/DiCE/blob/8277afe559f10904c75dbd6f52a63a0193581017/dice_ml/explainer_interfaces/explainer_base.py#L122. Is this issue a feature request?

Siki-cloud commented 1 year ago

@Siki-cloud The API generate_counterfacutals() doesn't support feature_weights

https://github.com/interpretml/DiCE/blob/8277afe559f10904c75dbd6f52a63a0193581017/dice_ml/explainer_interfaces/explainer_base.py#L122

. Is this issue a feature request?

I noticed that DiceRandom encounters errors, but is feature_weight not required for random-based? That's my understanding. However, for gradient-based methods, there is a custom feature_weight functionality implemented, which should be sufficient. Thank you.

gustavo-rddi commented 4 months ago

I encountered this issue too. The confusion is that this page in the documentation states that the user can pass a parameter called feature_weights, which is a dictionary with weights for each feature. However, as pointed out, this is not actually implemented in generate_counterfactuals. Instead, that method supports other 'weight' parameters: proximity_weight, sparsity_weight, diversity_weight; however, these parameters expect a positive float, meaning that the user cannot specify a different value for each feature. Furthermore, beyond the docstring, the documentation does not explain what their effect is, and it says they are ignored when using the 'random' and 'kdtree' methods.

I would like to have a way to specify that some features are harder to change than others, just as explained in the text on this page. I'm using sklearn pre-trained pipelines, so I cannot use gradient-based methods. How could I achieve this in the current implementation? Please advise. Thanks!