Skip to content

numpy>=2.0.0 results in a bug in demo code #474

@marat-davudov

Description

@marat-davudov

When running the demo code, I've encountered the following error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[3], line 5
      1 # Generate counterfactual examples
      2 query_instance = test_dataset.drop(columns="income")[0:1]
      3 
      4 print(f"Query instance:\n{query_instance}\nShape {query_instance.shape}")
----> 5 dice_exp = exp.generate_counterfactuals(query_instance, total_CFs=4, desired_class="opposite")
      6 # Visualize counterfactual explanation
      7 dice_exp.visualize_as_dataframe()

File <...>/.venv/lib/python3.12/site-packages/dice_ml/explainer_interfaces/explainer_base.py:184, in ExplainerBase.generate_counterfactuals(self, query_instances, total_CFs, desired_class, desired_range, permitted_range, features_to_vary, stopping_threshold, posthoc_sparsity_param, posthoc_sparsity_algorithm, verbose, **kwargs)
    182 for query_instance in tqdm(query_instances_list):
    183     self.data_interface.set_continuous_feature_indexes(query_instance)
--> 184     res = self._generate_counterfactuals(
    185         query_instance, total_CFs,
    186         desired_class=desired_class,
    187         desired_range=desired_range,
    188         permitted_range=permitted_range,
    189         features_to_vary=features_to_vary,
    190         stopping_threshold=stopping_threshold,
    191         posthoc_sparsity_param=posthoc_sparsity_param,
    192         posthoc_sparsity_algorithm=posthoc_sparsity_algorithm,
    193         verbose=verbose,
    194         **kwargs)
    195     res.test_instance_df = self.data_interface.ensure_consistent_type(
    196             res.test_instance_df, query_instance)
    197     if res.final_cfs_df is not None and len(res.final_cfs_df) > 0:

File <..>.venv/lib/python3.12/site-packages/dice_ml/explainer_interfaces/dice_random.py:88, in DiceRandom._generate_counterfactuals(self, query_instance, total_CFs, desired_range, desired_class, permitted_range, features_to_vary, stopping_threshold, posthoc_sparsity_param, posthoc_sparsity_algorithm, sample_size, random_seed, verbose, limit_steps_ls)
     86 test_pred = model_predictions[0]
     87 if self.model.model_type == ModelTypes.Classifier:
---> 88     self.target_cf_class = self.infer_target_cfs_class(desired_class, test_pred, self.num_output_nodes)
     89 elif self.model.model_type == ModelTypes.Regressor:
     90     self.target_cf_range = self.infer_target_cfs_range(desired_range)

File <..>/.venv/lib/python3.12/site-packages/dice_ml/explainer_interfaces/explainer_base.py:701, in ExplainerBase.infer_target_cfs_class(self, desired_class_input, original_pred, num_output_nodes)
    699 elif num_output_nodes == 1:  # only for pytorch DL model
    700     original_pred_1 = np.round(original_pred)
--> 701     target_class = int(1-original_pred_1)
    702     return target_class
    703 elif num_output_nodes > 2:

TypeError: only 0-dimensional arrays can be converted to Python scalars

I've initially installed the package with:

uv add dice-ml

As per README.md recommendation, then reinstalled the environment from scratch using the requirement files, however that also didn't help.

SOLUTION:

pinning numpy<2.0.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions