What you're seeing
k-NN: for each query, find k nearest training points; predict the majority class.
This example stores the labeled points without fitting model parameters. Each query computes every distance and sorts the results, so this implementation takes O(n log n) time per query. An index is not implemented; approximate-neighbor systems introduce their own speed and recall trade-offs.
- Click left half to add training points.
- Click right half to classify queries.
- Larger k = smoother boundary; smaller k = more local.
Classify by nearby examples
The canvas contains labeled training points and a movable query. Classification sorts the training points by squared Euclidean distance from the query, selects up to k neighbors and counts their labels. Colored points are examples, not learned centroids. The highlighted neighborhood and text output let you inspect how one prediction follows from the current dataset. This direct manipulation tool updates immediately; a timed movie is unnecessary because moving a point or changing k is the experiment.
Build a reproducible dataset
Choose the class used for the next training point. Click the left plot half to add an example, or use Feature X and Feature Y with Add training point for keyboard and touch access. Add query point places a query at those coordinates in the right half; both halves represent the same feature space. Clicking the right half also adds a query. k sets the requested neighborhood size and is normalized to an integer from one through twenty. With fewer examples, only existing neighbors vote. Ties select class A. Clear points removes training and query points; Restore demo recreates the same seeded twenty-point dataset.
Run a controlled vote
Clear the examples and place one class-A point at X 100, Y 100. Place a class-B point at X 200, Y 100, then query X 110, Y 100 with k one. The closer class-A example should determine the prediction. Add another query at X 190 without changing the examples and inspect the changed nearest neighbor. Add several class-B points near X 180 and increase k: a larger neighborhood can change the majority even when the single nearest example remains class A. Use the text counts for the latest query to explain the vote rather than judging colors alone.
Know what is simplified
Coordinates are pixel-space features with equal scale. There is no feature normalization, training/test split, held-out accuracy, distance weighting or fitted probability calibration. Sorting all distances costs O(n log n) per query in this implementation; it is not a spatial-index benchmark. A large k smooths local decisions but can incorporate irrelevant distant examples. Repeated points count as repeated observations, so duplicating a point can bias a vote. An empty training set cannot yield an evidence-based class. These limits make this a transparent geometric lesson, not a validated classifier for customer or medical decisions.