beginneradvanced-ml

K-Means Clustering

Group your data into k neat clusters — you just have to tell it how many to look for.

Key Points

What it is

K-Means is an unsupervised algorithm that groups data into k clusters by repeatedly assigning each point to its nearest cluster center (centroid) and then recalculating the centroids, until the assignments stop changing. It's the most common starting point for clustering.

Why it exists

Real-world data often has natural groupings that aren't labeled — customer segments, document topics, image color palettes. K-Means exists to discover this hidden group structure automatically, using only the geometry of the data itself.

Intuition

Imagine dropping k magnets randomly onto a table scattered with iron filings. Each filing sticks to its nearest magnet. Then you move each magnet to the center of the filings now stuck to it, and let the filings re-stick to whichever magnet is now closest. Repeat this a few times and the magnets settle into the natural centers of the filing clusters.

How it works

  1. Choose k and initialize centroids

    Pick the number of clusters k and randomly place k centroids in the data space.

  2. Assign points

    Assign every data point to its nearest centroid, forming k clusters.

  3. Recompute centroids

    Move each centroid to the mean position of all points currently assigned to it.

  4. Repeat until convergence

    Repeat assignment and recomputation until cluster assignments stop changing or a max iteration limit is reached.

When to use

When not to use

Common mistakes

In practice

Widely used for customer/user segmentation, anomaly detection preprocessing, image compression, and as a quick way to discover structure in unlabeled data before deeper analysis.

Was this concept helpful?

Spot an error or gap? Suggest an edit →

Related concepts

hierarchical-clusteringdbscanclustering-evaluation-silhouette-scorecurse-of-dimensionality
Chirp at me!