WorksComputer Science
Randomized Weighted Distance Nearest Neighbor Classification
Abstract
The nearest neighbor classifier is known to be memory-inefficient, and a popular solution is to compress its training data. Recently, a compression algorithm based on weighted distance nearest neighbor (WNN) was proposed, which has elegant theoretical properties and effective classification performance. However, its compression process is computationally expensive, for it performs a greedy search of the densest homogeneous balls in data space. In this paper, we propose a randomized variant RWNN. It simply randomizes the ball search process, and results in substantially reduced compression time (often 0.1%–1% of WNN's) and similar classification accuracy. RWNN also inherits several theoretical properties of WNN including consistency and a generalization bound. Experimental results on multiple datasets demonstrate its efficacy.
Introduction
The nearest neighbor classifier has been long studied for its simplicity and interpretability [3, 4], but it is also known to be memory-inefficient due to the need of storing all training data for inference. A popular solution is to compress the training set, but the compression process may be expensive and thus lead to a hard tradeoff between memory and computation efficiency.
An example is the recent compression algorithm weighted distance nearest neighbor (WNN) [9]. It is theoretically elegant and empirically effective, but the compression process is computationally expensive as it relies on a greedy search for the densest homogeneous balls in data space. Here, a homogeneous ball is one that contains data from the same class. Details will be provided later.
This research leverages randomness to accelerate WNN. We show that, by simply randomizing the ball search process (i.e., randomly pick a homogeneous ball instead of finding the densest one), the algorithm can be significantly accelerated without sacrificing empirical effectiveness or certain theoretical properties. We call this new variant randomized WNN (RWNN), and further extend it to a heuristic RWNNs which terminates the random-pick process early to achieve desired compression rate. Experimental results on multiple datasets demonstrate efficacy of our algorithms, where they often require less than 1% of the WNN time while maintaining classification accuracy.
Related Work
Data compression for nearest neighbor classification has been long studied. Early representative methods include condensed nearest neighbor (CNN) [10], reduced nearest neighbor (RNN) [7], and fast condensed nearest neighbor (FCNN) [1]. Their main idea is to have the compressed set repeatedly include training points that are misclassified by it. Modern studies explore other directions, e.g., near-optimal sample compression (NOSC) [8] is the first method with provable guarantees on compression rate and generalization performance, and stochastic neighbor compression (SNC) [11] generates a synthetic compressed set which often achieves higher compression rates. More recently, weighted distance nearest neighbor condensing (WNN) [9] aims to find a compressed set that is optimal for a distance-weighted nearest neighbor classifier. The resulting classifier is shown to enjoy many theoretical properties, including consistency (i.e., correctly classify all training points), Bayes consistency and generalization. In this paper, we improve the computational efficiency of WNN.
Preliminaries
Following [9], we focus on binary classification. Let be the data space, be a distance function, and be the labeling function, i.e., is the true label of . Given a training set , the nearest neighbor classifier is
The WNN classifier [9] is built on “homogeneous ball”. For an and , let
be the open ball centered at with radius . A homogeneous ball centered at is the largest open ball that only contains points which have the same class as , and the ball radius is the distance from to the nearest point from a different class, i.e.,
Given a compressed set , the WNN classifier is then defined as
where is a weighted distance function defined as
and is a weighting function which assigns 1 to all points not in and assigns the radii of homogeneous balls centered at points in . Let be the error of . The following is from [9 Theorem 3.4].
Theorem 2.1 ([9]). For any distribution on , labeling function , and , with probability at least over the i.i.d. sampling of , if the WNN classifier corresponding to and correctly classifies all points in , then
It is verified in [9] that the WNN classifier indeed correctly classifies all points in (i.e. being consistent), and thus enjoys the above generalization bound.
Algorithm 1 RWNN Compression
Input: Training set
Candidate set .
Condensed set .
Weight set , where for all .
while do
Randomly pick an .
Compute radius of the homogeneous ball centered at as .
Add to the condensed set, i.e., .
Update the weight of to the ball radius, i.e., .
Remove all points in the ball, i.e., .
end while
return and .
Randomized WNN
In this section, we introduce randomized weighted distance nearest neighbor condensing (RWNN). Our compression algorithm is almost the same as WNN, except that it randomly picks homogeneous balls instead of finding the densest ones. Detailed process is presented in Algorithm 1.
Our main innovation lies in Steps 5-6, which randomly pick and then compute its ball radius – this takes time per round. In contrast, WNN needs to compute the ball radius for every , which takes time; then, it repeatedly finds the densest balls which takes time per round. This is the main reason for our speedup.
After the condensed set and weight set are returned, we apply the same classifier as WNN, defined in (4). For clarity, we will refer to the resulting classifier as the RWNN classifier.
Theoretical Properties
RWNN inherits two theoretical properties of WNN, as these properties only depend on the homogeneity of balls and classifier design but not the density of balls. The first property is consistency.
Remark 3.1. The RWNN classifier returned by Algorithm 1 is consistent on the input training set, i.e., it can correctly classify all points in .
Proof. The argument is the same as in [9]. Let be the RWNN classifier, where and are the condensed set and weight set returned by Algorithm 1. Consider two cases.
Case 1: . Then, it will be correctly classified by as is its own nearest neighbor.
Case 2: . In this case, for some (and is removed with the ball at Step 9). By the definition of , this means . In addition, and by Steps 3 & 8. Then, by (5), the weighted distance from to is
where the inequality holds because .
Algorithm 2 RWNNs Compression
Input: Training set , threshold .
Candidate set .
Condensed set .
Weight set , where for all .
while and do
Randomly pick an .
Compute radius of the homogeneous ball centered at as .
Add to the condensed set, i.e., .
Update the weight of to the ball radius, i.e., .
Remove all points in the ball, i.e., .
end while
return and .
Now let be any point with a different label from . Then
where the inequality holds because is from a different class so by definition (3).
Combining (7), (8) shows that no point from a different class can be the nearest neighbor of , because their weighted distance to is at least 1 and strictly bigger than for the ball center which satisfies . Thus, the nearest neighbor rule will correctly classify .
The second property is that the RWNN classifier inherits the generalization error bound of the WNN classifier, because it is also consistent on the training set (Remark 3.1).
Remark 3.2. Theorem 2.1 applies to the RWNN classifier built on Algorithm 1.
The only difference is that, intuitively, the condensed set of RWNN is expected to be larger than the condensed set of WNN because the latter removes more points through finding the densest balls. This means WNN results in a smaller , which implies a smaller error bound in Theorem 2.1.
A Heuristic Variant
In this section, we propose a heuristic variant of RWNN (RWNNs), which can further control the condensed set size. Its idea is to simply stop the homogeneous ball search process when a desired condensing rate is achieved. This allows RWNNs to achieve the same condensing rate as WNN, although it also loses the consistency guarantee because is not exhausted. Detailed process is described in Algorithm 2, where Step 4 has an additional stopping criterion.
Experiment
We evaluated the proposed algorithms on ten real-world datasets, including Aids [12], Magic [13], Clients [14], SmartGrid [2], Diabetic [6], Avila [5], DryBean, Flipkart, CreditScore (Kaggle repository), and Malware (UCI repository). The first five sets have binary labels, and the last five have multi-class labels. (Although WNN was designed for binary label, it naturally applies to multi-class label.) On each set, we randomly selected 50% of the points for training and used the rest for testing. Features were standardized, and all results were averaged over 20 random splits.
In addition to WNN, we also compare RWNNs with representative compression methods including subsampling, NOSC [8], and SNC [11]. Euclidean distance is used by all methods. All results, including computation time, were generated on a machine with an 8-core Intel 11th Generation Core i7 processor, 32 GB of RAM, and a clock speed of 2.50 GHz. Source code is at 1.1
Comparison to WNN
Performance of WNN, RWNNs and RWNN are shown in Table 1. Condensing rate is , and RWNNs sets to achieve the same rate as others. The error standard deviations of WNN and RWNNs across different datasets are around 0.0012 and 0.0016, respectively.
| Dataset | Size | Condensing Rate | Classification Error | Compression Time | |||||
| WNN | RWNN | WNN | RWNNs | RWNN | WNN | RWNNs | RWNN | ||
| Aids | 15k | 0.32 | 0.45 | 0.337 | 0.343 | 0.341 | 420 | 0.798 | 0.814 |
| Magic | 19k | 0.16 | 0.26 | 0.163 | 0.178 | 0.172 | 4219 | 0.749 | 0.786 |
| Clients | 30k | 0.23 | 0.35 | 0.207 | 0.220 | 0.219 | 2230 | 3.121 | 3.225 |
| SmartGrid | 60k | 0.05 | 0.14 | 0.047 | 0.077 | 0.072 | 3828 | 7.692 | 7.909 |
| Avila | 7k | 0.25 | 0.34 | 0.277 | 0.298 | 0.282 | 46 | 0.151 | 0.153 |
| DryBean | 8k | 0.08 | 0.12 | 0.085 | 0.087 | 0.086 | 17 | 0.234 | 0.235 |
| Flipkart | 30k | 0.28 | 0.36 | 0.315 | 0.325 | 0.322 | 2702 | 1.488 | 1.569 |
Table 1. Performance of WNN, RWNNs and RWNN
Compared to WNN, we see that both RWNN and RWNNs consume substantially less compression time, with 100-1000 times speedup on most datasets. They also obtain similar classification accuracy to WNN, demonstrating a more optimal tradeoff between efficiency and accuracy.
We also see that RWNN generates larger condensed sets, which is consistent with the intuition. This, however, does not appear to affect its efficiency, since its compression time is close to the time of RWNNs and remains significantly lower than the time of WNN.
Comparison to Other Methods
Table 2 shows the results of other representative compression methods. Condensing rate is set to 1% (approximately for NOSC). To meet this goal, we stop the greedy search of WNN early and denote the resulting algorithm as WNNs. For NOSC, hyperparameter indirectly determines the condensing rate, and we manually tuned it so the resulting rate is as close to 1% as possible. Table 4 gives several example choices of and their resulting condensing rates. Figure 1 shows the performance across different condensing rates.
| 13.5 | 11 | 7.8 | 6.9 | 6.4 | ||
| NOSC Rate | .06e-2 | .11e-2 | .53e-2 | 1e-2 | 1.6e-2 | |
| Target Rate | .05e-2 | .10e-2 | .50e-2 | 1e-2 | 1.5e-2 |
Table 4. Example (top row) and resulting condensing rate (bottom rows) for NOSC on Diabetic

Figure 1. Error versus Condensing Rate on Diabetic
In Table 2, we observe that WNNs often achieves the lowest error, followed by RWNNs. This demonstrates the strength of this family of compression methods under low condensing rate. SNC also performs well such as on SmartGrid and CreditScore, but its compressed sets are synthetic and thus may have limited applicability due to the lack of interpretability. Besides, SNC compression
| Dataset | Size | 1NN | Sub | NOSC | SNC | WNNs | RWNNs |
| Aids | 15k | 0.374 | 0.381 | 0.407 | 0.362 | <u>0.312</u> | <u>0.337</u> |
| Magic | 19k | 0.192 | 0.302 | 0.376 | 0.268 | <u>0.194</u> | <u>0.246</u> |
| Clients | 30k | 0.270 | 0.257 | 0.262 | 0.275 | <u>0.217</u> | <u>0.225</u> |
| SmartGrid | 60k | 0.078 | 0.140 | 0.153 | <u>0.028</u> | <u>0.057</u> | 0.110 |
| Diabetic | 122k | 0.379 | 0.459 | 0.530 | <u>0.427</u> | - | <u>0.432</u> |
| Avila | 7k | 0.267 | 0.438 | - | 0.507 | <u>0.358</u> | <u>0.421</u> |
| DryBean | 8k | 0.101 | 0.112 | - | 0.194 | <u>0.087</u> | <u>0.101</u> |
| Flipkart | 30k | 0.309 | 0.482 | - | <u>0.411</u> | <u>0.442</u> | 0.501 |
| CreditScore | 100k | 0.253 | 0.389 | - | <u>0.305</u> | - | <u>0.351</u> |
| Malware | 332k | 0.063 | 0.340 | - | <u>0.282</u> | - | <u>0.220</u> |
caption: Classification Error. On each dataset, two lowest errors of the condensing algorithms are underlined. NOSC was designed for binary class and does not apply to the bottom five multiclass datasets. WNNs did not terminate in a reasonable time on Diabetic, CreditScore and Malware.
:::
is also more computationally expensive than RWNN, though less than WNN. Table 3 shows its compression time on SmartGrid. (WNNs/RWNNs takes more inference time because they compute weighted distance, while other methods compute unweighted distance.) Overall, the results suggest that RWNNs achieves a better efficiency-accuracy tradeoff.
| Method | Compression Time | Inference Time |
| 1NN | 0 | 5.48 |
| Sub | 0 | 0.14 |
| NOSC | 0.23 | 0.13 |
| SNC | 51.95 | 0.15 |
| WNNs | 887.85 | 0.92 |
| RWNNs | 7.83 | 0.91 |
Table 3. Time (sec) on SmartGrid
Finally, Table 5 shows additional results under other performance metrics, under the same setting as in Table 1. It also shows that RWNN and RWNNs perform closely to WNN.
| Dataset | Method | Precision | Recall | F1 |
| Aids | WNN | 0.441 | 0.266 | 0.332 |
| Aids | RWNN | 0.409 | 0.290 | 0.339 |
| Aids | RWNN | 0.416 | 0.284 | 0.337 |
| Avila | WNN | 0.724 | 0.643 | 0.668 |
| Avila | RWNN | 0.714 | 0.645 | 0.667 |
| Avila | RWNN | 0.688 | 0.602 | 0.625 |
Table 5. Performance under other Metrics
Conclusion
This study accelerates a theoretically elegant data compression algorithm WNN for nearest neighbor classification. The proposed algorithm RWNN simply randomizes the greedy ball search process of WNN, which substantially reduces compression time of WNN while maintaining its classification accuracy and certain theoretical properties including consistency and generalization bound. We further propose a heuristic variant of RWNN, named RWNNs, which allows one to specify the condensing rate by early stopping the (randomized) ball search process. Experimental results on multiple datasets demonstrate the efficacy of RWNN and RWNNs.
Acknowledgment
Claude was used to proofread the paper and refine the proof of Remark 3.1.
References
- [1]Angiulli. Fast condensed nearest neighbor rule. In Proceedings of the 22nd International Conference on Machine Learning (ICML), pages 25–32. ACM, 2005.DOI
- [2]P. C. Breviglieri. Smart grid stability dataset. https://www.kaggle.com/datasets/pcbreviglieri/smart-grid-stability, 2020. Accessed: 2025-06-25.
- [3]George H Chen, Devavrat Shah, et al. Explaining the success of nearest neighbor methods in prediction. Foundations and Trends® in Machine Learning, 10(5-6):337–588, 2018.
- [4]Thomas M. Cover and Peter E. Hart. Nearest neighbor pattern classification. IEEE Transactions on Information Theory, 13(1):21–27, 1967.DOI
- [5]Dheeru Dua and Casey Graff. Avila dataset. https://archive.ics.uci.edu/dataset/459/avila, 2018. Accessed: 2025-06-25.
- [6]Hossein Esfandiari, Vahab Mirrokni, Umar Syed, and Sergei Vassilvitskii. Label differential privacy via clustering. In International Conference on Artificial Intelligence and Statistics, pages 7055–7075. PMLR, 2022.
- [7]G. W. Gates. The reduced nearest neighbor rule. IEEE Transactions on Information Theory, 18(3):431–433, 1972.
- [8]Lee-Ad Gottlieb, Aryeh Kontorovich, and Pinhas Nisnevitch. Near-optimal sample compression for nearest neighbors. Advances in Neural Information Processing Systems, 27, 2014.arxiv.org/abs/1404.3368
- [9]Lee-Ad Gottlieb, Tomer Sharabi, and Roni Weiss. Weighted distance nearest neighbor condensing. In Proceedings of the 41st International Conference on Machine Learning, pages 16153–16166. PMLR, 2024.arxiv.org/abs/2310.15951
- [10]Peter E. Hart. The condensed nearest neighbor rule. IEEE Transactions on Information Theory, 14(3):515–516, 1968.
- [11]Matt Kusner, Stephen Tyree, Kilian Weinberger, and Kunal Agrawal. Stochastic neighbor compression. In International Conference on Machine Learning, pages 622–630. PMLR, 2014.
- [12]Shih-Jen Tan. Aids clinical trials dataset. https://www.kaggle.com/datasets/tanshihjen/aids-clinical-trials, 2023. Accessed: 2025-06-25.
- [13]UCI Machine Learning Repository. Magic gamma telescope data set. https://archive.ics.uci.edu/dataset/159/magic+gamma+telescope, 2007. Accessed: 2025-06-25.
- [14]I-Cheng Yeh and Che-hui Lien. Default of credit card clients dataset. https://archive.ics.uci.edu/dataset/350/default+of+credit+card+clients, 2008. Accessed: 2025-06-25.