Counterfactual explanations show what needs to change in a machine learning model's input to produce a different prediction. If an AI model rejects a loan application, a counterfactual explanation might show that requesting a smaller amount would change its prediction to approval. Instead of listing the features behind a decision, it identifies an alternative set of inputs that produces a desired result.
What makes counterfactual explanations valuable in Explainable AI is their ability to turn a difficult prediction into a concrete comparison. I find it more useful to examine what separates the original input from a successful alternative than to simply rank the model's most influential features. The challenge is ensuring that the proposed changes are realistic, meaningful, and genuinely connected to the model's decision.
Counterfactual Explanation Example
Consider a loan approval model that evaluates an applicant using income, existing debt, requested loan amount, and payment history.
An applicant submits the following information:

All four alternatives produce the desired prediction in this hypothetical example. However, they do not offer equally useful explanations.
Reducing the requested loan amount is a change the applicant can make directly. Lowering existing debt payments requires a financial change, while increasing income takes additional effort and cannot be assumed achievable.
The final alternative exposes a more serious limitation. Changing an accurate historical record is not a legitimate action, even if the altered input causes the model to approve the application.
This distinction is essential.
A counterfactual can be mathematically valid without being practically actionable.
The explanation also describes what the model would predict under the modified inputs. It does not guarantee that a bank would approve a future application or that changing one factor would cause approval in the real world.
How Counterfactual Explanations Work
Counterfactual explanations investigate a model by searching for alternative inputs that produce a specified prediction.
The original model acts as the reference throughout the process.
Selecting the Original Prediction
The process begins with an individual observation and its model output.
For the loan example, the observation contains the applicant's financial information, and the original output is a rejection.
The explanation must preserve this starting point because every proposed change is measured relative to it.
Defining the Desired Outcome
Next, the user specifies the result they want to investigate.
For a classification model, this could mean changing the predicted class from rejected to approved.
For a model producing a numerical prediction, the goal can be reaching a specified value or range.
I would define this target before generating alternatives. Without a clear target, the search can produce different predictions that do not answer the user's actual question.
Generating Alternative Inputs
The algorithm searches for modified versions of the original observation.
It changes selected feature values, submits the modified inputs to the prediction model, and checks the resulting outputs.
Different algorithms use different search strategies, including optimization, random sampling, and searches through existing data.
The search should respect the features that are permitted to change.
For a loan application, adjusting the requested amount is meaningful. Changing a person's historical identity or inventing a different financial record is not.
Selecting Meaningful Counterfactuals
The algorithm identifies alternatives that achieve the desired prediction while satisfying its configured objectives and constraints.
A useful counterfactual should require limited changes, remain plausible, and respect the application's rules.
For example, reducing the requested loan from $12,000 to $9,000 provides a simpler explanation than changing income, employment history, existing debt, and several unrelated features simultaneously.
However, the alternative with the fewest changes is not automatically the easiest to achieve.
That is why the search objective and practical constraints matter as much as the resulting prediction.
Generating Counterfactual Explanations
There are several approaches to generating counterfactual explanations in machine learning.
The appropriate method depends on the prediction model, available data, and constraints imposed on the generated alternatives.
Optimization Based Methods
Optimization methods search for input changes that move the model's prediction toward a desired result.
The search balances objectives such as achieving the target prediction and keeping the modified observation close to the original.
The approach introduced by Wachter, Mittelstadt, and Russell is an important foundation for counterfactual explanation research.
Its central idea is to identify a modified input that achieves a desired outcome while limiting the distance from the original observation.
I would use an optimization approach when the application provides a clear prediction target and meaningful rules for permissible changes.
The optimization objective must be defined carefully. Without adequate constraints, an algorithm can produce a technically successful alternative that makes little sense in practice.
Search and Sampling Methods
Search based methods generate candidate inputs and evaluate them using the original model.
Randomized search explores changes within a defined space, while genetic search iteratively develops candidate solutions based on a fitness objective.
Another approach searches for suitable observations within an existing dataset.
For example, an algorithm could look for approved applications that resemble a rejected applicant's profile.
That can produce realistic reference points, but similarity to an existing person does not automatically establish that the applicant can achieve the same circumstances.
I would treat dataset based examples as comparisons rather than direct instructions.
Diverse Counterfactual Generation
A single successful alternative does not provide every useful explanation.
Consider an applicant who cannot increase their income but can reduce the requested loan amount.
Another applicant might have the opposite options.
Generating several counterfactuals allows the user to compare different routes toward the desired prediction.
This is the principle behind Diverse Counterfactual Explanations, commonly associated with the DiCE framework.
The goal is not simply to generate several nearly identical records. The alternatives should offer meaningfully different combinations of changes.
Actionable Counterfactuals and Algorithmic Recourse
Counterfactual explanations and actionable recourse are closely related, but they are not identical.
A counterfactual explanation identifies an alternative input that produces a different model prediction.
Algorithmic recourse examines the actions an individual could take to move toward a desired outcome.
The difference becomes important when the input features are connected to real circumstances.
Actionable Features
Actionable features are variables that a person or organization can change through a feasible intervention.
For the loan example, the requested loan amount is directly adjustable.
Existing debt obligations can also change, but reducing them requires an actual financial action.
A counterfactual should reflect the effort and conditions involved rather than presenting every numerical adjustment as equally accessible.
Immutable and Historical Features
Some features cannot be changed through a legitimate present action.
Birth history, past events, and accurate historical records are examples.
A useful counterfactual generation process should prevent changes to features that cannot legitimately be altered.
I would specify those restrictions before searching for alternative predictions rather than remove unrealistic outputs afterward.
Relationships Between Features
Even when individual values are valid, their combinations can violate real relationships.
For example, increasing a person's monthly income does not automatically change their documented employment history.
A generated alternative that modifies one value without accounting for connected variables can represent an unrealistic individual.
This is where domain knowledge becomes essential.
I would review whether the proposed changes respect the relationships between features and whether the intended actions could produce those changes in the real world.
A counterfactual that satisfies the prediction model is not necessarily evidence of a causal relationship.
Evaluating Counterfactual Explanations
A good explanation must do more than change the model's output.
I use five criteria to evaluate whether a generated counterfactual is worth presenting to a user.
Validity
Validity establishes whether the alternative input achieves the required prediction.
For the loan example, a valid counterfactual must produce the desired approval prediction when evaluated by the original model.
If the model still predicts rejection, the alternative has not achieved the specified target.
Proximity
Proximity measures the difference between the original observation and the generated alternative.
For numerical features, distance calculations should account for different units and scales.
A $1,000 change in income cannot be compared directly with a one year change in employment duration without a meaningful normalization or weighting method.
I would choose feature distances that reflect the application rather than rely solely on raw numerical differences.
Sparsity
Sparsity concerns the number of features changed.
An explanation that adjusts one or two understandable variables is easier to interpret than an alternative modifying nearly every input.
For an applicant, reducing the requested loan amount is more straightforward than simultaneously changing income, employment duration, repayment history, and debt.
Sparsity improves readability, but it must be balanced against feasibility.
Diversity
Diversity measures whether multiple counterfactuals provide genuinely different alternatives.
A set containing five nearly identical loan amounts offers limited additional value.
A more useful set could include separate alternatives involving the requested amount, debt obligations, or income.
Different users can then assess which options are relevant to their circumstances.
Plausibility and Feasibility
Plausibility concerns whether the generated record represents a realistic combination of feature values.
Feasibility concerns whether the proposed changes are permitted and achievable under the relevant constraints.
A counterfactual can be close to the original record and satisfy the model's prediction target while failing both requirements.
For decisions affecting individuals, I would prioritize these checks before presenting any alternative as practical guidance.
Counterfactual Explanations with DiCE
DiCE is a Python library developed to generate diverse counterfactual explanations for machine learning models.
It supports different search methods and allows developers to control which features can change and which values are permitted.
For a practical implementation, I would begin with a trained classification model and a representative input that receives an unfavorable prediction.
Preparing the Data and Model
DiCE needs an interface to the prediction model and information describing the input features.
Its standard workflow uses a dataset interface and a model interface.
For a structured loan application, the dataset description should identify numerical and categorical features correctly.
The model interface must also preserve the preprocessing used during training.
This matters because a generated alternative should be evaluated by the same prediction pipeline as the original application.
Defining Generation Constraints
Before generating counterfactuals, configure the desired prediction and the features permitted to change.
DiCE provides the features_to_vary parameter for selecting changeable features.
The permitted_range parameter restricts supported feature values to specified ranges or categories.
For the loan example, I would allow changes to requested loan amount and selected financial variables while excluding historical information that cannot legitimately be modified.
These controls restrict the search space, but they do not independently establish that every resulting suggestion is practical for a real person.
Generating and Inspecting Alternatives
The explainer can generate multiple candidate records that target the desired prediction.
For each result, I would compare the generated values against the original application and verify the prediction using the trained model.
I would then inspect whether the changes satisfy the business constraints and offer distinct options.
If the results include only unrealistic changes, I would revise the permitted features and ranges rather than present those outputs as useful explanations.
The practical strength of DiCE is its support for multiple alternatives and configurable search constraints.
Its output still requires independent evaluation before it is used in an application that affects real decisions.
Counterfactual Explanations vs SHAP and LIME
Counterfactual explanations, SHAP, and LIME all help investigate machine learning predictions, but they answer different questions.
SHAP attributes a model prediction to input features relative to a defined reference. It helps explain how different features contribute to a result.
LIME approximates the model's behavior around an individual observation using an interpretable local model.
Counterfactual explanations identify alternative inputs that produce a desired prediction.
Consider the rejected loan application.
SHAP can help explain how the applicant's financial features contribute to the prediction.
LIME can show how those features influence a local approximation of the model.
A counterfactual explanation can show that the model would predict approval for a smaller requested loan amount.
I would use counterfactual analysis when the main objective is to investigate possible changes in a prediction.
Feature attribution methods remain useful when the objective is understanding how the model responds to individual features.
The approaches complement one another, but none establishes that its explanation represents the true causal process behind a real decision.
Applications of Counterfactual Explanations
Counterfactual explanations are relevant to applications where understanding alternative model outcomes has practical value.
Credit and Lending Decisions
Lending models can be examined to identify alternative applicant profiles that receive different predictions.
Counterfactuals also help developers investigate whether a decision depends on factors that should not influence the model.
Any applicant facing guidance must be based on legitimate, feasible changes and reviewed within the institution's applicable decision and compliance processes.
Healthcare Prediction Models
In healthcare research, counterfactual analysis can investigate how a predictive model responds to changes in patient measurements.
For example, researchers can examine how different input values affect a model's estimated risk category.
These model level comparisons are not medical treatment recommendations.
Clinical relationships, biological constraints, and independent medical validation remain necessary.
Recruitment and Employment Models
Counterfactual explanations can help evaluate which applicant attributes influence a screening model's output.
They can also reveal problematic dependencies when changes to inappropriate characteristics alter predictions.
For this application, I would focus on model auditing rather than automatically presenting every generated alternative as advice to applicants.
Customer Retention Models
A business using a customer churn model can investigate which changes in customer behavior or service characteristics produce a lower predicted churn risk.
For example, counterfactual analysis can identify alternative customer profiles associated with different predictions.
Those findings can guide further experiments, but they do not prove that changing a specific service feature will cause a customer to remain.
Limitations of Counterfactual Explanations
Counterfactual methods explain the behavior of a prediction model, not the full decision process operating in the real world.
This distinction creates several limitations.
No guaranteed solution: A search algorithm can fail to find a valid counterfactual under the available constraints. That does not prove that no valid alternative exists.
Dependence on model quality: A counterfactual can accurately represent the behavior of a model that is itself inaccurate or biased.
Sensitivity to generation settings: Different search objectives, constraints, or initialization choices can produce different alternatives for the same observation.
Computational demands: Searching for valid alternatives can require many evaluations of the original model, particularly when the input space is complex.
I would also distinguish between a prediction reaching a model threshold and a real decision being guaranteed.
If the model predicts approval after a feature change, that only establishes the behavior of the evaluated model under the modified input. Real decisions can depend on additional policies, updated information, and circumstances outside the model.
Counterfactual explanations are most useful when their scope is clear and their outputs are evaluated against practical constraints.
Conclusion
Counterfactual explanations provide a direct way to investigate alternative machine learning predictions.
Instead of stopping at a description of influential features, they identify changes that cause the evaluated model to produce a different result.
Their usefulness depends on more than reaching the target prediction. The changes must also be understandable, realistic, and consistent with the conditions of the application.
I would prioritize a small set of valid, feasible, and distinct alternatives over a large collection of technically successful but impractical examples. The strongest explanation is one that clarifies the model's behavior without confusing a hypothetical prediction with a guaranteed real outcome.
FAQ
Can I generate counterfactuals without having the training dataset?
Yes. Certain methods can generate counterfactuals using access to the trained model and information about its expected input features. However, the absence of representative training data makes it harder to assess whether generated alternatives resemble realistic observations.
Can counterfactual explanations work with regression models too?
Yes. For regression, the desired result is defined as a numerical target or acceptable range rather than a different class. For example, a counterfactual can identify input changes associated with a predicted property value reaching a specified threshold.
Can I use counterfactual explanations for images or text?
Yes. Specialized methods generate modified text or image inputs that change a model's prediction. Such explanations require additional checks to ensure that changes preserve meaningful language, valid image content, and the intended semantic relationships.
Why am I getting different counterfactuals every time I run the tool?
Different results can arise from randomized initialization, sampling, optimization procedures, and the existence of several valid solutions. Fixing random seeds and preserving the generator configuration improves reproducibility. Multiple valid explanations should still be compared for quality and feasibility.
Can I save counterfactual explanations for an audit?
Yes. An audit record can include the original input, prediction, generated alternatives, model version, generation method, constraints, and evaluation results. Preserving these details supports traceability, but the explanation alone does not establish model fairness, regulatory compliance, or the correctness of the original decision.
Seen first.



