K-Nearest Neighbors
AvailableClassify 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
- 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
- Metric
- Euclidean
- Scaling
- Off
Count of neighbors per class among the selected k.
Nearest neighbors
| # | x | y | Class | Distance |
|---|---|---|---|---|
| 1 | -0.50 | 0.30 | Class 0 | 0.583 |
| 2 | -0.08 | 0.93 | Class 0 | 0.933 |
| 3 | -0.29 | -1.39 | Class 2 | 1.420 |
| 4 | 0.90 | -1.68 | Class 2 | 1.906 |
| 5 | -0.89 | 1.74 | Class 0 | 1.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.
Standard deviation of each group's Gaussian noise.
Stretches x to simulate a feature measured in a different unit than y.
Classification
Number of neighbors queried.