# k-Nearest Neighbors (k-NN)

> It finds the closest cases it knows. Then it copies what most of them say.

**Field:** Artificial Intelligence  
**Category:** Machine Learning  
**Also known as:** k-NN, KNN, Nearest Neighbor Classifier

## Think of it like

Think of it like a new kid at school. You guess their team from the three kids next to them.

## Definition

k-nearest neighbors answers by looking at the closest examples it has stored. For a new example, it finds the k closest ones in the training data, where k is a small number you pick, like 5. If most of those neighbours are in one group, the new example goes in that group too. It does almost no work during training. All the work happens when a question comes in.

## Example

Jack sets k to 3. A new fruit sits nearest to two apples and one pear on his chart, so his model calls it an apple.

## Why it matters

It is simple and needs no real training. The same idea of finding the nearest items helps power search and suggestions.

## Common mistake

Using it on huge data with no help. Checking each new case against millions of stored ones is slow.

## Three principles

- **Find the closest.** The k nearest examples.
- **Copy the majority.** Most neighbours decide.
- **Lazy learner.** Work happens at question time.

## Related terms

- [k-Means Clustering](https://learn.benibix.com/term/ai/k-means-clustering)
- [Classification (Machine Learning)](https://learn.benibix.com/term/ai/classification)
- [Recommendation System](https://learn.benibix.com/term/ai/recommendation-system)

## Told apart from

- [k-Means vs k-Nearest Neighbors](https://learn.benibix.com/compare/k-means-vs-k-nearest-neighbors): Both have a k and both measure closeness, so people often swap them. They do different jobs. In k-means, k is how many groups to find in data with no labels. In k-nearest neighbors, k is how many labelled neighbours vote on a new example's group.

## Taught in

- [k-NN, SVM and Naive Bayes](https://learn.benibix.com/learn/ai-machine-learning/knn-svm-and-naive-bayes): Machine Learning, 9/18

![Coloured dots and grey dots on a chart, and one new hollow dot. A circle around the new dot holds its three nearest dots, two coloured and one grey, so the new dot is called coloured.](https://learn.benibix.com/diagrams/ml-knn.svg)

---

Source: https://learn.benibix.com/term/ai/k-nearest-neighbors
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-17
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
