1. Home
  2. AI & Machine Learning
  3. K-Nearest Neighbours (KNN)

K-Nearest Neighbours (KNN)

Classify a new point by asking its k closest neighbours to vote. See the distances, the neighbourhood bubble and the vote in 3D.

Interactive 3DBeginner10 min readAI/MLUpdated

Drag to rotate · Right-drag to pan · Click, then scroll to zoom · Space play · Left arrowRight arrow step

What's happening

K-Nearest Neighbours: to label a new point, look at the k most similar (closest) labelled points and let them vote. Each point here has 3 features, so it lives in 3D. Press Classify.

Pseudocode

  1. procedure classify(query, k):
  2. for each training point p:
  3. d[p] ← distance(query, p) // usually Euclidean
  4. neighbours ← the k points with the smallest d
  5. return the most common class among the neighbours
Read this model as text · 5 steps

One run of the model above, written out step by step. Useful with a screen reader, on a slow connection, or for quick revision. The values may differ from the run on your screen.

  1. K-Nearest Neighbours: to label a new point, look at the k most similar (closest) labelled points and let them vote. Each point here has 3 features, so it lives in 3D. Press Classify.
  2. A new, unlabelled point (◆) arrives. Which class does it belong to? KNN has no training step — it simply looks at the data.
  3. Measure the straight-line (Euclidean) distance from the new point to every one of the 54 training points.
  4. Keep the k = 5 closest ones (inside the bubble, radius 1.92). Distances: 0.73, 0.87, 1.59, 1.66, 1.92.
  5. Votes — Class A: 0, Class B: 5, Class C: 0. The majority wins: the new point is Class B.

Try this in the 3D model

  • Press Classify with k = 1, then k = 9 on the same point. Does the answer change?
  • Press New query point a few times — which points are hardest to classify?
  • Rotate the scene — a point that looks close from the front may be far away in depth!

The idea

“Tell me who your friends are, and I’ll tell you who you are.”

K-Nearest Neighbours classifies a new example by finding the k most similar labelled examples and letting them vote:

  1. Measure the distance from the new point to every training point.
  2. Pick the k closest.
  3. The most common class among them is the prediction.

That’s the whole algorithm. There is no training phase — KNN just remembers the data (it is a lazy learner).

In the 3D model, each point has three features, so it lives in 3D space. The ◆ is the new point; the bubble encloses its k nearest neighbours.

Measuring “close”: distance

The usual choice is Euclidean distance — the straight-line distance:

d(p, q) = √( (p₁ − q₁)² + (p₂ − q₂)² + (p₃ − q₃)² )

Other options: Manhattan distance (sum of absolute differences), or cosine similarity for text.

Scale your features!

If one feature is “salary” (thousands) and another is “age” (tens), salary dominates the distance completely. Always standardise features (subtract the mean, divide by the standard deviation) before KNN.

Choosing k

k Behaviour
Small (1–3) Very flexible, follows every wiggle — sensitive to noise (overfitting)
Large Smooth, stable decisions — may ignore small groups (underfitting)

Pick k with cross-validation, and prefer an odd k for two classes to avoid ties. A common variation weights each vote by 1/distance, so closer neighbours count more.

Code

import numpy as np
from collections import Counter

def knn_predict(X_train, y_train, query, k=5):
    dists = np.linalg.norm(X_train - query, axis=1)   # distance to every point
    nearest = np.argsort(dists)[:k]                     # indexes of the k closest
    return Counter(y_train[nearest]).most_common(1)[0][0]

X = np.array([[1, 2, 1], [2, 1, 1], [1, 1, 2], [6, 5, 6], [5, 6, 5], [6, 6, 5]])
y = np.array(["A", "A", "A", "B", "B", "B"])
print(knn_predict(X, y, np.array([2, 2, 2]), k=3))   # A

With scikit-learn (including scaling):

from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier

model = make_pipeline(StandardScaler(), KNeighborsClassifier(n_neighbors=5)).fit(X, y)
print(model.predict([[2, 2, 2]]))

The curse of dimensionality

In very high dimensions (hundreds of features), almost all points end up roughly the same distance apart, so “nearest” stops meaning much. KNN works best with a modest number of meaningful features — often after PCA.

KNN vs K-Means

They sound alike but are different:

KNN K-Means
Type Supervised (needs labels) Unsupervised (no labels)
k means Number of neighbours that vote Number of clusters
Task Classify a new point Group the data

Where is it used?

Recommendation systems (“customers similar to you bought…”), handwriting recognition, anomaly detection, and as a quick, strong baseline for small datasets.

Common mistakes

  • Not scaling features.
  • Using KNN on huge datasets without an index — every prediction scans everything.
  • Confusing KNN with K-Means.

Complexity at a glance

Case / operationTimeWhy
TrainingO(1)Just store the data ("lazy learning").
Prediction (brute force)O(n · d)Distance to every training point.
Prediction with a k-d tree (low d)about O(log n)
Extra spaceO(n · d)

Quick check

Test yourself — pick an answer to see if you got it.

1. What happens in the training phase of KNN?

2. With k = 1, the prediction is…

3. Why should features be scaled before using KNN?

4. Why is an odd k often chosen for two classes?

Saved in this browser.
Spotted a mistake or a bug in the 3D model?

Report a mistake

in K-Nearest Neighbours (KNN). Thank you — every report makes the lesson better for the next reader.

We'll also include a link to the step of the 3D model you're on and your browser type, so we can reproduce it.