K-Nearest Neighbors

1 min read

KNN is a non-parametric algorithm: it stores all training data and classifies new points by majority vote among the kk nearest neighbors.

Algorithm:

  1. Given query point xq\mathbf{x}_q, compute distance to all training points
  2. Select the kk closest neighbors
  3. Classification: majority vote. Regression: average of neighbors' values

Key properties:

  • No training phase — all computation at inference ("lazy learner")
  • Decision boundary can be arbitrarily complex
  • kk controls the Bias-Variance Tradeoff:
    • Small kk → complex boundary, low bias, high variance (overfits)
    • Large kk → smooth boundary, high bias, low variance (underfits)

Distance metric matters: Euclidean is default but Manhattan, Minkowski, or learned metrics may be better depending on the problem. Features must be scaled (standardized) first.

Curse of dimensionality: in high dimensions, all points become roughly equidistant — KNN degrades. Dimensionality reduction (Principal Component Analysis (PCA)) can help.

Complexity: O(nd)O(nd) per query (nn = dataset size, dd = dimensions). KD-trees or ball trees accelerate to O(dlogn)O(d \log n) in low dimensions.

See also: Evaluation Metrics, Cross-Validation

Linked from