docstr: Finds counterfactuals by querying a K-D tree for the nearest data points in the desired class from the dataset.
docstr_tok: ["Finds","counterfactuals","by","querying","a","K-D","tree","for","the","nearest","data","points","in","the","desired","class","from","the","dataset","."]
code: def find_counterfactuals(
      self,
      data_df_copy,
      query_instance,
      query_instance_orig,
      desired_range,
      desired_class,
      total_CFs,
      features_to_vary,
      permitted_range,
      sparsity_weight,
      stopping_threshold,
      posthoc_sparsity_param,
      posthoc_sparsity_algorithm,
      verbose,
      limit_steps_ls,
  ):
      """Finds counterfactuals by querying a K-D tree for the nearest data points in the desired class from the dataset."""
  
      start_time = timeit.default_timer()
  
      # Making the one-hot-encoded version of query instance match the one-hot encoded version of the dataset
      query_instance_df_dummies = pd.get_dummies(query_instance_orig)
  
      data_df_columns = pd.get_dummies(
          data_df_copy[self.data_interface.feature_names]
      ).columns
      for col in data_df_columns:
          if col not in query_instance_df_dummies.columns:
              query_instance_df_dummies[col] = 0
  
      # Fix order of columns in the query instance. This is necessary because KD-tree treats data as a simple array
      # instead of a dataframe.
      query_instance_df_dummies = query_instance_df_dummies.reindex(
          columns=data_df_columns
      )
  
      self.final_cfs, cfs_preds = self.vary_valid(
          query_instance_df_dummies,
          total_CFs,
          features_to_vary,
          permitted_range,
          query_instance_orig,
          sparsity_weight,
      )
  
      total_cfs_found = len(self.final_cfs)
      if total_cfs_found > 0:
          # post-hoc operation on continuous features to enhance sparsity - only for public data
          if (
              posthoc_sparsity_param is not None
              and posthoc_sparsity_param > 0
              and "data_df" in self.data_interface.__dict__
          ):
              self.final_cfs_df_sparse = copy.deepcopy(self.final_cfs)
              self.final_cfs_df_sparse = (
                  self.do_posthoc_sparsity_enhancement(
                      self.final_cfs_df_sparse,
                      query_instance,
                      posthoc_sparsity_param,
                      posthoc_sparsity_algorithm,
                      limit_steps_ls,
                  )
              )
          else:
              self.final_cfs_df_sparse = None
      else:
          self.final_cfs_df_sparse = None
  
      self.final_cfs_df = self.final_cfs
      if total_cfs_found > 0:
          self.round_to_precision()
  
      self.elapsed = timeit.default_timer() - start_time
  
      m, s = divmod(self.elapsed, 60)
  
      if verbose:
          if total_cfs_found < total_CFs:
              self.elapsed = timeit.default_timer() - start_time
              m, s = divmod(self.elapsed, 60)
              print(
                  "Only %d (required %d) "
                  % (total_cfs_found, total_CFs),
                  "Diverse Counterfactuals found for the given configuation, perhaps ",
                  "change the query instance or the features to vary..."
                  "; total time taken: %02d" % m,
                  "min %02d" % s,
                  "sec",
              )
          else:
              print(
                  "Diverse Counterfactuals found! total time taken: %02d"
                  % m,
                  "min %02d" % s,
                  "sec",
              )
  
      return query_instance, cfs_preds
  
file_path: DiCE/dice_ml/explainer_interfaces/dice_KD.py
imports: import copy
  import timeit
  import numpy
  import pandas
  from dice_ml import diverse_counterfactuals
  from dice_ml.constants import ModelTypes
  from dice_ml.explainer_interfaces.explainer_base import ExplainerBase
label_desc: Use image node_id 8 for calling the DiceKD obj's underlying member method code with example usage: obj.find_counterfactuals(data_df_copy, query_instance, query_instance_orig, desired_range, desired_class, total_CFs, features_to_vary, permitted_range, sparsity_weight, stopping_threshold, posthoc_sparsity_param, posthoc_sparsity_algorithm, verbose, limit_steps_ls) and returns: query_instance,  cfs_preds