Intro
K-Nearest Neighbors is a supervised machine learning algorithm for classification and regression that finds the “k” closest data points
![]()
Application of a k-NN classifier considering k = 3 neighbors. Left - Given the test point ”?”, the algorithm seeks the 3 closest points in the training set, and adopts the majority vote to classify it as “class red”. Right - By iteratively repeating the prediction over the whole feature space (X1, X2), one can depict the “decision surface”
Algorithm
How it works
-
Choose K (number of neighbors)
-
Compute distance (e.g. Euclidean, Manhattan, or cosine (often for embeddings)) between the new point and all training points
-
Select the K nearest points
-
Aggregate:
- Classification → majority vote
- Regression → average of values




