KNN is a non-parametric algorithm: it stores all training data and classifies new points by majority vote among the nearest neighbors.
Algorithm:
- Given query point , compute distance to all training points
- Select the closest neighbors
- 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
- controls the Bias-Variance Tradeoff:
- Small → complex boundary, low bias, high variance (overfits)
- Large → 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: per query ( = dataset size, = dimensions). KD-trees or ball trees accelerate to in low dimensions.
See also: Evaluation Metrics, Cross-Validation