ContrastiveExplainer<T>

Models & Types · AiDotNet.Interpretability.Explainers

Contrastive explainer that answers “Why X and not Y?” questions.

Implements: ILocalExplainer<T, ContrastiveExplanation<T>>

For Beginners

Contrastive explanations answer a specific type of question: “Why did the model predict X instead of Y?”

This is how humans naturally ask for explanations:

  • “Why was my loan denied (instead of approved)?”
  • “Why is this classified as cat (and not dog)?”
  • “Why is this patient high risk (rather than low risk)?”

Key concepts:

  1. Fact: What actually happened (the model’s prediction)
  2. Foil: What you’re comparing against (the alternative outcome)
  3. Pertinent Positives: Features that support the fact
  4. Pertinent Negatives: Features that, if changed, would lead to the foil

Why contrastive explanations are useful:

  • Match how humans think about explanations
  • Focus on what matters for the specific comparison
  • Actionable: “Change these features to get the alternative outcome”

Example: “Loan denied instead of approved”

  • Pertinent Positive: “Credit score is 580 (below 620 threshold)”
  • Pertinent Negative: “If income were $10K higher, loan would be approved”

Constructors

Constructor Summary
ContrastiveExplainer(Func<Matrix<T>, Vector<T>> predictFunction, int numFeatures, string[] featureNames, string[] classNames, T[] featureMins, T[] featureMaxs, double perturbationStep) Initializes a new Contrastive explainer.

Properties

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

Methods

Method Summary
ContrastiveExplanation<T> Explain(Vector<T> instance) Explains why the model predicted one class instead of another.
ContrastiveExplanation<T> Explain(Vector<T> instance, int factClass, int foilClass) Explains why the model predicted factClass instead of foilClass.
ContrastiveExplanation<T>[] ExplainBatch(Matrix<T> instances)