betaFeedback

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 X⊆Rp\mathcal{X} \subseteq\mathbb{R}^{p} be the data space, d:X×X→Rd : \mathcal{X} \times\mathcal{X} \to\mathbb{R} be a distance function, and ℓ:X→{0,1}\ell: \mathcal{X} \to\{0,1\} be the labeling function, i.e., ℓ(x)\ell(x) is the true label of xx. Given a training set S⊆XS \subseteq\mathcal{X}, the nearest neighbor classifier is

h(z)=ℓ(arg min⁡x∈S d(z,x)),∀z∈X.(1)h(z) = \ell\left(\underset{x \in S}{\operatorname{arg\,min}}\, d(z,x)\right), \quad\forall z \in\mathcal{X}. \tag*{(1)}

The WNN classifier [9] is built on “homogeneous ball”. For an x∈Xx \in\mathcal{X} and r>0r > 0, let

B(x,r)={z∈X:d(z,x)<r},(2)B(x,r) = \{z \in\mathcal{X} : d(z,x) < r\}, \tag*{(2)}

be the open ball centered at xx with radius rr. A homogeneous ball centered at xx is the largest open ball that only contains SS points which have the same class as xx, and the ball radius is the distance from xx to the nearest SS point from a different class, i.e.,

r(x)=min⁡z∈S:ℓ(z)≠ℓ(x)d(x,z).(3)r(x) = \min_{z \in S:\ell(z)\ne\ell(x)} d(x,z). \tag*{(3)}

Given a compressed set S~⊆S\widetilde{S} \subseteq S, the WNN classifier is then defined as

h(S,w~)(z)=ℓ(arg min⁡x∈S~ dw(z,x)),(4)h_{(S,\widetilde{w})}(z) = \ell\left(\underset{x \in\widetilde{S}}{\operatorname{arg\,min}}\, d_{w}(z,x)\right), \tag*{(4)}

where dw:X×X→Rd_{w} : \mathcal{X} \times\mathcal{X} \to\mathbb{R} is a weighted distance function defined as

