k-Nearest Neighbors
Vote (or average) among the k closest training points.
k-nearest neighbours (k-NN) has no training phase: it simply stores the data. To predict for a new point, find the training points closest to it and take a majority vote of their labels (classification) or the average of their targets (regression).
controls the bias–variance trade-off. follows every point, giving jagged boundaries and overfitting (zero training error). Large smooths the boundary and eventually underfits — with , it always predicts the majority class. Choose by cross-validation; odd avoids ties in binary problems.
Variants weight neighbours by inverse distance, so closer points count more.
Pros: simple, no assumptions about the data's shape, naturally multiclass. Cons: prediction is slow ( per query without index structures like KD-trees), memory-heavy, and suffers badly in high dimensions.
Formula to remember
k-NN prediction
Majority vote among the k nearest neighbours.