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:
- 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?
- 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.
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:
- 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.
- 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.
- 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