IP Library Granted Patent US 12688441
Granted Patent B2
US 12688441 · App. 17/162,931 · Granted Jul 21, 2026

Systems and methods for counterfactual explanation in machine learning models

Inventors: Wenzhuo Yang (Singapore, SG); Jia Li (Mountain View, CA); Chu Hong Hoi (Singapore, SG); Caiming Xiong (Menlo Park, CA)
Assignee: Salesforce, Inc.
G06N5/045G06F18/2113G06F18/217G06F18/24147G06F18/24323G06N5/01G06N7/01G06N20/00
View Patent ↗
Loading inventors, assignments & file history…
Monitor This Case
Get email alerts when status or documents change.
Order Certified Copies
Most orders are placed with the USPTO same day — all within 24 business hours.
Order via The Patent Place →
Pre-filled with this patent's details
Quick Facts
Patent No.
US 12688441
App. No.
17/162,931
Granted
Jul 21, 2026
Kind
B2
Abstract

Embodiments described herein provide a two-stage model-agnostic approach for generating counterfactual explanation via counterfactual feature selection and counterfactual feature optimization. Given a query instance, counterfactual feature selection picks a subset of feature columns and values that can potentially change the prediction and then counterfactual feature optimization determines the best feature value for the selected feature as a counterfactual example.

Claims (70)

1 . A method for generating a counterfactual example for counterfactual explanation in a machine learning model, the method comprising:

receiving a query instance including a plurality of feature columns and corresponding feature values;

generating, by a machine learning model, a predicted label in response to the query instance;

determining, for each feature column of the plurality of feature columns and via a nearest neighbor search, a discrete number of candidate feature values, each of the discrete number of candidate feature values corresponding to a different label than the predicted label;

combining respective candidate feature values from each of the plurality of feature columns to generate a set of counterfactual candidates, wherein each of the set of counterfactual candidates corresponds to a different label than the predicted label;

comparing feature columns and corresponding feature values between the query instance and the set of counterfactual candidates;

determining, based on the comparing, a first frequency of feature column changes and a second frequency of feature value changes for each feature column of the plurality of feature columns

selecting, based on the first frequency, a subset of feature columns;

selecting, based on the second frequency a subset of feature values for each feature column of the subset of feature columns;

selecting an optimized counterfactual candidate as the counterfactual example based on the subset of feature columns and the subset of feature values; and

outputting, to a user a counterfactual explanation accompanying the predicted label, wherein the counterfactual explanation is based on the counterfactual example.

2 . The method of claim 1 , wherein the set of counterfactual candidates is identified by:

building a search index for the different label based on training query instances that have been annotated with the different label; and

finding the set of counterfactual candidates via the search index using a second nearest neighbor search tree.

3 . The method of claim 1 , wherein the counterfactual explanation is generated by performing a counterfactual feature optimization to construct the counterfactual example based on the subset of feature columns and the subset of feature values.

4 . The method of claim 3 , wherein the counterfactual feature optimization includes determining an optimal policy that maximizes an objective comprising an expected feedback reward caused by a modified query instance.

5 . A method for generating a counterfactual example for counterfactual explanation in a machine learning model, the method comprising:

receiving a query instance including a plurality of feature columns and corresponding feature values;

generating, by a machine learning model, a predicted label in response to the query instance;

identifying, based on a recommendation model in response to the query instance, a subset of feature columns wherein each of the subset of feature columns include candidate feature values;

generating, a set of counterfactual candidates based on the subset of feature columns and the candidate feature values, wherein each of set of counterfactual candidates corresponds to a different label than the predicted label;

generating, by the recommendation model and for each feature column of the subset of feature columns, a first probability that changing a feature column would change the prediction label;

generating, by the recommendation model and for each of the candidate feature values, a second probability that changing a feature value would change the prediction label;

selecting, based on the first probability a subset of feature columns from the set of counterfactual candidates, the first probability of any of the subset of feature columns being greater than the first probability of any of remaining feature column(s) that are not selected;

selecting, based on the second probability and for each feature column of the selected subset of feature columns, a subset of feature values, the second probability of any of the subset of feature values being greater than the second probability of any of remaining feature value(s), that are not selected;

selecting an optimized counterfactual candidate as the counterfactual example from the set of counterfactual candidates based on the selected subset of feature columns and the selected subset of feature values; and

outputting, to a user a counterfactual explanation accompanying the predicted label, wherein the counterfactual explanation is based on the counterfactual example.

6 . The method of claim 5 , further comprising: training the recommendation model using a dataset built based on the machine learning model and a training dataset of query instances and respective labels.

