CounterfactualExplainer<T>

Models & Types · AiDotNet.Interpretability.Explainers

Model-agnostic Counterfactual explainer that finds minimal changes needed for a different prediction.

Implements: ILocalExplainer<T, CounterfactualExplanation<T>>

For Beginners

Counterfactual explanations answer the question: “What would need to change for the model to give a different prediction?”

Example: If a loan application was denied, a counterfactual might say: “If your income was $5,000 higher, the loan would have been approved.”

Key features:

  • Shows the MINIMUM changes needed (fewest features changed)
  • Changes should be realistic and actionable
  • Helps users understand what they can do to get a different outcome

This is especially useful for:

  • Credit decisions (what to improve for approval)
  • Medical diagnoses (what tests would change the diagnosis)
  • Any scenario where users want to know “what if”

Constructors

Constructor Summary
CounterfactualExplainer(Func<Matrix<T>, Vector<T>> predictFunction, int numFeatures, int maxIterations, double stepSize, int maxChanges, double targetThreshold, string[] featureNames, T[] featureMins, T[] featureMaxs, bool[] featuresMutable, int? randomState) Initializes a new Counterfactual explainer.

Properties

Property Summary
bool SupportsGlobalExplanations { get; }
bool SupportsLocalExplanations { get; }
string MethodName { get; }

Methods

Method Summary
CounterfactualExplanation<T> Explain(Vector<T> instance) Finds a counterfactual explanation for the given instance.
CounterfactualExplanation<T> Explain(Vector<T> instance, Vector<T> targetPrediction) Finds a counterfactual explanation to achieve a specific target prediction.
CounterfactualExplanation<T>[] ExplainBatch(Matrix<T> instances)