Home / Classification

K-Nearest Neighbors

Drag the target point to see how the KNN algorithm classifies it based on proximity.

Parameters

3
5

Visualization

Drag the target circle to classify.

Real-time Analysis

Result: Based on the 5 nearest neighbors, the target is classified as:

Predicted Class
--

Neighbor Votes

Overview

K-nearest neighbors (KNN) classifies a new example by finding the k closest labelled training examples and taking their majority class. For regression, it averages the neighbors' target values. The method stores the training data instead of fitting a compact formula, so most of its work happens when a prediction is requested.

Find neighbors, then combine their labels

For each training point, KNN computes a distance from the query, selects the k smallest distances, and combines their targets. With k = 3, suppose the nearest labels are A, B, A: the predicted class is A. The visualizer shows those neighbors and the vote as you move the query point.

Distance is part of the model. Euclidean distance is common for continuous features; Manhattan distance sums absolute differences. For either, a feature with a much larger numeric range can dominate the result unless the features are scaled.

Choose k using held-out data

Small k makes predictions sensitive to individual points. At k = 1, a training point predicts itself, so a perfect training score says little about performance on new data. Larger k smooths the boundary, but an excessively large value can wash out a small local class. Compare candidate values with cross-validation, then report performance on a separate test set. An odd k can avoid a tie in a two-class, unweighted vote; it does not prevent ties in every multiclass setting.

Scale features before measuring distance

If age ranges from 18 to 80 while income ranges from 20,000 to 200,000, raw Euclidean distance is likely to be driven mostly by income. Standardise or otherwise scale numeric features so their units do not silently decide which neighbors count. Fit the scaler on the training split only, then apply that fitted transformation to validation and test data; fitting it on all rows leaks information. See feature scaling and machine-learning pipelines.

Scaling does not solve every distance problem. Irrelevant features can obscure nearby points, and in high dimensions distances often become less useful for distinguishing neighbors. Feature selection or dimensionality reduction may help, but validate the full workflow rather than assuming it will.

A small scikit-learn example

This example scales both features, fits a three-neighbor classifier, and predicts a query. The labels of its three closest rows are A, A, and B, so the vote returns A.

example_01.pyscikit-learn
Output

Try it in the visualizer

  1. Set Neighbors (K) to 1 and move the query point. Notice how easily its class changes near individual training points.
  2. Set Neighbors (K) to 20 and move it again. The boundary is smoother; a small local group can now be outvoted by the wider neighborhood.

KNN is useful for small, well-scaled datasets where local similarity is meaningful and a simple baseline is valuable. It can be slow at prediction time because it compares a query with stored examples, and it needs memory for those examples. High-dimensional, large, imbalanced, or latency-sensitive problems may need a different model or a dedicated nearest-neighbor index.

Further reading

The scikit-learn nearest-neighbor guide documents distance choices and implementation options. Continue with classification metrics to see how to evaluate the predictions.

Check yourself

0 of 3

Answer without scrolling back up.

  1. You set K = 1 and the decision boundary becomes jagged and unstable. That is a symptom of:

  2. Why does KNN need its features scaled?

  3. KNN is called a lazy learner because:

Cheat sheet

K-Nearest Neighbors

Drop a new point on the plane and watch KNN classify it by neighbour vote. Change k to see the decision boundary tighten or smooth.

MACHINE LEARNING · vizlearn.in/machine_learning/knn.html

About the author

Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.