Machine Learning

k-Nearest Neighbors (k-NN)

The k-nearest neighbors (k-NN) algorithm predicts the label or value of a new record from the k most similar records with known outcomes, using their majority vote or their average.

What is k-nearest neighbors (k-NN)?

The k-nearest neighbors (k-NN) algorithm classifies a new record by finding the k labeled records most similar to it and taking a majority vote. For a numeric prediction, it averages their values instead. The team chooses k, for example 5 or 15. Small values follow the data closely and react to noise, while larger values give smoother answers.

k-NN does no training in the usual sense. It stores the labeled records and does its work at prediction time, measuring the distance from the new record to the stored ones. Adding a labeled example changes the next prediction immediately, which makes the model easy to update. Large collections need a search index, or every prediction becomes slow.

k-NN in practice

A managed service provider routes support tickets by comparing each new ticket with past resolved tickets. Each ticket is converted into an embedding, a numeric vector that captures its meaning. The ten closest past tickets suggest the queue, the likely fix, and the technician who solved similar problems. This is the same nearest-neighbor search that vector databases run for semantic search, with a vote added at the end.

What to watch for

  • Normalize numeric features first, or a field such as annual revenue will swamp a field such as employee count.
  • Distance loses meaning across hundreds of raw features, so apply dimensionality reduction or use embeddings.
  • The stored records are the model, so they need the same access controls as the source data.

← Back to AI Glossary