k-Nearest Neighbours
Making non parametric predictions based on distance metrics to the k closest training samples.
What is k-Nearest Neighbors (k-NN)?
k-Nearest Neighbors (k-NN) is one of the simplest supervised machine learning algorithms.
It relies on a intuitive principle: "Birds of a feather flock together."
Given a new unlabelled test sample :
- Measure geometric distance between and every single training point.
- Identify the closest training points (Neighbors).
- Classify: Take a majority vote among the neighbors.
- Regress: Calculate the average target value of the neighbors.
Class A (Red Circles) Class B (Blue Squares)
(O) (O) (X) (X)
\ │ / / │ /
▼ ▼ ▼ ▼ ▼ ▼
[ NEW TEST SAMPLE (?) ] ──► Find 3 Closest Neighbors: (2 Red, 1 Blue)
──► Predict: Class A (Red)!
Why k-NN is a "Lazy Learner"
Most ML algorithms (Linear Regression, Neural Nets) are Eager Learners: they process training data upfront to learn parameter weights (), and throw away the raw training data.
k-NN is a Lazy Learner (Instance-Based Learning):
- Training Phase: Zero math! Simply stores the raw training dataset in memory.
- Inference Phase: Computes distances to all training samples when a query arrives.
Distance Metrics
- Euclidean Distance (L2 Distance): Straight line distance in 2D/3D space.
- Manhattan Distance (L1 Distance): City block grid distance.
- Cosine Distance: Measures vector direction angle rather than magnitude, ideal for text embeddings.
The Impact of Hyperparameter k
┌──────────────────────────┬──────────────────────────┐
│ SMALL k (e.g. k = 1) │ LARGE k (e.g. k = 50) │
├──────────────────────────┼──────────────────────────┤
│ High Variance / Overfitting│ High Bias / Underfitting│
│ Boundary noise sensitive.│ Decision boundary becomes│
│ Fits individual outliers.│ smooth and over-averaged.│
└──────────────────────────┴──────────────────────────┘
Always choose an odd value for in binary classification (e.g. ) to prevent tie votes!
Severe Limitations of k-NN
- Curse of Dimensionality: As feature count grows large, distance between points becomes uniform, ruining neighbor search quality.
- Inference Latency : Must measure distance to every single sample at prediction time. Slow for millions of samples unless indexed via KD-Trees or Ball-Trees.
- Feature Scaling Mandatory: Un-scaled features dominate distance formulas. Always apply
StandardScalerfirst.
Say this out loud
k-Nearest Neighbors is a non parametric instance based lazy learner that makes predictions by finding the k closest training points using distance metrics. Small k produces high variance decision boundaries sensitive to noise, while large k produces high bias. Feature scaling is mandatory because distance metrics are sensitive to feature numerical ranges.
Followups to expect
- How do KD-Trees speed up k-NN inference? Partitioning feature space into a binary tree structure, reducing neighbor search time from brute force down to for low dimensional data ().
- What is Weighted k-NN? Weighting neighbor votes inversely by distance () so closer neighbors have a stronger influence on predictions than distant neighbors.
Check yourself
Why is k-Nearest Neighbors referred to as a Lazy Learner algorithm?