The two steps
The objective, called inertia, is the sum of squared distances from each point to its assigned centroid. Assignment lowers or preserves it; updating to the mean lowers or preserves it. Because the objective can have local minima, different initial centroids can produce different results. Practical implementations run multiple starts and keep the lowest-inertia solution.
Follow a clustering
Set Clusters (K) and choose an Initial Layout. Press the step control to watch points change assignments and centroids move. Increase Points Count to see how the display changes as the dataset grows. Compare Gaussian Blobs with Nested Moons or Concentric Circles: k-means can split curved groups because each cluster is represented by a centroid and nearest-centroid regions.
Choosing k
The elbow method plots inertia for several values of (k). Inertia always decreases as (k) increases, reaching zero if every point gets its own cluster. A bend where additional centroids yield smaller improvements can suggest a useful value, but the bend may be unclear and is not a statistical proof.
Silhouette score compares each point’s distance to its own cluster with its distance to the nearest other cluster. Higher values indicate better separation under that distance measure, but it too favors some shapes and does not establish that clusters are meaningful. Use these diagnostics with domain knowledge and, when relevant, downstream validation.
When k-means fits
It is a reasonable baseline when features are numeric, clusters are roughly compact and similarly scaled, and assigning every point to a cluster is appropriate. Standardize features when measurement units differ. K-means is sensitive to outliers because both squared distances and means can be pulled by extreme points.
It is a poor match when clusters have very different densities or sizes, are strongly non-convex, or contain substantial noise. Consider DBSCAN for density-connected shapes and noise, or hierarchical clustering when examining multiple merge levels is useful.
Common mistakes with K-Means Clustering
- Treating (k) as discovered: it is supplied by the user; choose and justify it.
- Reading lower inertia as proof of better clusters: it almost always improves with larger (k).
- Skipping feature scaling: large-unit features can dominate distance.
- Trusting one initialization: run multiple starts and inspect stability.