# k-Nearest Neighbors

*noun*

say it: **“KAY NEER-ist NAY-berz”** **/ˈkeɪ ˈnɪrəst ˈneɪbərz/**

A method that classifies a new example by finding the k most similar examples it already knows and taking their most common answer.

## What is **k-nearest neighbors**?

**k-nearest neighbors** (k-NN) guesses by finding the k training examples closest to a new one, by distance, and
taking their most common label, or their average for a number. It learns nothing ahead of time: it keeps the examples
and compares when it guesses. scikit-learn's is `KNeighborsClassifier`.

```python
from sklearn.neighbors import KNeighborsClassifier
fruit = [[120, 3], [125, 4], [130, 3], [180, 8], [185, 9], [190, 8]]   # grams, sweetness
names = ["lemon", "lemon", "lemon", "orange", "orange", "orange"]
knn = KNeighborsClassifier(n_neighbors=3).fit(fruit, names)
print(knn.predict([[128, 4]]))   # ['lemon']: its 3 nearest are lemons
```

## In a sentence

- “**k-Nearest neighbors** needs scaled features, because it decides by distance.”
- “With k set to 5, **k-nearest neighbors** looked at the five most similar fruits and took a vote.”

Level: Journeyman · [More AI Basics terms at this level](https://www.ask-codey.com/glossary?category=ai&level=3)

Updated: November 19, 2026

Source: https://www.ask-codey.com/glossary/k-nearest-neighbors
