Explainable Artificial Intelligence (XAI) is increasingly essential as AI systems are deployed in critical fields such as healthcare and finance, offering transparency into AI-driven decisions. Two major XAI paradigms, counterfactual explanations (CFX) and feature attribution (FA), serve distinct roles in model interpretability. This study introduces GradCFA, a hybrid framework combining CFX and FA to improve interpretability by explicitly optimizing feasibility, plausibility, and diversity - key qualities often unbalanced in existing methods. Unlike most CFX research focused on binary classification, GradCFA extends to multi-class scenarios, supporting a wider range of applications. We evaluate GradCFA's validity, proximity, sparsity, plausibility, and diversity against state-of-the-art methods, including Wachter, DiCE, CARE for CFX, and SHAP for FA. Results show GradCFA effectively generates feasible, plausible, and diverse counterfactuals while offering valuable FA insights. By identifying influential features and validating their impact, GradCFA advances AI interpretability. The code for implementation of this work can be found at: https://github.com/jacob-ws/GradCFs .
Figures & tables
Fig. 1 : Overview of the GradCFA Pipeline: Beginning with a trained neural network, a query instance, and the observed dataset. The counterfactual set X′ is initialized with random values sampled from a normal distribution. Individual loss components are calculated and compared with user-defined thresholds τchar , where each loss component L has its own threshold τchar . For proximity, sparsity, and plausibility L within τ is true if L≤τchar , and for diversity L is considered within τ if L≥τchar . If thresholds are not met, a penalty is applied. The total loss is computed based on user-defined weighting. User constraints are then applied, gradients are computed to attribute feature importance, and update X′ . If the final loss after optimization does not meet the threshold, values are perturbed, and optimization is repeated until an acceptable loss is achieved or the maximum number of perturbation attempts is reached, after which the counterfactual set and FAs are returned.
Dataset
Training
Validation
Testing
Credit Approval
0.88
0.86
0.82
Obesity
0.90
0.84
0.85
Fetal Health
0.92
0.93
0.91
TABLE I : Classification accuracy of the predictive model on each dataset across training, validation and testing.
Ablation Study Combinations
GradCFA
Previous Work
Multi-Class
GradCFA
1
2
3
4
5
6
7
W/o Pert.
W/o Pen.
Wachter
DiCE
CARE
0
1
2
3
4
5
6
Credit Approval Dataset for Binary Classification
Prox.
0.15
0.14
0.12
0.19
0.16
0.16
0.13
0.23
0.23
0.21
0.03
0.12
0.18
-
-
-
-
-
-
-
Spars
0.18
0.19
0.16
0.21
0.20
0.20
0.17
0.25
0.27
0.24
0.10
0.28
0.56
-
-
-
-
-
-
-
Plaus
0.37
0.68
0.53
0.61
0.46
0.45
0.54
0.42
0.51
0.45
0.56
0.51
0.47
-
-
-
-
-
-
-
Div
0.96
0.86
0.72
0.82
0.92
0.80
0.88
0.94
0.91
0.92
0.35
0.84
0.68
-
-
-
-
-
-
-
TABLE II : Proximity, Sparsity, Plausibility, Diversity and Confidence of GradCFA in Comparison with the results obtained in the experiments conducted in subsections IV A, B, C, E, and F. Proximity, Sparsity and Plausibilty are minimized, while Diversity and Validity are maximized. An overall average of these metrics is given for each experiment.
Fig. 2 : Loss curves for (a) credit approval, (b) obesity levels, and (c) fetal health, with perturbation, where the grey dashed lines represent the points of perturbation. The curves demonstrate that after each perturbation the loss is driven further down, indicating the efficacy of the approach in improving the results.
Fig. 3 : Loss curves for (a) credit approval, (b) obesity levels, and (c) fetal health without perturbation.
Feature
Query
Counterfactual Values
Age
34.14
28.55
33.44
35.34
37.26
46.75
Debt
2.75
4.2
3.9
3.4
5.1
8.0
YE
2.5
3.28
3.93
2.94
3.54
2.5
Income
200.01
199.00
200.00
205.00
105.00
195.00
Gender
1
1
1
1
0
0
MS
1
1
1
1
1
1
TABLE III : Example Counterfactual Set for Credit Approval.
Feature
Query
Counterfactual Values
Age
19.0
20.0
19.0
21.0
23.0
22.0
Height
1.75
1.68
1.72
1.65
1.66
1.65
Weight
100.0
53.4
51.9
62.3
51.6
61.8
FVFC
2.0
1.9
1.9
1.9
2.0
2.0
NCP
3.0
3.0
3.0
2.9
3.0
3.0
CH2O
2.0
1.9
2.0
2.0
2.0
2.0
TABLE IV : Example Counterfactual Set for Obesity Levels.
Feature
Query
Counterfactual Values
LBE
125.0
128.9
148.0
128.3
141.5
142.2
LB
125.0
132.8
124.9
124.8
132.1
118.2
AC
10.0
5.1
2.8
0.1
5.5
2.2
FM
-4.6e-11
-3e-11
-7.4e-11
-1.1e-11
1.5e-11
1.2e-11
UC
2.3e-10
3e-10
-1.2e-10
4.3e-10
-4e-10
-2e-10
ASTV
41.0
56.9
55.1
52.6
44.2
41.0
TABLE V : Example Counterfactual Set for Fetal Health.
Fig. 4 : FA computed with GradCFA for (a) credit approval, (b) obesity levels and (c) fetal health. The results highlight the critical role of prior default and income in credit approval, weight in obesity classification, and active vigilance and calm sleep in fetal health, while other features like age, years employed, dietary habits, and histogram statistics are less influential in generating plausible and feasible counterfactuals.
Fig. 5 : FA computed with SHAP for (a) credit approval, (b) obesity levels and (c) fetal health.
Feature
Val.
Prox.
Spars.
Plaus.
Div.
Fixed
PriorDefault
0.37
-
-
-
-
Income
0.54
0.26
0.24
0.87
0.82
Industry
0.62
0.25
0.22
0.61
0.86
CreditScore
0.65
0.24
0.24
0.50
0.88
BankCustomer
0.67
0.23
0.23
0.46
0.88
TABLE VI : Analysis of the FA with the Credit Approval Data.
Feature
Val.
Prox.
Spars.
Plaus.
Div.
Fixed
Weight
0.18
-
-
-
-
MTRANS
0.52
0.33
0.42
0.62
0.71
SMOKE
0.57
0.29
0.37
0.67
0.75
CALC
0.59
0.28
0.35
0.61
0.76
CAEC
0.61
0.28
0.30
0.55
0.78
TABLE VII : Analysis of the FA with the Obesity Levels Data.
Feature
Val.
Prox.
Spars.
Plaus.
Div.
Fixed
D
0.24
-
-
-
-
A
0.45
-
-
-
-
AD
0.51
0.48
0.49
0.77
0.79
B
0.53
0.47
0.47
0.71
0.77
C
0.56
0.47
0.46
0.69
0.80
TABLE VIII : Analysis of the FA with the Fetal Health Data.
Counterfactual explanations show how changing an input could alter a model’s prediction, supporting model understanding, auditing, and actionable recourse.
In the field of Explainable AI (XAI), counterfactual (CF) explanations interpret a model's decision by suggesting the changes to the input that would lead to a more favourable outcome. To be useful in practice, such an explanation should change few features and change them as little as possible, properties known as sparsity and proximity. We observe that existing methods remain limited in this respect, especially for numerical features, whether they are model-agnostic and amortised, or gradient-based with full access to the model. In this paper, we propose FlowCF, a model-agnostic generative method that frames CF generation as sparse transport from the factual to the target class. We solve this transport with flow matching, which we extend to mixed feature types with a novel mixed flow operator, and exploit the resulting geometry to optimise for sparsity through a gating network that minimises the number of features the transport changes. Extensive experiments on six benchmark datasets demonstrate that FlowCF produces the best numerical sparsity and proximity, changing 29% of the numerical features where the best baseline changes 89%, at 70% smaller displacement, while remaining comparable on the other desiderata.
Emmanouil Panagiotou, Eirini Ntoutsi
Freie Universität Berlin · University of the Bundeswehr Munich
Counterfactual explanation (CE) is an important domain within post-hoc explainability. However, the explanations generated by most CE generators are often highly redundant. This work introduces an open-source Python library xai-cola, which provides an end-to-end pipeline for sparsifying CEs produced by arbitrary generators, reducing superfluous feature changes while preserving their validity. It offers a documented API that takes as input raw tabular data in pandas DataFrame form, a preprocessing object (for standardization and encoding), and a trained scikit-learn or PyTorch model. On this basis, users can either employ the built-in or externally imported CE generators. The library also implements several sparsification policies and includes visualization routines for analysing and comparing sparsified counterfactuals. xai-cola is released under the MIT license and can be installed from PyPI. Empirical experiments indicate that xai-cola produces sparser counterfactuals across several CE generators, reducing the number of modified features by up to 50% in our setting. The source code is available at https://github.com/understanding-ml/COLA.
Lin Zhu, Lei You
Technical University of Denmark, Copenhagen, Denmark
Counterfactual (CF) explanations identify changes that alter an input's classification. While existing methods produce realistic and low-cost CFs, they often fail to ensure feasibility, by suggesting non-constructive modifications or incompatible with future changes (e.g., changing an individual's race to secure a job offer). We introduce a refinement of CF explanations that explicitly enforces feasibility. Our approach is the first to efficiently generate CFs that are realistic, low-cost and feasible. We accommodate both hard feasible constraints, specified by domain knowledge users, and soft feasible constraints, inferred automatically via causal inference from the dataset. Our method, Feasible Counterfactual Explanations (FCx), is based on a modified Variational Autoencoder (VAE) optimized with a multi-factor loss function. We measure the cost of a change based on the absolute change in values (proximity) as well as the number of features changed (sparsity) while realism is measured based on the LOF for density estimation, guaranteeing that CFs reside in densely populated regions. Extensive experiments on four public datasets show that our approach matches state-of-the-art performance across multiple metrics while guaranteeing feasibility.
Kleopatra Markou, Vana Kalogeraki, Dimitrios Gunopulos
National and Kapodistrian University of Athens, Department of Informatics and Telecommunications, Greece · Athens University of Economics and Business, Department of Informatics, Greece