ML Playground
ClassificationBeginnerSupervised learning

K-Nearest Neighbors

Available

Classify a new point based on its nearest neighbors and see the effect of k.

Description

Qué haceClassifies a new point by copying the majority class among its k most similar neighbors, without fitting any parameters beforehand.

Para qué sirveQuick to implement classification when there's enough example data on hand and the relationship between features and class is too irregular to model with a formula.

Classifies a new point by voting the majority class among its k nearest neighbors. Synthetic dataset.

Voting among neighbors

Lazy learning

No training: the “model” is the entire dataset, and all the work happens at classification time.

Inference cost

O(n) + O(n log n)

Computing and sorting distances against the n training points, per query.

Tie-breaking:In a vote tie, the class of the single closest neighbor wins — never iteration order. See the “Vote tie” case.

What each metric shows

Prediction / effective k

The majority class and how many neighbors actually took part (less than k only if the dataset has fewer points).

Votes

How many of the k neighbors belong to each class.

Scaling:Without standardizing, the feature with the largest range dominates the distance even if it isn't more relevant — see the “Different scales” case.

Strengths and limits

Arbitrary boundaries

Non-parametric: learns complex shapes without assuming a function — see “Concentric circles.”

Cost grows with n

Every prediction scans the whole dataset, which must be kept in memory.

Sensitive to k

Small k chases noise (high variance); large k over-smooths (high bias) — see “Small k” / “Large k.”

Sensitive to scale

Depends on a geometric distance: standardizing is almost always necessary.

Playground

Training points, query point, and decision boundary-6.3-3.00.33.77.0-7.2-3.8-0.33.16.5xy
120 training points across 3 classes. With k=5, the query point (0.00, 0.00) is classified as class 0.
  • Class 0
  • Class 1
  • Class 2
  • Query point

The 5 neighbors used in the vote are marked with an accent ring and a dashed line to the query point. The shaded region uses each class's color to show the decision boundary.

Classification result

Prediction
Class 0
Effective k
5 / 5
Votes
class 0: 3 · class 2: 2

Count of neighbors per class among the selected k.

Metric
Euclidean
Scaling
Off

Nearest neighbors

Nearest neighbors sorted by ascending distance, with their class and whether their vote matches the final prediction.
#xyClassDistance
1-0.500.30Class 00.583
2-0.080.93Class 00.933
3-0.29-1.39Class 21.420
40.90-1.68Class 21.906
5-0.891.74Class 01.954

Dataset generation

Changing these controls generates a new synthetic dataset. KNN has no training phase: every change is reflected immediately in the classification.

Seed for the deterministic generator.

40
5120
1.2

Standard deviation of each group's Gaussian noise.

0.24.0
×1.0

Stretches x to simulate a feature measured in a different unit than y.

×1.0×8.0

Classification

5

Number of neighbors queried.

125
Distance metric

Query point

Educational cases

Related concepts

References