# k-nearest neighbors (k-NN)

> Canonical: https://skillalmanac.com/glossary/k-nearest-neighbors | Last verified: 2026-11-25 | Index status: index-eligible

k-nearest neighbors (k-NN) is a supervised method that predicts for a new item by looking at the k most similar items among stored examples and taking a majority vote, or an average for numbers.

Also called: kNN, k-NN, k-nearest neighbor, knn algorithm, k nearest neighbors algorithm.

## In plain language

k-nearest neighbors works like asking the neighbours. To label a new item, it finds the k most similar items it has already seen, where k is a number you choose such as 5, and goes with the most common label among them. There is no training step in the usual sense, because it just keeps all the examples. That makes it easy to understand, but it gets slower and needs more memory as the data grows, and a poor choice of k or of what counts as similar can give poor answers.

## Technical definition

k-NN is an instance-based, non-generalizing method: according to the scikit-learn guide it does not construct a general internal model but stores instances of the training data. For a query point it computes distances to stored points, for example Euclidean distance, selects the k nearest and returns the majority class, or for regression an average. Weights can be uniform or proportional to the inverse of distance. In the guide the best k is data-dependent: a larger k suppresses the effects of noise but makes class boundaries less distinct. Brute-force search, KD trees and ball trees are used to find neighbours, and the guide notes KD trees become inefficient as dimension grows very large.

## How it shows up when you use agents

k-NN is a classic [supervised learning](/glossary/supervised-learning) method that an agent may write when you ask for a quick baseline on a table of labelled rows. It also shares an idea with modern tooling: finding the stored items closest to a query. [Semantic search](/glossary/semantic-search) does a similar nearest-neighbour lookup over [embeddings](/glossary/embedding), though it is a retrieval system and not a classifier.

The catalogue has no skill that trains models, so the examples cover the checks around one. Because k-NN stores every training row, a leaked or duplicated row can make a test score look perfect: a test row finds its own copy as its nearest neighbour. Run a leakage check before trusting a score. Because distance drives the result, a column measured in large units can dominate the others, so ask the agent whether features were scaled. A next action is to try several values of k and compare them on held-out data.

## Common confusions

- **k-NN versus k-means.** k-NN is supervised and needs labels. [k-means](/glossary/k-means) is unsupervised and groups unlabelled data. The k means a different number in each.
- **No training versus no cost.** Storing the data is cheap, but IBM notes k-NN takes up more memory and data storage than other classifiers, and each prediction does the search work.
- **Bigger k versus better.** A larger k smooths out noise but blurs class boundaries, according to the scikit-learn guide. The best value depends on the data.
- **Neighbour search versus semantic search.** Vector search uses a nearest-neighbour idea over embeddings to retrieve items. It does not vote on a label.

## Worked examples in this catalogue

### [split-leakage-check](https://skillalmanac.com/skills/skill-almanac/split-leakage-check)

Status: illustrative example from the item's own documentation, not executed for this page.

What you see: A JSON report that checks train, validation and test files for duplicates, shared groups, time leakage and label leakage.

Why it exercises the concept: The catalogue has no skill that trains this algorithm. This one covers the step that decides whether a score from any trained model can be trusted: whether the test rows were kept apart from the training rows.

### [mock-record-generator](https://skillalmanac.com/skills/skill-almanac/mock-record-generator)

Status: illustrative example from the item's own documentation, not executed for this page.

What you see: Seeded mock records written as CSV or JSON from a JSON field list or a simple TypeScript interface.

Why it exercises the concept: The catalogue has no training skills. Seeded mock rows give you a small, repeatable table to try a scikit-learn example on without touching real data.

### [data-analysis-eda](https://skillalmanac.com/skills/skill-almanac/data-analysis-eda)

Status: illustrative example from the item's own documentation, not executed for this page.

What you see: A fixed exploratory-analysis procedure and a CSV profiler, with findings kept descriptive, evidenced and caveated.

Why it exercises the concept: The catalogue has no training skills. Profiling a table first shows the column types, gaps and spread that decide whether this method is a sensible choice for the data.

## Related terms

- [Supervised learning](https://skillalmanac.com/glossary/supervised-learning.md)
- [Machine learning (ML)](https://skillalmanac.com/glossary/machine-learning.md)
- [Classification](https://skillalmanac.com/glossary/classification.md)
- [Embedding](https://skillalmanac.com/glossary/embedding.md)
- [Cosine similarity](https://skillalmanac.com/glossary/cosine-similarity.md)
- [scikit-learn](https://skillalmanac.com/glossary/scikit-learn.md)

## Sources

- [1.6. Nearest Neighbors](https://scikit-learn.org/stable/modules/neighbors.html), scikit-learn (accessed 2026-11-24)
- [What is the k-nearest neighbors (KNN) algorithm?](https://www.ibm.com/think/topics/knn), IBM (accessed 2026-11-24)

Definitions are written by Skill Almanac from the sources above and checked against them on 2026-11-25. [Glossary index](https://skillalmanac.com/glossary.md).
