Return to Algorithms

K-Means Clustering Visualization

Cluster 1 Cluster 2 Cluster 3 Cluster 4
Centroids

Controls

Data Generation

5.0

K-Means Parameters

Visualization Options

Algorithm Status

Current Iteration: 0
Inertia (Sum of Squared Distances): -
Silhouette Score: -
Status: Not Started

Elbow Method

How K-Means Clustering Works

K-Means is an iterative algorithm that partitions data into K distinct clusters by minimizing the within-cluster sum of squares (inertia).

The Algorithm:

  1. Initialization: Place K centroids randomly or using K-Means++ (more strategic placement)
  2. Assignment Step: Assign each data point to the nearest centroid, forming K clusters
  3. Update Step: Recalculate the position of each centroid as the mean of all points in its cluster
  4. Iteration: Repeat Assignment and Update steps until convergence or max iterations

Key Concepts:

  • Inertia: Sum of squared distances from each point to its assigned centroid (lower is better)
  • Silhouette Score: Measures how similar points are to their own cluster compared to other clusters (higher is better)
  • Elbow Method: Plots inertia against different K values to find the "elbow point" where adding more clusters yields diminishing returns

Limitations:

  • Assumes clusters are roughly spherical and equal in size
  • Sensitive to initial centroid placement
  • Requires specifying K in advance (hence the elbow method)
  • Can be trapped in local minima