k-NN Classifier (2D)

Click to add labelled points, then click again to query — the k nearest neighbours vote on the class.

About K-Nearest Neighbours Classifier (2D)

This 2D companion view shows the classic textbook representation of the k-nearest-neighbours (KNN) algorithm: a scatter plot of labelled training points with a colour-coded decision-region background. Click to drop training points in class A, B, or C, then place a query point to see it classified by a majority vote among its k closest neighbours (Euclidean distance). The tinted background is computed by classifying a grid of points across the whole canvas with the same rule, so the decision boundary between classes is visible everywhere at once, not just at the query point.

KNN is a non-parametric, instance-based ("lazy") learning algorithm — it stores the training data and defers all computation to prediction time, rather than fitting an explicit model. Small k values produce a jagged, overfit boundary sensitive to individual points; large k values smooth the boundary but can blur genuine structure. Try adding an outlier point and watch how much k=1 reacts to it compared with k=15.