AI GLOSSARY · AI CONCEPTS
k-nearest neighbors (k-NN)
Definition
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.
Worked examples in this catalogue
Catalogue examples point at a skill, guide or coding tool listed on this site, with what to run and what you see. An example marked illustrative describes the item from its own documentation; it was not executed for this page. Platform Scores are our assessment under a published method, not a safety guarantee.
Skill by Skill Almanac · Illustrative, not executed
split-leakage-check 53/100 · Provisional, not yet measuredScored · Method v0.2 · How we score
- 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.
Skill by Skill Almanac · Illustrative, not executed
mock-record-generator 42/100 · Provisional, not yet measuredScored · Method v0.2 · How we score
- 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.
Skill by Skill Almanac · Illustrative, not executed
data-analysis-eda 56/100 · Provisional, not yet measuredScored · Method v0.2 · How we score
- 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.
How it shows up when you use agents
k-NN is a classic 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 does a similar nearest-neighbour lookup over embeddings, 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 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.
Related terms
Terms that mention k-nearest neighbors (k-NN)
4 terms mention this one
More in AI concepts
Sources
- 1.6. Nearest Neighbors, scikit-learn (accessed 2026-11-24)
- What is the k-nearest neighbors (KNN) algorithm?, IBM (accessed 2026-11-24)
Definitions are written by Skill Almanac from these sources and were checked against them on 2026-11-25. Wikipedia is never the only source.
Review record
Last verified
Drafted by claude-sonnet-5-5; sources and example targets checked by Independent automated source review (OpenAI Codex); not human peer review on 2026-11-25. Last verified .
Change log (4)
- : Independent automated factual/example review completed. Cached US query estimate aligned to receipt; no traffic promise. Final technical gate recorded separately; publication disabled.
- : First version.
- : Verifier pass: applied wording corrections from the verification list.
- : Automated review completed; approved for the staged public release. Search indexing is controlled by the release configuration.