Skip to content

nearest neighbor

A nearest neighbor is the data point in a reference set that has the smallest distance to a query point under a specified metric.

In machine learning and search, this idea extends to k-nearest neighbors (k-NN), where tasks such as classification and regression use the labels or target values of the k closest points.

Implementations typically compute distances, such as Euclidean or Manhattan, and find neighbors either by brute-force scanning or by using spatial index structures like KD-trees or Ball Trees, depending on the dataset, dimensionality, and chosen metric. In the high-dimensional embedding spaces used by AI tooling, those exact methods stop paying off, so vector databases use approximate nearest neighbor (ANN) indexes such as HNSW, which trade a little accuracy for much faster lookups.

The k-Nearest Neighbors (kNN) Algorithm in Python

Course

Using k-Nearest Neighbors (kNN) in Python

In this video course, you'll learn all about the k-nearest neighbors (kNN) algorithm in Python, including how to implement kNN from scratch. Once you understand how kNN works, you'll use scikit-learn to facilitate your coding process.

intermediate algorithms data-science machine-learning

For additional information on related topics, take a look at the following resources:


By Leodanis Pozo Ramos • Updated Aug. 23, 2026