Skip to main content
. Author manuscript; available in PMC: 2026 Jan 28.
Published before final editing as: Int J Geogr Inf Sci. 2025 Jan 28:10.1080/13658816.2024.2443757. doi: 10.1080/13658816.2024.2443757

Algorithm 1.

Counterfactual Generation

Input: query_instance_set (U), prototype_set (S)
Parameter: num_cfs (E), variables_not_vary (Hc)
Output: The set of counterfactual examples CF
1: Initialize empty list lst_CF
2: Initialize num cfs(E)
3: prototypes select(P)S[S.classquery(qi).class]
4: for each qi in U do
5: Tconcat [qi,P]
6: k_dTreeBuild_k_d_tree(T)
7: num_neighbors (N)num_cfs(E)
8: while Nlen(P) do
9:   NNk_dTree.search[qi,N]
10:   NN.indices = constraints_check(NN,Hc)
11:   if len(NN.indices) < num_cfs(E) then
12:    NN+1
13:   else
14:    lst_CFconcat[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