GLOBE-CE¶
Global Counterfactual Explanations
GLOBE-CE finds universal transformations that apply across an entire dataset.
Overview¶
GLOBE-CE identifies a single transformation direction that, when applied to instances, changes their predictions to the target class.
Usage¶
from counterfactuals.cf_methods.global_methods.globe_ce import GLOBE_CE
method = GLOBE_CE(
disc_model=classifier,
dataset_config=dataset_config
)
result = method.explain(
X=X_test,
y_target=target_class
)
API Reference¶
GLOBE_CE
¶
GLOBE_CE(predict_fn, dataset, X, affected_subgroup=None, dropped_features=[], ordinal_features=[], delta_init='zeros', normalise=None, bin_widths=None, monotonicity=None, p=1)
(required arguments) predict_fn : Contains predict function dataset : Custom dataset wrapper that includes the data (there is no direct need to pass x_aff as an argument) as well as categorical/continuous features information x_aff : Pandas DataFrame. Inputs in training data which received a negative prediction
(optional arguments) lr : learning rate for gradient descent optimizer lams : hyperparameters for objective function (currently using softmax prediction + l1 distance regularization) delta_init: initial global translation before optimization is performed (default 0) cuda : if GPU is to be used
Source code in counterfactuals/cf_methods/global_methods/globe_ce/globe_ce.py
10 11 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 | |
round_categorical
¶
This function is used after the optimization to compute the actual counterfactual Currently not implemented for optimization: argmax will likely break gradient descent
Input: counterfactuals computed using x_aff + global translation Output: valid counterfactuals where one_hot encodings are integers (0 or 1), not floats
Source code in counterfactuals/cf_methods/global_methods/globe_ce/globe_ce.py
compute_costs
¶
Compute the costs of the counterfactuals
Source code in counterfactuals/cf_methods/global_methods/globe_ce/globe_ce.py
evaluate
¶
Evaluate the performance of delta. Returns prediction/cost vectors
delta (numpy), idxs (numpy, optional), none_type (str, optional),
vector (bool)
Output: predictions, costs (0 or inf or nan where predictions are 0) return types are numpy if vector else floats
Source code in counterfactuals/cf_methods/global_methods/globe_ce/globe_ce.py
rules
¶
Return feature-wise dictionary of GCEs according to delta
delta (numpy), idxs (numpy, optional), none_type (str, optional),
vector (bool)
Output: predictions, costs (0 or inf or nan where predictions are 0) return types are numpy if vector else floats
Source code in counterfactuals/cf_methods/global_methods/globe_ce/globe_ce.py
monotonic
staticmethod
¶
Drops all x_i, y_i pairs (x and y have same length) which result in a decrease in y as x increases This function is used to flatten the coverage vs cost profile Consider moving functions like this to a universal src file
x (typically the cost vector, numpy)
y (typically the coverage vector, numpy)
Output: predictions, costs (0 or inf or nan where predictions are 0) return types are numpy if vector else floats
Source code in counterfactuals/cf_methods/global_methods/globe_ce/globe_ce.py
lower_bounds_k
¶
Returns the lower bounds for the k values of the GCEs, given the delta vector and according to the categorical features
Source code in counterfactuals/cf_methods/global_methods/globe_ce/globe_ce.py
scale
¶
scale(delta, scalars='auto', disable_tqdm=False, x_aff=None, n_scalars=1000, vector=False, plot=False, none_type=None, eps=None, non_zero_costs=False)
Scale the delta vector by a scalar and return the coverage, cost, and scalars
Source code in counterfactuals/cf_methods/global_methods/globe_ce/globe_ce.py
bisection
¶
Returns the maximum scalar for which the coverage is above thresh
Source code in counterfactuals/cf_methods/global_methods/globe_ce/globe_ce.py
cluster_continuous
¶
Clusters the continuous features according to the costs, returns scalar_idxs
Source code in counterfactuals/cf_methods/global_methods/globe_ce/globe_ce.py
cluster_by_costs
¶
Clusters the continuous features according to the costs.
Source code in counterfactuals/cf_methods/global_methods/globe_ce/globe_ce.py
evaluate_clustering
¶
evaluate_clustering(delta, scalars, max_scalar_idxs, costs=None, x_aff=None, print_outputs=True, vector=False, eps=0, latex_table=False)
Evaluates the clustering by computing the coverage and cost for each cluster
Source code in counterfactuals/cf_methods/global_methods/globe_ce/globe_ce.py
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 | |