dw(x,x′)=d(x,x′)w(x) w(x′),(5)d_{w}(x,x') = \frac{d(x,x')}{w(x)\,w(x')}, \tag*{(5)}

and w:X→Rw : \mathcal{X} \to\mathbb{R} is a weighting function which assigns 1 to all points not in S~\widetilde{S} and assigns the radii of homogeneous balls centered at points in S~\widetilde{S}. Let err⁡(h)=Pr⁡(h(x)≠ℓ(x))\operatorname{err}(h) = \Pr(h(x) \ne\ell(x)) be the error of hh. The following is from [9 Theorem 3.4].

Theorem 2.1 ([9]). For any distribution on X\mathcal{X}, labeling function ℓ\ell, and δ∈(0,1)\delta\in(0,1), with probability at least 1−δ1-\delta over the i.i.d. sampling of SS, if the WNN classifier h(S,w~)h_{(S,\widetilde{w})} corresponding to S~\widetilde{S} and ww correctly classifies all points in SS, then

err⁡(h(S,w~))≤2∣S∣−∣S~∣(∣S~∣ln⁡2e∣S∣∣S~∣+ln⁡∣S∣δ).(6)\operatorname{err}(h_{(S,\widetilde{w})}) \le\frac{2}{|S|-|\widetilde{S}|}\left(|\widetilde{S}|\ln\frac{2e|S|}{|\widetilde{S}|}+\ln\frac{|S|}{\delta}\right). \tag*{(6)}

It is verified in [9] that the WNN classifier h(S,w~)h_{(S,\widetilde{w})} indeed correctly classifies all points in SS (i.e. being consistent), and thus enjoys the above generalization bound.

Algorithm 1 RWNN Compression

Input: Training set SS

  1. Candidate set S′←SS' \leftarrow S.

  2. Condensed set S~←∅\widetilde{S} \leftarrow\emptyset.

  3. Weight set ww, where w(x)←1w(x) \leftarrow1 for all x∈Sx \in S.

  4. while S′≠∅S' \ne\emptyset do

  5. Randomly pick an x∈S′x \in S'.

  6. Compute radius of the homogeneous ball centered at xx as r(x)=min⁡z∈S:ℓ(z)≠ℓ(x)d(x,z)r(x) = \min_{z \in S:\ell(z) \ne\ell(x)} d(x,z).

  7. Add xx to the condensed set, i.e., S~←S~∪{x}\widetilde{S} \leftarrow\widetilde{S} \cup\{x\}.

  8. Update the weight of xx to the ball radius, i.e., w(x)←r(x)w(x) \leftarrow r(x).

  9. Remove all points in the ball, i.e., S′←S′−{z∈S′∩B(x,r(x))}S' \leftarrow S' - \{z \in S' \cap B(x,r(x))\}.

  10. end while

  11. return S~\widetilde{S} and ww.

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 xx and then compute its ball radius – this takes O(∣S∣)O(|S|) time per round. In contrast, WNN needs to compute the ball radius for every x∈Sx \in S, which takes O(∣S∣2)O(|S|^2) time; then, it repeatedly finds the densest balls which takes Ω(∣S′∣)\Omega(|S'|) time per round. This is the main reason for our speedup.

After the condensed set S~\widetilde{S} and weight set ww 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 SS.

Proof. The argument is the same as in [9]. Let h(S~,w)h_{(\widetilde{S},w)} be the RWNN classifier, where S~\widetilde{S} and ww are the condensed set and weight set returned by Algorithm 1. Consider two cases.

Case 1: z∈S~z \in\widetilde{S}. Then, it will be correctly classified by h(S~,w)h_{(\widetilde{S},w)} as zz is its own nearest neighbor.

Case 2: z∉S~z \notin\widetilde{S}. In this case, z∈B(x,r(x))z \in B(x,r(x)) for some x∈S~x \in\widetilde{S} (and is removed with the ball at Step 9). By the definition of r(x)r(x), this means ℓ(z)=ℓ(x)\ell(z) = \ell(x). In addition, w(z)=1w(z) = 1 and w(x)=r(x)w(x) = r(x) by Steps 3 & 8. Then, by (5), the weighted distance from zz to xx is

dw(z,x)=d(z,x)w(z)w(x)=d(z,x)1⋅r(x)<1,(7)d_w(z,x) = \frac{d(z,x)}{w(z)w(x)} = \frac{d(z,x)}{1 \cdot r(x)} < 1, \tag*{(7)}

where the inequality holds because z∈B(x,r(x))z \in B(x,r(x)).

Algorithm 2 RWNNs Compression

Input: Training set SS, threshold θ\theta.

  1. Candidate set S′←SS' \leftarrow S.

  2. Condensed set S~←∅\widetilde{S} \leftarrow\emptyset.

  3. Weight set ww, where w(x)←1w(x) \leftarrow1 for all x∈Sx \in S.

  4. while S′≠∅S' \ne\emptyset and ∣S~∣/∣S∣<θ|\widetilde{S}|/|S| < \theta do

  5. Randomly pick an x∈S′x \in S'.

  6. Compute radius of the homogeneous ball centered at xx as r(x)=min⁡z∈S:ℓ(z)≠ℓ(x)d(x,z)r(x) = \min_{z \in S:\ell(z)\ne\ell(x)} d(x,z).

  7. Add xx to the condensed set, i.e., S~←S~∪{x}\widetilde{S} \leftarrow\widetilde{S} \cup\{x\}.

  8. Update the weight of xx to the ball radius, i.e., w(x)←r(x)w(x) \leftarrow r(x).

  9. Remove all points in the ball, i.e., S′←S′−{z∈S′∩B(x,r(x))}S' \leftarrow S' - \{z \in S' \cap B(x,r(x))\}.

  10. end while

  11. return S~\widetilde{S} and ww.

Now let x′∈S~x' \in\widetilde{S} be any point with a different label from xx. Then

dw(z,x′)=d(z,x′)w(z)w(x′)=d(z,x′)1⋅r(x′)≥1,(8)d_w(z,x') = \frac{d(z,x')}{w(z)w(x')} = \frac{d(z,x')}{1 \cdot r(x')} \ge1, \tag*{(8)}

where the inequality holds because x′x' is from a different class so d(z,x′)≥r(x′)d(z,x') \ge r(x') by definition (3).

Combining (7), (8) shows that no point from a different class can be the nearest neighbor of zz, because their weighted distance to zz is at least 1 and strictly bigger than dw(x,z)<1d_w(x,z) < 1 for the ball center xx which satisfies ℓ(x)=ℓ(z)\ell(x) = \ell(z). Thus, the nearest neighbor rule will correctly classify zz.

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 ∣S~∣|\widetilde{S}|, 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 S′S' 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 ∣S~∣/∣S∣|\widetilde{S}|/|S|, and RWNNs sets θ=∣S~∣/∣S∣\theta= |\widetilde{S}|/|S| 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.

DatasetSizeCondensing RateClassification ErrorCompression Time
WNNRWNNWNNRWNNsRWNNWNNRWNNsRWNN
Aids15k0.320.450.3370.3430.3414200.7980.814
Magic19k0.160.260.1630.1780.17242190.7490.786
Clients30k0.230.350.2070.2200.21922303.1213.225
SmartGrid60k0.050.140.0470.0770.07238287.6927.909
Avila7k0.250.340.2770.2980.282460.1510.153
DryBean8k0.080.120.0850.0870.086170.2340.235
Flipkart30k0.280.360.3150.3250.32227021.4881.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 γ\gamma 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 γ\gamma and their resulting condensing rates. Figure 1 shows the performance across different condensing rates.

γ\gamma13.5117.86.96.4
NOSC Rate.06e-2.11e-2.53e-21e-21.6e-2
Target Rate.05e-2.10e-2.50e-21e-21.5e-2

Table 4. Example γ\gamma (top row) and resulting condensing rate (bottom rows) for NOSC on Diabetic

Error versus Condensing Rate 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

DatasetSize1NNSubNOSCSNCWNNsRWNNs
Aids15k0.3740.3810.4070.362<u>0.312</u><u>0.337</u>
Magic19k0.1920.3020.3760.268<u>0.194</u><u>0.246</u>
Clients30k0.2700.2570.2620.275<u>0.217</u><u>0.225</u>
SmartGrid60k0.0780.1400.153<u>0.028</u><u>0.057</u>0.110
Diabetic122k0.3790.4590.530<u>0.427</u>-<u>0.432</u>
Avila7k0.2670.438-0.507<u>0.358</u><u>0.421</u>
DryBean8k0.1010.112-0.194<u>0.087</u><u>0.101</u>
Flipkart30k0.3090.482-<u>0.411</u><u>0.442</u>0.501
CreditScore100k0.2530.389-<u>0.305</u>-<u>0.351</u>
Malware332k0.0630.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.

MethodCompression TimeInference Time
1NN05.48
Sub00.14
NOSC0.230.13
SNC51.950.15
WNNs887.850.92
RWNNs7.830.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.

DatasetMethodPrecisionRecallF1
AidsWNN0.4410.2660.332
AidsRWNN0.4090.2900.339
AidsRWNNs_{\mathrm{s}}0.4160.2840.337
AvilaWNN0.7240.6430.668
AvilaRWNN0.7140.6450.667
AvilaRWNNs_{\mathrm{s}}0.6880.6020.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. [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. [2]P. C. Breviglieri. Smart grid stability dataset. https://www.kaggle.com/datasets/pcbreviglieri/smart-grid-stability, 2020. Accessed: 2025-06-25.
  3. [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. [4]Thomas M. Cover and Peter E. Hart. Nearest neighbor pattern classification. IEEE Transactions on Information Theory, 13(1):21–27, 1967.DOI
  5. [5]Dheeru Dua and Casey Graff. Avila dataset. https://archive.ics.uci.edu/dataset/459/avila, 2018. Accessed: 2025-06-25.
  6. [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. [7]G. W. Gates. The reduced nearest neighbor rule. IEEE Transactions on Information Theory, 18(3):431–433, 1972.
  8. [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. [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. [10]Peter E. Hart. The condensed nearest neighbor rule. IEEE Transactions on Information Theory, 14(3):515–516, 1968.
  11. [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. [12]Shih-Jen Tan. Aids clinical trials dataset. https://www.kaggle.com/datasets/tanshihjen/aids-clinical-trials, 2023. Accessed: 2025-06-25.
  13. [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. [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.

Paper details

vlnc:W_pzmrjwyk
Version
v1 · latest
Published
Fresh — added to the Hub within the last 30 days
Added
License
CC BY 4.0
Usage

views · 0

downloads · 0

A view counts once per browser per day, a download each time the PDF is requested.

  • Computer Science

Claude

View statement
Looking for related papers…
Contents