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
from sklearn.neighbors import KNeighborsClassifier
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
X = [[0, 0], [1, 0], [0, 1], [1, 1]]
y = ["A", "A", "B", "B"]
model = make_pipeline(StandardScaler(), KNeighborsClassifier(n_neighbors=3))
model.fit(X, y)
print(model.predict([[0.8, 0.1]])[0])
Output
Try it in the visualizer
Set Neighbors (K) to 1 and move the query point. Notice how easily its class changes near individual training points.
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.
You set K = 1 and the decision boundary becomes jagged and unstable. That is a symptom of:
With K = 1 every single point, including mislabelled ones, gets its own territory. High variance and a jagged boundary are the classic signature of overfitting.
Why does KNN need its features scaled?
Distance sums squared differences. A salary in the tens of thousands swamps an age in the tens, so the model quietly becomes 'nearest by salary' no matter what you intended.
KNN is called a lazy learner because:
Training is just storing the dataset. All the cost lands on prediction, when it must measure the new point against every stored one - which is why KNN is expensive to serve at scale.
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.
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.