diff --git a/.gitignore b/.gitignore index d1e0fe7d..7501977c 100644 --- a/.gitignore +++ b/.gitignore @@ -113,5 +113,6 @@ docs/notebooks/DiCE_getting_started.ipynb docs/notebooks/DiCE_getting_started_feasible.ipynb docs/notebooks/DiCE_with_advanced_options.ipynb docs/notebooks/DiCE_with_private_data.ipynb +docs/notebooks/*.ipynb diff --git a/MANIFEST.in b/MANIFEST.in index 1cc0757f..6dbce984 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,5 +1,6 @@ include requirements.txt include requirements-deeplearning.txt +include requirements-test.txt include LICENSE recursive-include docs * recursive-include tests *.py diff --git a/dice_ml/counterfactual_explanations.py b/dice_ml/counterfactual_explanations.py index 4c8b45d0..2e499784 100644 --- a/dice_ml/counterfactual_explanations.py +++ b/dice_ml/counterfactual_explanations.py @@ -45,10 +45,10 @@ class CounterfactualExplanations: :param cf_examples_list: A list of CounterfactualExamples instances :param local_importance: List of estimated local importance scores. The - size of the list is the number of input instances, each containing feature - importance scores for that input. + size of the list is the number of input instances, each containing + feature importance scores for that input. :param summary_importance: Estimated global feature importance scores - based on the input set of CounterfactualExamples instances + based on the input set of CounterfactualExamples instances """ def __init__(self, cf_examples_list, @@ -118,6 +118,7 @@ def _check_cf_exp_output_against_json_schema( :param cf_dict: Serialized version of the counterfactual explanations. :type cf_dict: Dict + """ schema_file_name = 'counterfactual_explanations_v{0}.json'.format(version) schema_path = os.path.join(os.path.dirname(__file__), diff --git a/dice_ml/explainer_interfaces/dice_pytorch.py b/dice_ml/explainer_interfaces/dice_pytorch.py index ce84024e..0aaa52f3 100644 --- a/dice_ml/explainer_interfaces/dice_pytorch.py +++ b/dice_ml/explainer_interfaces/dice_pytorch.py @@ -55,7 +55,7 @@ def generate_counterfactuals(self, query_instance, total_CFs, desired_class="opp max_iter=5000, project_iter=0, loss_diff_thres=1e-5, loss_converge_maxiter=1, verbose=False, init_near_query_instance=True, tie_random=False, stopping_threshold=0.5, posthoc_sparsity_param=0.1, posthoc_sparsity_algorithm="linear"): - """Generates diverse counterfactual explanations + """Generates diverse counterfactual explanations. :param query_instance: Test point of interest. A dictionary of feature names and values or a single row dataframe :param total_CFs: Total number of counterfactuals required. @@ -67,7 +67,7 @@ def generate_counterfactuals(self, query_instance, total_CFs, desired_class="opp :param categorical_penalty: A positive float. A weight to ensure that all levels of a categorical variable sums to 1. :param algorithm: Counterfactual generation algorithm. Either "DiverseCF" or "RandomInitCF". :param features_to_vary: Either a string "all" or a list of feature names to vary. - param permitted_range: Dictionary with continuous feature names as keys and permitted min-max range in list as values. + :param permitted_range: Dictionary with continuous feature names as keys and permitted min-max range in list as values. Defaults to the range inferred from training data. If None, uses the parameters initialized in data_interface. :param yloss_type: Metric for y-loss of the optimization function. Takes "l2_loss" or "log_loss" or "hinge_loss". @@ -94,6 +94,7 @@ def generate_counterfactuals(self, query_instance, total_CFs, desired_class="opp Prefer binary search when a feature range is large (for instance, income varying from 10k to 1000k) and only if the features share a monotonic relationship with predicted outcome in the model. + :return: A CounterfactualExamples object to store and visualize the resulting counterfactual explanations (see diverse_counterfactuals.py). """ @@ -417,7 +418,7 @@ def find_counterfactuals(self, query_instance, desired_class, optimizer, learnin max_iter, project_iter, loss_diff_thres, loss_converge_maxiter, verbose, init_near_query_instance, tie_random, stopping_threshold, posthoc_sparsity_param, posthoc_sparsity_algorithm): - """Finds counterfactuals by graident-descent.""" + """Finds counterfactuals by gradient-descent.""" # Prepares user defined query_instance for DiCE. # query_instance = self.data_interface.prepare_query_instance(query_instance=query_instance, encoding='one-hot') diff --git a/dice_ml/explainer_interfaces/dice_tensorflow1.py b/dice_ml/explainer_interfaces/dice_tensorflow1.py index 9de7dee8..8ad5088b 100644 --- a/dice_ml/explainer_interfaces/dice_tensorflow1.py +++ b/dice_ml/explainer_interfaces/dice_tensorflow1.py @@ -21,6 +21,7 @@ def __init__(self, data_interface, model_interface): :param data_interface: an interface class to access data related params. :param model_interface: an interface class to access trained ML model. + """ # initiating data related parameters super().__init__(data_interface) @@ -83,7 +84,7 @@ def generate_counterfactuals(self, query_instance, total_CFs, desired_class="opp :param algorithm: Counterfactual generation algorithm. Either "DiverseCF" or "RandomInitCF". :param features_to_vary: Either a string "all" or a list of feature names to vary. - param permitted_range: Dictionary with continuous feature names as keys and permitted min-max range in list as values. + :param permitted_range: Dictionary with continuous feature names as keys and permitted min-max range in list as values. Defaults to the range inferred from training data. If None, uses the parameters initialized in data_interface. :param yloss_type: Metric for y-loss of the optimization function. Takes "l2_loss" or "log_loss" or "hinge_loss". @@ -112,8 +113,10 @@ def generate_counterfactuals(self, query_instance, total_CFs, desired_class="opp Prefer binary search when a feature range is large (for instance, income varying from 10k to 1000k) and only if the features share a monotonic relationship with predicted outcome in the model. + :return: A CounterfactualExamples object to store and visualize the resulting counterfactual explanations (see diverse_counterfactuals.py). + """ # check feature MAD validity and throw warnings diff --git a/dice_ml/explainer_interfaces/dice_tensorflow2.py b/dice_ml/explainer_interfaces/dice_tensorflow2.py index 34d92534..b8e1ab75 100644 --- a/dice_ml/explainer_interfaces/dice_tensorflow2.py +++ b/dice_ml/explainer_interfaces/dice_tensorflow2.py @@ -19,6 +19,7 @@ def __init__(self, data_interface, model_interface): """Init method :param data_interface: an interface class to access data related params. :param model_interface: an interface class to access trained ML model. + """ # initiating data related parameters super().__init__(data_interface) @@ -69,7 +70,7 @@ def generate_counterfactuals(self, query_instance, total_CFs, desired_class="opp variable sums to 1. :param algorithm: Counterfactual generation algorithm. Either "DiverseCF" or "RandomInitCF". :param features_to_vary: Either a string "all" or a list of feature names to vary. - param permitted_range: Dictionary with continuous feature names as keys and permitted min-max range in list as values. + :param permitted_range: Dictionary with continuous feature names as keys and permitted min-max range in list as values. Defaults to the range inferred from training data. If None, uses the parameters initialized in data_interface. :param yloss_type: Metric for y-loss of the optimization function. Takes "l2_loss" or "log_loss" or "hinge_loss". @@ -100,6 +101,7 @@ def generate_counterfactuals(self, query_instance, total_CFs, desired_class="opp :return: A CounterfactualExamples object to store and visualize the resulting counterfactual explanations (see diverse_counterfactuals.py). + """ # check feature MAD validity and throw warnings if feature_weights == "inverse_mad": diff --git a/dice_ml/explainer_interfaces/explainer_base.py b/dice_ml/explainer_interfaces/explainer_base.py index 37c47b9a..4c256a2b 100644 --- a/dice_ml/explainer_interfaces/explainer_base.py +++ b/dice_ml/explainer_interfaces/explainer_base.py @@ -531,8 +531,8 @@ def misc_init(self, stopping_threshold, desired_class, desired_range, test_pred) def infer_target_cfs_class(self, desired_class_input, original_pred, num_output_nodes): """ Infer the target class for generating CFs. Only called when model_type=="classifier". - TODO: Add support for opposite desired class in multiclass. Downstream methods should decide - whether it is allowed or not. + TODO: Add support for opposite desired class in multiclass. + Downstream methods should decide whether it is allowed or not. """ if desired_class_input == "opposite": if num_output_nodes == 2: diff --git a/dice_ml/model_interfaces/base_model.py b/dice_ml/model_interfaces/base_model.py index 8108ad07..09b49ddd 100644 --- a/dice_ml/model_interfaces/base_model.py +++ b/dice_ml/model_interfaces/base_model.py @@ -26,6 +26,7 @@ def __init__(self, model=None, model_path='', backend='', func=None, kw_args=Non :param func: function transformation required for ML model. If func is None, then func will be the identity function. :param kw_args: Dictionary of additional keyword arguments to pass to func. DiCE's data_interface is appended to the dictionary of kw_args, by default. + """ self.model = model self.model_path = model_path diff --git a/docs/_images/dice_getting_started_api.png b/docs/_images/dice_getting_started_api.png new file mode 100644 index 00000000..926c1256 Binary files /dev/null and b/docs/_images/dice_getting_started_api.png differ diff --git a/docs/_modules/dice_ml/constants.html b/docs/_modules/dice_ml/constants.html new file mode 100644 index 00000000..17899779 --- /dev/null +++ b/docs/_modules/dice_ml/constants.html @@ -0,0 +1,248 @@ + + + + + + + + + + dice_ml.constants — DiCE 0.7 documentation + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + + + + +
+ +
    + +
  • »
  • + +
  • Module code »
  • + +
  • dice_ml.constants
  • + + +
  • + +
  • + +
+ + +
+
+
+
+ +

Source code for dice_ml.constants

+"""Constants for dice-ml package."""
+
+
+
[docs]class BackEndTypes: + Sklearn = 'sklearn' + Tensorflow1 = 'TF1' + Tensorflow2 = 'TF2' + Pytorch = 'PYT' + + ALL = [Sklearn, Tensorflow1, Tensorflow2, Pytorch]
+ + +
[docs]class SamplingStrategy: + Random = 'random' + Genetic = 'genetic' + KdTree = 'kdtree'
+ + +
[docs]class ModelTypes: + Classifier = 'classifier' + Regressor = 'regressor' + + ALL = [Classifier, Regressor]
+ + +class _SchemaVersions: + V1 = '1.0' + V2 = '2.0' + CURRENT_VERSION = V2 + + ALL_VERSIONS = [V1, V2] +
+ +
+ +
+ + +
+
+ +
+ +
+ + + + + + + + + + + \ No newline at end of file diff --git a/docs/_modules/dice_ml/counterfactual_explanations.html b/docs/_modules/dice_ml/counterfactual_explanations.html index b750956a..dd54d293 100644 --- a/docs/_modules/dice_ml/counterfactual_explanations.html +++ b/docs/_modules/dice_ml/counterfactual_explanations.html @@ -7,7 +7,7 @@ - dice_ml.counterfactual_explanations — DiCE 0.5 documentation + dice_ml.counterfactual_explanations — DiCE 0.7 documentation @@ -31,6 +31,8 @@ + + @@ -88,9 +90,11 @@

Notebooks: