AI & LLMs

K-Means

also: k-means clustering

A clustering algorithm that partitions data into k groups by iteratively assigning points to the nearest center.

K-means is an unsupervised clustering algorithm that partitions data into k groups: it picks k initial centers, assigns each point to its nearest center, recomputes each center as the mean of its points, and repeats until stable. It finds natural groupings without labels.

Worked example: to segment customers into 4 groups by behavior, k-means iteratively moves 4 centroids until each customer belongs to the nearest one and the centroids stop moving — yielding 4 clusters you can then profile. Gotcha: you must choose k in advance (the “elbow method” helps), results depend on the random initialization (k-means++ improves it), and it assumes roughly spherical, equal-size clusters — so it fails on elongated or nested shapes, where density-based methods like DBSCAN work better.