7 . The method of claim 6 , wherein the dataset is built by finding a number of neighbors of a training query instance in the training dataset using a nearest neighbor search tree.

8 . The method of claim 5 , wherein the counterfactual explanation is generated by performing a counterfactual feature optimization to construct the counterfactual example based on the subset of feature columns and the subset of feature values.

9 . A system for generating a counterfactual example for counterfactual explanation in a machine learning model, the system comprising:

a memory that stores the machine learning model;

a data interface that receives a query instance including a plurality of feature columns and corresponding feature values; and

a processor that reads instructions from the memory to perform:

generating, by the machine learning model, a predicted label in response to the query instance;

determining, for each feature column of the plurality of feature columns and via a nearest neighbor search, a discrete number of candidate feature values, each of the discrete number of candidate feature values corresponding to a different label than the predicted label;

combining respective candidate feature values from each of the plurality of feature columns to generate a set of counterfactual candidates, wherein each of the set of counterfactual candidates corresponds to a different label than the predicted label;

comparing feature columns and corresponding feature values between the query instance and the set of counterfactual candidates;

determining, based on the comparing, a first frequency of feature column changes and a second frequency of feature value changes for each feature column of the plurality of feature columns

selecting, based on the first frequency, a subset of feature columns;

selecting, based on the second frequency a subset of feature values for each feature column of the subset of feature columns;

selecting an optimized counterfactual candidate as the counterfactual example based on the subset of feature columns and the subset of feature values; and

outputting, to a user a counterfactual explanation accompanying the predicted label, wherein the counterfactual explanation is based on the counterfactual example.

10 . The system of claim 9 , wherein the set of counterfactual candidates is identified by:

building a search index for the different label based on training query instances that have been annotated with the different label; and

finding the set of counterfactual candidates via the search index using a second nearest neighbor search tree.

11 . The system of claim 9 , wherein the counterfactual explanation is generated by performing a counterfactual feature optimization to construct the counterfactual example based on the subset of feature columns and the subset of feature values.

12 . The system of claim 11 , wherein the counterfactual feature optimization includes determining an optimal policy that maximizes an objective comprising an expected feedback reward caused by a modified query instance.

13 . A system for generating a counterfactual example for counterfactual explanation in a machine learning model, the system comprising:

a memory that stores the machine learning model;

a data interface that receives a query instance including a plurality of feature columns and corresponding feature values; and

a processor that reads instructions from the memory to perform:

generating, by the machine learning model, a predicted label in response to the query instance;

identifying, based on a recommendation model in response to the query instance, a subset of feature columns wherein each of the subset of feature columns include candidate feature values;

generating, a set of counterfactual candidates based on the subset of feature columns and the candidate feature values, wherein each of set of counterfactual candidates corresponds to a different label than the predicted label;

generating, by the recommendation model and for each feature column of the subset of feature columns, a first probability that changing a feature column would change the prediction label;

generating, by the recommendation model and for each of the candidate feature values, a second probability that changing a feature value would change the prediction label;

selecting, based on the first probability a subset of feature columns from the set of counterfactual candidates, the first probability of any of the subset of feature columns being greater than the first probability of any of remaining feature column(s) that are not selected;

selecting, based on the second probability and for each feature column of the selected subset of feature columns, a subset of feature values, the second probability of any of the subset of feature values being greater than the second probability of any of remaining feature value(s), that are not selected;

selecting an optimized counterfactual candidate as the counterfactual example from the set of counterfactual candidates based on the selected subset of feature columns and the selected subset of feature values; and

outputting, to a user a counterfactual explanation accompanying the predicted label, wherein the counterfactual explanation is based on the counterfactual example.

14 . The system of claim 13 , further comprising: training the recommendation model using a dataset built based on the machine learning model and a training dataset of query instances and respective labels.

15 . The system of claim 14 , wherein the dataset is built by finding a number of neighbors of a training query instance in the training dataset using a nearest neighbor search tree.

16 . The system of claim 13 , wherein the counterfactual explanation is generated by performing a counterfactual feature optimization to construct the counterfactual example based on the subset of feature columns and the subset of feature values.

17 . The method of claim 1 , further comprising:

sorting the first frequencies in descending order, wherein the first frequency of the subset of feature columns correspond to a top first number of the sorted first frequency; and

sorting the second frequencies in descending order, wherein the second frequency of the subset of feature values correspond to a top second number of the sorted second frequency.

18 . The method of claim 5 , further comprising:

sorting the first probabilities in descending order, wherein the first probability of the subset of feature columns correspond to a top first number of the sorted first probability; and

sorting the second probabilities in descending order, wherein the second probability of the subset of feature values correspond to a top second number of the sorted second probability.