Explore modules

Counterfactual Explanations

Find the minimum L1 or Euclidean L2 change to flip a linear classifier decision.

Free theory article

Find the minimum L1 or Euclidean L2 change to flip a linear classifier decision.

Concept & interpretation

A counterfactual explanation describes the smallest change to the feature values of an instance that changes the prediction to a predefined output (such as a favorable outcome). For a given negative prediction, the counterfactual finds a point on or across the decision boundary by minimizing a specific distance metric (like L1 or L2 norm), subject to some features being immutable. It illustrates model recourse; the mathematical suggestion alone does not establish a feasible real-world action.

Experimenting with the Module

Use the interactive Counterfactual Explanations module to explore how distance metrics and immutable features affect the recourse path:

  1. Set Feature x1 to 0 and Feature x2 to 0.
  2. Set Distance to L2 and Immutable to None. Observe the delta [1/5, 2/5] and the distance 0.447214.
  3. Change the Distance to L1. Notice the delta changes to [0, 1/2] (distance 0.5), as the L1 norm places the entire change on the feature with the highest coefficient magnitude (x2) to minimize the absolute sum.
  4. Set the Immutable dropdown to x2. The delta becomes [1, 0] (distance 1.0), because x2 cannot be changed and all the burden falls on x1.
  5. Set the Immutable dropdown to Both. The module correctly reports "Infeasible: both features immutable" and distance "undefined", since the point cannot reach the boundary.
  6. Set Feature x1 to 1 and Feature x2 to 0. The point now lies exactly on the boundary (score 0), resulting in a "Already positive" status and 0 distance.
  7. For an output outside the input range, set Feature x1 = -1, Feature x2 = -1 and Immutable = x2. Under either distance, only x1 can move: Δ = [4, 0], the counterfactual is [3, -1], and the minimum distance is 4. The plot expands; 3 is not clipped to the slider maximum 1.
Read the full free theory article