Implement k-NN Efficiently
Code k-nearest neighbours fast, without slow loops.
Implementing k-nearest neighbours efficiently is a common coding question. There is no training step: you just store the data. The slow part is prediction, which needs the distance from each new point to every stored point. Looping over pairs is far too slow, so compute all distances at once with matrix operations, using the fact that squared distance equals both squared lengths added together minus twice their dot product. Then take the k smallest with a partial sort rather than sorting everything.
Math Foundations
Given training set and query set , k-Nearest Neighbors (k-NN):
- Computes Euclidean Distance from each query sample to all training samples :
- Identifies the smallest distance neighbors.
- Assigns the majority vote class label (Classification) or mean target value (Regression).
Matrix Expansion Speed Trick
Calculating pairwise distances using nested Python loops is extremely slow ().
Using the algebraic expansion of squared Euclidean distance:
- : Sum of squares along test rows (
[N_test, 1]). - : Sum of squares along train rows (
[1, N_train]). - : High speed matrix multiplication (
np.dot(Q, T.T)).
This computes all pairwise distances using optimized C / BLAS matrix math!
Efficient NumPy Implementation
python
import numpy as np
class KNNClassifierEfficient:
def __init__(self, k=5):
self.k = k
self.X_train = None
self.y_train = None
def fit(self, X, y):
# Lazy learning: store training data
self.X_train = X
self.y_train = y
def _compute_distances_fast(self, X_test):
# Fast pairwise squared Euclidean distance using matrix expansion
# D^2 = sum(Q^2) + sum(T^2) - 2 * (Q @ T.T)
test_sum = np.sum(np.square(X_test), axis=1, keepdims=True) # [N_test, 1]
train_sum = np.sum(
np.square(self.X_train), axis=1, keepdims=True
).T # [1, N_train]
dot_product = np.dot(X_test, self.X_train.T) # [N_test, N_train]
dists_sq = test_sum + train_sum - 2 * dot_product
# Clip small negative values due to floating point imprecision
return np.sqrt(np.maximum(dists_sq, 0.0))
def predict(self, X_test):
dists = self._compute_distances_fast(X_test)
num_test = X_test.shape[0]
y_pred = np.zeros(num_test, dtype=self.y_train.dtype)
for i in range(num_test):
# np.argpartition finds K smallest indices in O(N) time!
knn_indices = np.argpartition(dists[i], self.k)[: self.k]
knn_labels = self.y_train[knn_indices]
# Majority vote
counts = np.bincount(knn_labels)
y_pred[i] = np.argmax(counts)
return y_pred
Say this out loud
Implementing k-NN efficiently utilizes matrix expansion to compute all pairwise Euclidean distances using high speed matrix multiplication. Using
np.argpartitionidentifies top K nearest neighbor indices in linear time without doing a full array sort. Feature scaling is mandatory before running k-NN.
Followups to expect
- What is the computational complexity of k-NN inference? Brute force k-NN inference takes time. Using tree indexes (KD-Tree or Ball Tree) reduces query search time to .
- Why does k-NN struggle in high dimensional spaces? The Curse of Dimensionality causes distances between all pairs of points in high dimensional space to converge to near identical values, ruining neighbor discrimination.
Check yourself
Question 1 of 3
Why is k-Nearest Neighbors called a Lazy Learning non-parametric algorithm?