Counterfactual Explainer
interpretation.explainer.agnostic.counterfactual.CounterfactualExplainer
Bases: AgnosticExplainer
Counterfactual explainer using methods proposed by Wachter et al. (2018).
Source code in interpretation/explainer/agnostic/counterfactual/explainer.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | |
__init__(input_model, input_data)
Initialise the explainer by taking in the model to explain and the observed data to be used for to estimate the MAD and sampling.
Source code in interpretation/explainer/agnostic/counterfactual/explainer.py
14 15 16 17 18 19 20 21 22 23 24 | |
dandl(X, desired_y, k=5, topn=10)
Computes a counterfactual via nondominated sorting genetic algorithm.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
NDArray
|
The instance to be explained by the counterfactual, (n_features). |
required |
desired_y
|
float
|
The predefined prediction for the counterfactual explanation to produce. |
required |
k
|
int
|
Number of samples taken to take average in fourth objective, k > 0, by default 5. |
5
|
topn
|
int
|
Top n counterfactual to return, by default 10. |
10
|
Returns:
| Type | Description |
|---|---|
NDArray
|
Counterfactual instances with shape (n_candidates, n_features). |
Source code in interpretation/explainer/agnostic/counterfactual/explainer.py
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | |
explain(X, desired_y, method='wachter', **method_kwargs)
Computes a counterfactual that produces a model output specified by desired_y. Only supports scalar labels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
NDArray
|
The instance to be explained by the counterfactual, (n_features). |
required |
desired_y
|
float
|
The predefined prediction for the counterfactual explanation to produce. |
required |
method
|
str
|
The method used to compute the counterfactual, either 'wachter' or 'dandl', by default 'wachter'.
|
'wachter'
|
**method_kwargs
|
Additional keyword arguments passed directly to the selected
counterfactual generation method. See the documentation of
:meth: |
{}
|
Returns:
| Type | Description |
|---|---|
NDArray
|
One Counterfactual instance for wachter and multiple instances for dandl. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in interpretation/explainer/agnostic/counterfactual/explainer.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | |
wachter(X, desired_y, lambda_initial=0.01, lambda_max=10000.0, lambda_multiplier=10.0, tol=0.001)
Computes a counterfactual using method proposed by Wachter et al.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
NDArray
|
The instance to be explained by the counterfactual, (n_features). |
required |
desired_y
|
float
|
The predefined prediction for the counterfactual explanation to produce. |
required |
lambda_initial
|
float
|
Initial lambda, by default 1e-2 |
0.01
|
lambda_max
|
float
|
Maximum lambda, by default 1e4 |
10000.0
|
lambda_multiplier
|
float
|
Multiplier of lambda after each iteration, by default 10 |
10.0
|
tol
|
float
|
Tolerance for prediction error, the function increases lambda exponentially if prediction error is higher than tol, by default 1e-3. |
0.001
|
Returns:
| Type | Description |
|---|---|
NDArray
|
Counterfactual instance. |
Source code in interpretation/explainer/agnostic/counterfactual/explainer.py
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | |