∑GATE DA Atlas

k-Nearest Neighbours

The k-NN algorithm for classification and regression, distance metrics (Euclidean, Manhattan, Minkowski, cosine, Hamming), why feature scaling matters, choosing k and its bias–variance effect, distance-weighted voting, computational cost and speed-ups (k-d trees, approximate search), the curse of dimensionality, and the 1-NN error guarantee.

Section 69 practice questions

The algorithm

k-NN makes predictions directly from the stored training data:

  1. Compute the distance from the query x\mathbf x to every training point.
  2. Take the kk nearest.
  3. Classification: majority vote of their labels. Regression: average (or median) of their targets.
Python
import numpy as np
from collections import Counter

def knn_predict(X_train, y_train, x, k=3):
    d = np.linalg.norm(X_train - x, axis=1)       # Euclidean distances
    idx = np.argsort(d)[:k]                       # k nearest
    return Counter(y_train[idx]).most_common(1)[0][0]

k-NN is non-parametric (no fixed set of parameters), instance-based and lazy (all work happens at prediction time).

? k = 1 → greenk = 5 → blue (3 vs 2)
The prediction can change with k: the nearest point is green, but most of the five nearest are blue.

Distance metrics

MetricFormulaUse
Euclidean (L2)∑(xi−yi)2\sqrt{\sum(x_i - y_i)^2}default for continuous features
Manhattan (L1)∑∣xi−yi∣\sum\lvert x_i - y_i\rvertgrid-like movement, robust to outliers
Minkowski (LpL_p)(∑∣xi−yi∣p)1/p\left(\sum\lvert x_i - y_i\rvert^p\right)^{1/p}p=1,2p = 1, 2 special cases; p→∞p \to \infty: Chebyshev max⁡∣xi−yi∣\max\lvert x_i - y_i\rvert
Cosine distance1−x⊤y∥x∥∥y∥1 - \frac{\mathbf x^\top\mathbf y}{\lVert\mathbf x\rVert\lVert\mathbf y\rVert}text, embeddings (direction matters, not length)
Hammingnumber of differing positionscategorical/binary features

Choosing k

kkBoundaryBiasVariance
1very jagged, follows noiselowhigh
moderatesmoothermoderatemoderate
nnconstant (majority class everywhere)highlow

Choose kk by cross-validation; a common heuristic starting point is k≈nk \approx \sqrt n. Use odd kk for binary problems.

Distance-weighted voting gives closer neighbours more say (weights 1/d1/d or 1/d21/d^2), reducing sensitivity to kk.

Cost and speed-ups

  • Training: O(1)O(1) (store the data) — but memory O(nd)O(nd).
  • Prediction: O(nd)O(nd) distances plus selecting the kk smallest.
  • Speed-ups: k-d trees and ball trees (effective in low dimensions), locality-sensitive hashing and graph-based approximate nearest neighbour search (high dimensions, used in vector databases).

The curse of dimensionality

In high dimensions, data becomes sparse and distances concentrate — the nearest and farthest points are almost equally distant, so neighbourhoods are no longer "local". To keep the same density of samples, the data needed grows exponentially with dd. Mitigate with PCA, feature selection or learned embeddings.

A theoretical guarantee

As n→∞n \to \infty, the error rate of 1-NN is at most twice the Bayes error (the lowest achievable error) — a remarkable result for such a simple method (Cover and Hart, 1967). With k→∞k \to \infty and k/n→0k/n \to 0, k-NN converges to the Bayes classifier itself.

Quick revision

Practice questions

GATE-style questions: 4 one-mark and 5 two-mark. Wrong MCQs cost ⅓ of their marks in the real exam; MSQ and NAT have no negative marking. Attempt each before opening the solution.

Q1MCQ2 marks

Training points: class + at (1,1)(1, 1) and (2,1)(2, 1); class − at (4,4)(4, 4), (4,5)(4, 5) and (5,5)(5, 5). Using Euclidean distance and k=3k = 3, the query point (2,2)(2, 2) is classified as

Q2NAT2 marks

k-NN regression with k=3k = 3 uses training data x=1,2,4,7,8x = 1, 2, 4, 7, 8 with targets y=3,5,4,10,12y = 3, 5, 4, 10, 12. The prediction at x=5.5x = 5.5 is ______ (round to two decimals).

Q3MCQ1 mark

How does increasing kk in k-NN affect the model?

Q4MSQ2 marks

Which statements about k-NN are true?

One or more options are correct.

Q5NAT1 mark

The Manhattan (L1) distance between (1,2,3)(1, 2, 3) and (4,0,3)(4, 0, 3) is ______.

Q6NAT1 mark

For a training set with no two identical points carrying different labels, the training error of the 1-nearest-neighbour classifier (each training point classified using the full training set) is ______.

Q7MCQ2 marks

The "curse of dimensionality" hurts k-NN in high dimensions mainly because

Q8NAT2 marks

Distance-weighted k-NN uses weight 1/d1/d for each neighbour. With k=3k = 3 neighbours at distances 1 (class A), 3 (class B) and 3 (class B), the total weight for class B is ______ (round to two decimals).

Q9MCQ1 mark

For a binary classification problem, why is an odd kk usually chosen?