Optimizing Data Shapley Interaction Calculation from O(2^n) to O(t n^2) for KNN models

Mohamed Karim Belaid, Dorra El Mekki, Maximilian Rabus, Eyke HüllermeierarXiv 2023 · DOI

Abstract

Calculating exact pair-interaction Shapley values is traditionally an NP-hard problem with exponential complexity ($O(2^n)$). This paper introduces STI-KNN, a recursive algorithm that reduces this complexity to polynomial time ($O(tn^2)$) specifically for K-Nearest Neighbor (KNN) models. By exploiting the distance-based sorting property of KNN, STI-KNN provides exact interaction values, enabling deep data valuation for large-scale datasets. We demonstrate the algorithm's efficiency and utility in visualizing data quality across 16 diverse datasets.

Introduction

Data valuation—quantifying the contribution of individual training points to a model's performance—is essential for dataset curation and model interpretability. The Shapley value is the gold standard for this task, but it often fails to capture interactions: the synergistic or antagonistic effects between pairs of data points. While individual Shapley values can be approximated, calculating exact pair interactions has remained computationally prohibitive due to its $O(2^n)$ complexity.

This research bridges the gap between theoretical explainable AI (xAI) and practical application by introducing STI-KNN. This algorithm allows researchers to efficiently identify redundant or mislabeled data points by analyzing how they interact with others to influence model accuracy.

Methods: Exploiting the KNN Structure

The core innovation of STI-KNN lies in its exploitation of the mathematical structure of KNN models. Unlike complex architectures like neural networks, a KNN model's "training" phase is essentially a sorting operation based on distance to a test point.

Sorting training points by distance to a test point allows for recursive interaction calculation.

Recursive Calculation and Exactness

Instead of iterating through every possible subset of training data, STI-KNN uses a recursive approach. It calculates interaction values starting from the most distant points and propagates them toward the nearest neighbors. This method leverages the fact that for small subsets, the KNN valuation function becomes linear. Unlike Monte Carlo approximations, STI-KNN provides exact interaction values, ensuring mathematical rigor and satisfying the efficiency axiom of the Shapley Taylor Interaction index.

Key Insights and Ideas

The researchers identified several critical properties of data interactions through the lens of STI-KNN:

  • The "Unexpected Independence" Property: The interaction between two training points for a specific test point is independent of the features of a third point; it depends solely on their relative distance-based ranking.
  • Visualizing Data Quality: Interaction matrices serve as high-resolution "heat maps" for dataset health. For instance, redundant points within a class typically show decreased interaction values, while mislabeled points exhibit patterns characteristic of the opposing class.
Interaction matrices for the Circle dataset reveal how data points within and across classes synergize or conflict.
  • Robustness to Hyperparameters: Interestingly, the choice of $k$ (the number of neighbors) primarily affects the scale of interaction values rather than the overall structure of the interaction matrix. Correlations between matrices generated with different $k$ values consistently exceed 0.99.

Results

The primary achievement of this work is the dramatic reduction in computational complexity from $O(2^n)$ to $O(tn^2)$, where $n$ is the training set size and $t$ is the test set size. This shift transforms interaction calculation from a theoretical impossibility to a task that can be completed in seconds on standard hardware.

The algorithm was validated on 16 datasets, including synthetic geometric sets like "Circle" and "Moon," and real-world benchmarks like FashionMNIST and CreditCard. In all cases, the sum of interaction values correctly equaled the total test accuracy, confirming the algorithm's mathematical consistency.

Limitations and Future Work

While STI-KNN is a significant step forward, it is currently limited to KNN models. However, because KNN Shapley values often transfer well to other models (like Gradient Boosting), they can serve as effective surrogates. Future research aims to extend this recursive logic to higher-order interactions (beyond pairs) and other interaction indices like the Shapley Interaction Index (SII), further automating the detection of noisy labels in massive datasets.

Summary updated

BibTeX
@misc{belaid2023optimizing,
  author = {Mohamed Karim Belaid and Dorra El Mekki and Maximilian Rabus and Eyke Hüllermeier},
  title = {{Optimizing Data Shapley Interaction Calculation from O(2^n) to O(t n^2) for KNN models}},
  year = {2023},
  eprint = {2304.01224},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2304.01224},
}
Figure