Algorithm 1.
Counterfactual Generation
| Input: query_instance_set , prototype_set | |
| Parameter: num_cfs , variables_not_vary | |
| Output: The set of counterfactual examples | |
| 1: | Initialize empty list lst_CF |
| 2: | Initialize num cfs |
| 3: | prototypes select ← [.class ≠ query.class] |
| 4: | for each in do |
| 5: | ← concat |
| 6: | k_dTree ← Build_k_d_tree |
| 7: | num_neighbors ← num_cfs |
| 8: | while ≤ len do |
| 9: | ← k_dTree.search |
| 10: | .indices = constraints_check |
| 11: | if len(.indices) < num_cfs then |
| 12: | |
| 13: | else |
| 14: | lst_CF ← concat[lst_CF, NN.indices] |
| 15: | break |
| 16: | end if |
| 17: | end while |
| 18: | Can not find enough counterfactuals |
| 19: | end for |
| 20: | Create counterfactuals using indices from lst_CF |