AReS¶
Anchor/Rule-based Explanations
AReS generates rule-based global explanations.
Overview¶
AReS identifies interpretable rules that describe when and how predictions can be changed.
Usage¶
from counterfactuals.cf_methods.global_methods import AReS
method = AReS(
gen_model=gen_model,
disc_model=classifier,
disc_model_criterion=criterion,
device="cuda"
)
result = method.explain(
X=X_test,
y_origin=y_test,
y_target=target_class,
X_train=X_train,
y_train=y_train
)
API Reference¶
AReS
¶
AReS(predict_fn, dataset, X, dropped_features=[], n_bins=10, ordinal_features=[], normalise=False, constraints=[20, 7, 10], correctness=False)
(required arguments) model : Any black box model with a predict() method that returns a binary prediction x_aff : Pandas DataFrame. Full training data of interest (positive and negative predictions) dataset : Our custom dataset_loader object including the data (there is no direct need to pass X as an argument) and information on categorical/continuous features
(optional arguments) dropped_features : List of dropped features in the form of just the feature e.g. 'Foreign-Worker' add_redundant : If True, evaluate each candidate rule and reject those which don't provide any recourse for the affected inputs (speeds up optimisation) apriori_threshold : The support threshold used by the apriori algorithm (probability of an itemset, lower values thus return more possible rules) constraints : As defined by the paper e1 = total number of rules e2 = maximum rule width (number of conditions) e3 = total number of unique subgroup descriptors (outer if-then clauses) lams : hyperparameters for objective function (list of size 4 for AReS, size 2 for our objective) feature_costs : optional vector for defined feature costs (otherwise, we use l1 norm) ordinal_features : List of categorical features that require ordinal costs when moving between categories (typically continuous features which have been one-hot encoded before model training) original_objective : If True, use the original AReS objective function (otherwise just optimise correctness and cost) n_bins : number of (equal) bins for continuous variables normalise : If True, normalise the inputs prior to the self.model.predict() call then_generation : Apriori threshold value. In progress. If not None, then generate the "then" condition using apriori on a set filtered according to each if-if condition (search for candidate rules in SDxRL). May find more relevant rules. If None, search for candidate rules in SDxRLxRL. There's also the possibility to set "then" to RL, and divide SD appropriately to match RL. As well as (alternatively) the possibility to allow "then" to not match the "inner-if" entirely.
Source code in counterfactuals/cf_methods/global_methods/ares/ares.py
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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 | |
bin_continuous_features
¶
Method for binning continuous features. Also computes self.bin_mids and self.bin_mids_tree (dictionary and dictionary of dictionaries respectively) which store mid point values for each bin range
Input: original data Outputs: data with continuous features binned (default is 10 equally sized bins) list of all feature values, with binned feature values included list of only binned feature values
Source code in counterfactuals/cf_methods/global_methods/ares/ares.py
generate_itemsets
¶
The feature value of the subgroup of interest
e.g. 'Foreign-Worker = A201' (see dataset_loader naming) If None, SD and RL are set to the same set generated by apriori
Source code in counterfactuals/cf_methods/global_methods/ares/ares.py
generate_groundset
¶
Compute candidate set of rules for self.optimise(). Determines if rules are valid and also applies maxwidth constraint. User sets self.add_redundant to False (init method) if we ignore any rules that do not provide any successful recourse (slower, but completely irrelevant rules are not added). Size of candidate rules, V, seems to be the bottleneck in the submodular maximisation.
SD and RL: outer and inner if conditions (as per paper)
SD_lengths and RL_lengths: widths of each SD/RL element feature_values_tree: as described in self.encode_feature_values then_gen UPDATE
Output: candidate set of rules after applying constraints
Source code in counterfactuals/cf_methods/global_methods/ares/ares.py
optimise_groundset
¶
optimise_groundset(lams, factor=1, print_updates=False, print_terms=False, save_copy=False, plot_accuracy=True)
Submodular maximisation. We make 2 major modifications: 1. Don't repeat procedure k times, where k is the number of constraints. This rarely increased performance yet increases computation time k-fold (mostly pointless despite formal guarantees) 2. Don't permit up to k elements to be exchanged (computationally infeasible- to this day I am clueless regarding how this is done efficiently). In this case, you might have 20 choose 2 = 190 options for elements to exchange (instead of just 20) which is just not a worthwhile trade-off.
Output: Final two level recourse set, S
Source code in counterfactuals/cf_methods/global_methods/ares/ares.py
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 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 | |
constraints
¶
Computes if constraints (e1: total number of rules, e3: total number of unique sub-descriptors) are violated
Input: Two Level Recourse Set, Si Output: boolean (True if constraints are not violated)
Source code in counterfactuals/cf_methods/global_methods/ares/ares.py
bin_X_test
¶
Combine with first class method?