Implement k-NN Efficiently

Code k-nearest neighbours fast, without slow loops.

🟡 intermediate5 min readcoding
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 (Xtrain,ytrain)(X_{\text{train}}, y_{\text{train}}) and query set XtestX_{\text{test}}, k-Nearest Neighbors (k-NN):

  1. Computes Euclidean Distance from each query sample xqx_q to all training samples xix_i:
d(xq,xi)=∑j=1D(xq,j−xi,j)2d(x_q, x_i) = \sqrt{\sum_{j=1}^D (x_{q,j} - x_{i,j})^2}
  1. Identifies the KK smallest distance neighbors.
  2. 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 (O(Ntest×Ntrain×D)\mathcal{O}(N_{\text{test}} \times N_{\text{train}} \times D)).

Using the algebraic expansion of squared Euclidean distance:

∥Q−T∥2=∥Q∥2+∥T∥2−2(QTT)\| Q - T \|^2 = \| Q \|^2 + \| T \|^2 - 2 (Q T^T)

This computes all Ntest×NtrainN_{\text{test}} \times N_{\text{train}} 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.argpartition identifies 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

  1. What is the computational complexity of k-NN inference? Brute force k-NN inference takes O(Ntest×Ntrain×D)\mathcal{O}(N_{\text{test}} \times N_{\text{train}} \times D) time. Using tree indexes (KD-Tree or Ball Tree) reduces query search time to O(Dlog⁡Ntrain)\mathcal{O}(D \log N_{\text{train}}).
  2. 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?

More in Coding for ML

See all →
Implement Linear Regression from Scratch5 minImplement Self-Attention from Scratch5 minNumPy Broadcasting & Vectorization5 min