Skip to content
BENIBIX Learn

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.

AI📈 Machine Learning

k-Means Clustering

It splits data into k groups. It moves k centre points until they stop.

k-means clustering is a method that splits data into k groups, where k is a number you choose. It starts by dropping k centre points at random. Each example joins the group of its nearest centre. Then each centre moves to the middle of its group. These two steps repeat until the centres stop moving.

Example
Emma runs k-means with k set to 4 on her customers' spending. It finds four groups, from rare small spenders to weekly big spenders.
Why it matters
It is one of the simplest and fastest ways to find groups in data that has no labels.
Full entry
AI📈 Machine Learning

k-Nearest Neighbors (k-NN)

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

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.
Full entry

In one line

k-Means Clustering
It splits data into k groups. It moves k centre points until they stop.
k-Nearest Neighbors (k-NN)
It finds the closest cases it knows. Then it copies what most of them say.