kmeans() — k-Means clustering

Groups points into k clusters around moving centres.

kmeans(k, X)

Key idea

Definition

how many clusters you ask for
the points in cluster j
the centroid (mean) of cluster j
squared distance to the centroid

In plain words

Drop k flags on a map, send each town to its nearest flag, then move each flag to the middle of its towns. Repeat until the flags stop moving.

Where you'd use it

Frequently asked questions

How do I choose k?
Try several and use the elbow or silhouette; results also vary with the random starts, so run it a few times.