Machine Learning · 11 min read

K-Nearest Neighbors (KNN) Explained: The Lazy Learner That Just Works (with Python)

K-Nearest Neighbors makes a prediction by asking its closest neighbours to vote. There is no training phase at all — it just memorises the data — which makes it the simplest algorithm to understand and a great lesson in distance, scaling, and the curse of dimensionality.

What you'll learn

K-Nearest Neighbors (KNN) is about as simple as machine learning gets: to classify a new point, look at the K training points closest to it and take a majority vote. There is no equation to fit — KNN is a 'lazy learner' that simply stores the training data and does all the work at prediction time.

  • How KNN classifies (and does regression) by distance
  • How to choose K and what it trades off
  • Why feature scaling is non-negotiable
  • What the curse of dimensionality does to KNN

The intuition in plain English

You are the average of your closest neighbours. To decide whether a new fruit is an apple or an orange, KNN finds the K most similar fruits it has already seen and lets them vote. If 4 of the 5 nearest are apples, it predicts apple. For regression, it averages the neighbours' values instead of voting.

How it works: distance and voting

KNN needs a notion of 'closeness'. The usual choice is Euclidean distance — straight-line distance in feature space:

distance(a, b) = sqrt( Σ (a_i - b_i)² )

The algorithm is three steps: (1) compute the distance from the query point to every training point, (2) keep the K smallest, (3) return the majority class among them. There is no model to train — hence 'lazy'. The cost is paid at prediction time, which can be slow on large datasets.

Choosing K

K is the one hyperparameter and it controls the bias–variance trade-off:

  • Small K (e.g. 1) — flexible but noisy; a single mislabelled neighbour flips the prediction. High variance.
  • Large K — smoother, more stable, but can blur the boundaries between classes. High bias.
  • Rule of thumb: try odd values (to avoid ties in binary problems) and pick K by cross-validation.

Why feature scaling is essential

Because KNN is built on distance, a feature measured in large units dominates the calculation. Imagine mixing salary (0–100000) with age (0–100): salary differences swamp age differences entirely, so 'nearest' is decided almost solely by salary. Always standardise or normalise your features before using KNN. This is the single most common KNN mistake.

Implementing it in Python

We recognise handwritten digits and sweep over K:

from sklearn.neighbors import KNeighborsClassifier
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import accuracy_score

X, y = load_digits(return_X_y=True)
Xtr, Xte, ytr, yte = train_test_split(X, y, test_size=0.2, random_state=42)

# Scaling is ESSENTIAL for KNN — it is a distance-based method.
scaler = StandardScaler().fit(Xtr)
Xtr, Xte = scaler.transform(Xtr), scaler.transform(Xte)

for k in [1, 3, 5, 9]:
    knn = KNeighborsClassifier(n_neighbors=k).fit(Xtr, ytr)
    acc = accuracy_score(yte, knn.predict(Xte))
    print(f"k={k}: accuracy={acc:.4f}")

Output:

k=1: accuracy=0.9778
k=3: accuracy=0.9694
k=5: accuracy=0.9750
k=9: accuracy=0.9722

Here k=1 just edges the others, with k=5 close behind — on a different train/test split a slightly larger K often wins, which is exactly why you tune K empirically rather than guessing. One caveat on scaling: the digits are all pixel intensities on the same 0–16 scale, so the StandardScaler changes little here — but on data with mixed units (say age and salary) that step is the difference between a working model and a broken one.

The curse of dimensionality

KNN's Achilles' heel: as the number of features grows, points spread out and everything becomes roughly equidistant, so 'nearest' loses meaning. Distances concentrate and the vote becomes uninformative. Mitigations: reduce dimensions first (PCA or feature selection), or use a distance metric suited to the data. On very high-dimensional data (like raw text or images), KNN usually underperforms tree ensembles or neural nets.

Strengths and weaknesses

Strengths: dead simple, no training time, naturally handles multi-class problems, and makes no assumption about the shape of the data.

Weaknesses: slow and memory-hungry at prediction time (it stores everything), highly sensitive to scaling and irrelevant features, and degrades in high dimensions. For speed on big datasets, approximate-nearest-neighbour indexes (KD-trees, ball trees, or HNSW) help.

Key takeaways

  • KNN classifies by majority vote of the K closest training points — no training phase.
  • K trades off noise (small K) against over-smoothing (large K); pick it by cross-validation.
  • Feature scaling is mandatory because KNN is distance-based.
  • It struggles in high dimensions and on large datasets, but shines as a simple, transparent baseline.

Further reading: scikit-learn neighbors and the visual guide.