Skip to content
IT glossary
Data and MLUpdated December 3, 2026

k-nearest neighbors

Метод k ближайших соседей

A machine learning algorithm that predicts an outcome using the k most similar examples in its training data. It votes on a category or averages known values to predict a number.

Also known as: kNN

The idea behind kNN

Suppose you want to estimate the rent of a new listing. Rather than fitting a single formula for the whole city, you could find a few comparable apartments and average their rents. That's the idea behind k-nearest neighbors (kNN): make a prediction using examples near the query, where “near” is defined by their features.

kNN is supervised learning because those examples come with known answers. In its simplest form, fitting means storing the training data rather than learning a set of coefficients. Most of the work happens when a prediction is requested, which is why it's called a lazy learning method.

How a prediction is made

Represent each example by its features, such as floor area and walking time to a station. Choose a distance measure: Euclidean distance, for example, is the straight-line distance between two points in feature space. Find the k training examples with the smallest distances to the query. For classification, choose the class with the most votes. For regression, take the mean of the neighbors' target values. Either version can give closer neighbors more weight.

A worked example

Say k = 3 and the three closest apartments rent for $1,400, $1,500, and $1,750 per month. With equal weights, the prediction is $1,550. If the $1,400 apartment is the closest and you change to k = 1, the prediction becomes $1,400. A single neighbor can capture a very local pattern, but an unusual listing can also swing the result.

When it works and what to watch for

kNN makes a useful baseline when you have a modest dataset and a meaningful way to compare examples. The same neighbor search can retrieve similar items for a recommender, though retrieval alone returns items rather than predicting a label or value.

  • Feature scale matters. Measuring floor area in square feet instead of square meters can change which apartment is closest, even though the apartments haven't changed. Scaling features can help. Fit the scaler on training data only, and refit it within each training fold during cross-validation.
  • There is no universal best k. Small values are sensitive to noise; large values smooth out noise but can wash out local patterns or minority classes. Use validation data or cross-validation to choose k and the distance measure. Keep the test set for the final evaluation.
  • More features aren't always better. In high-dimensional spaces, distances can become less informative. Check whether removing irrelevant features or reducing dimensionality improves validation performance.

How it comes up in interviews

Be ready to explain the cost of an exact brute-force query. With n training examples and d features, computing all Euclidean distances takes O(nd) time. Sorting all distances adds O(n log n), but you can find the top k without a full sort. A common follow-up is how to break tied votes: an odd k rules out equal vote counts in binary classification with equal weights, but it doesn't prevent ties when there are three or more classes.