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.

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


By Leodanis Pozo Ramos • Updated July 2, 2026