Explaining k-Nearest Neighbors: Abductive and Counterfactual Explanations

Pablo Barceló, Alexander Kozachinskiy, Miguel Romero Orth, Bernardo Subercaseaux, José Verschae

TL;DR

While $k$-Nearest Neighbors ($k$-NN) is traditionally considered "self-interpretable" because it points to real training examples, this data-driven perspective fails in high-dimensional feature spaces. This paper maps out the formal computational complexity of generating feature-based explanations for $k$-NN, revealing that tractability hinges entirely on your choice of distance metric, and demonstrates how modern SAT and optimization solvers make finding these explanations practical.

What Problem Does This Paper Address?

When explaining a $k$-NN classifier's decision, the standard approach is to show the user the $k$ training examples closest to the input vector. This is known as the "data perspective." However, if your data has hundreds or thousands of features (like word embeddings or high-resolution images), simply looking at a neighbor doesn't tell you why it's a neighbor or which features actually drove the classification.

To solve this, the authors shift to a "feature perspective," focusing on two formal types of explainability:

  1. Abductive Explanations (Minimum Sufficient Reasons): What is the smallest subset of features in our input vector that guarantees its classification, regardless of what values the other features take?
  2. Counterfactual Explanations: What is the smallest possible tweak we can make to the input vector's features to flip the classifier's decision?

The paper addresses a major gap in machine learning literature: the lack of a rigorous theoretical understanding of how hard it actually is to compute these feature-based explanations for $k$-NN models.

Figure 1: Illustration of a counterfactual explanation for an image of digit 4 in the binarized MNIST dataset, which after changing 13 pixels is classified as a 9.

Methods

The authors conduct a thorough computational complexity analysis across two distinct settings:

  • Continuous Spaces ($\mathbb{R}^n$): Evaluating performance under $\ell_1$ (Manhattan) and $\ell_2$ (Euclidean) distance metrics.
  • Discrete Spaces ($\{0, 1\}^n$): Evaluating performance under the Hamming distance.

For tractable scenarios, they leverage geometric properties. For instance, under the $\ell_2$ norm, the boundary between equidistant points forms a linear hyperplane, allowing them to reduce the explanation problem to Convex Quadratic Programming or Linear Programming.

For intractable (NP-hard) scenarios, they develop practical algorithmic pipelines using:

  • Integer Quadratic Programming (IQP): Formulated and solved using the commercial Gurobi optimizer.
  • SAT Encoding: A novel Conjunctive Normal Form (CNF) encoding that utilizes a modern SAT solver (cardinality-cadical) with native support for cardinality constraints to find optimal counterfactuals.

Datasets

The mathematical proofs are validated and evaluated using:

  1. The MNIST Dataset: Used for the continuous setting (grayscale values) and binarized for the discrete setting. Images were rescaled to experiment with different dimensional scales.
  2. Synthetic Random Data: Uniformly random vectors in $\{0, 1\}^n$ labeled via independent Bernoulli variables to rigorously benchmark solver runtimes against varying feature dimensions.

Key Insights and Ideas

  • The Metric Matters Immensely: The most striking takeaway is that for counterfactual explanations in continuous spaces, switching from the $\ell_2$ norm to the $\ell_1$ norm flips the problem from polynomial-time solvable (P) to NP-complete.
  • Geometry is Key: The tractability of the $\ell_2$ norm stems from the fact that its decision boundaries are flat hyperplanes. Under $\ell_1$ or Hamming distances, the boundaries become complex, non-linear, and combinatorial, causing the complexity to skyrocket.
Figure 2: Illustration of minimum distance counterfactual explanations over R2 in the ℓ2 metric, with 𝑘= 1. Blue (red) areas are classified negatively (positively).
  • Discrete Mirrors $\ell_1$: The discrete setting (Hamming distance) behaves almost identically to the continuous $\ell_1$ setting, demonstrating that discrete feature spaces inherit the same hardness characteristics.
  • The $k=1$ Oasis: For $k=1$ (Nearest Neighbor), finding a minimal sufficient reason remains tractable across all metrics, but as soon as $k \ge 3$, the problem becomes coNP-complete for $\ell_1$ and Hamming distances.

Key Results

The paper provides a complete landscape of the complexity of $k$-NN explanations, summarized below:

  • Continuous $\ell_2$: Counterfactual explanations and checking sufficient reasons are solvable in polynomial time ($n^{O(k)}$). Finding the minimum sufficient reason is NP-complete.
  • Continuous $\ell_1$ & Discrete Hamming: Finding a counterfactual explanation is NP-complete. Checking a sufficient reason is coNP-complete for any fixed $k \ge 3$.
  • Solvers to the Rescue: Despite these daunting theoretical lower bounds, the empirical results show that modern solvers crush these problems in practice. For datasets featuring hundreds of dimensions and thousands of training points, both the IQP and SAT formulations successfully computed exact explanations in under two minutes.

Limitations and Future Work

  • The Parameterized Complexity of $k$: The polynomial-time algorithms designed for the $\ell_2$ metric scale exponentially with respect to $k$ ($n^{O(k)}$). The authors proved that this is theoretically unavoidable (W[1]-hard), meaning these algorithms slow down significantly if $k$ is large.
  • Binary vs. Multi-label: The current framework assumes binary classification. While extending this to multi-label classification is straightforward for $k=1$, it remains an open algorithmic challenge for $k \ge 3$.
  • Approximation Algorithms: Given that finding minimum sufficient reasons is universally hard, a promising future direction is exploring whether polynomial-time approximation algorithms can find "good enough" explanations close to the true minimum.

Summary updated

Abstract

Despite the wide use of $k$-Nearest Neighbors as classification models, their explainability properties remain poorly understood from a theoretical perspective. While nearest neighbors classifiers offer interpretability from a ``data perspective'', in which the classification of an input vector $\bar{x}$ is explained by identifying the vectors $\bar{v}_1, \ldots, \bar{v}_k$ in the training set that determine the classification of $\bar{x}$, we argue that such explanations can be impractical in high-dimensional applications, where each vector has hundreds or thousands of features and it is not clear what their relative importance is. Hence, we focus on understanding nearest neighbor classifications through a ``feature perspective'', in which the goal is to identify how the values of the features in $\bar{x}$ affect its classification. Concretely, we study abductive explanations such as ``minimum sufficient reasons'', which correspond to sets of features in $\bar{x}$ that are enough to guarantee its classification, and counterfactual explanations based on the minimum distance feature changes one would have to perform in $\bar{x}$ to change its classification. We present a detailed landscape of positive and negative complexity results for counterfactual and abductive explanations, distinguishing between discrete and continuous feature spaces, and considering the impact of the choice of distance function involved. Finally, we show that despite some negative complexity results, Integer Quadratic Programming and SAT solving allow for computing explanations in practice.
BibTeX
@inproceedings{barcelo2025explaining,
  author = {Pablo Barceló and Alexander Kozachinskiy and Miguel Romero Orth and Bernardo Subercaseaux and José Verschae},
  title = {{Explaining k-Nearest Neighbors: Abductive and Counterfactual Explanations}},
  booktitle = {Proc. ACM Manag. Data},
  year = {2025},
}
Figure