K-Means Clustering Lab

A hands-on teaching app for exploring how k-means assigns points, moves centroids, converges, and sometimes fails. Change the data, k, initialization, and distance metric, then step through the learning process.

Teaching Notes

1

Initialize: choose k starting centroids. Bad starts can lead to worse local minima.

2

Assign: each point joins the cluster with the closest centroid.

3

Update: each centroid moves to the mean of its assigned points.

4

Stop: repeat until assignments stabilize, centroid movement is tiny, or max iterations is reached.

Iteration
0
Inertia / WCSS
Centroid shift
Empty clusters
0

Cluster Space

Watch each point choose the nearest centroid, then watch centroids move to the average of assigned points.

Ready

Learning Curve

During normal runs this shows inertia over iteration. During the elbow test, the x-axis is k and the dashed marker is the app's best-guess optimal k.

Best k: —

Hierarchical Clustering

Approximate agglomerative clustering with average linkage, drawn as cluster assignments.

Process: start with each point as its own cluster, repeatedly merge the closest pair, then cut the tree at k groups. Teaching angle: compare with k-means when clusters are uneven or when you want a tree of possible groupings.

Spectral Clustering

Graph-style local neighborhood clustering, useful for non-convex shapes.

Process: build a similarity graph, connect nearby points strongly, transform the graph into a low-dimensional spectral space, then cluster there. This lab uses neighbor propagation instead of a full eigenvector solver for intuition.

PCA Projection

Principal component axis and 1D projection score for the current 2D data.

Process: center the data, find the direction with greatest variance, then project points onto that axis. PCA is deterministic and preserves large-scale linear structure.

t-SNE Style Map

A neighbor-preserving nonlinear map approximation for teaching local neighborhoods.

Process: estimate which points are neighbors in high-dimensional space, place points on a map, then iteratively pull neighbors together and push non-neighbors apart. This pedagogical view emphasizes the intuition, not exact t-SNE math.

Related Techniques: What to Use When

K-means is fast and visual, but it is only one member of a broader clustering and dimensionality-reduction toolkit. Use this section to frame why different methods can produce different answers on the same data.

K-means

Algorithm: initialize centroids, assign points, update means, repeat.

  • Pros: fast, simple, scalable.
  • Cons: assumes roughly round clusters; sensitive to k, scale, initialization, and outliers.

Hierarchical

Algorithm: compute distances, merge nearest clusters, read groups from the dendrogram.

  • Pros: no single k needed up front; good explanatory tree.
  • Cons: slower; early merge choices cannot be undone.

Spectral

Algorithm: build a similarity graph, use graph eigenvectors, then cluster in that transformed space.

  • Pros: handles rings, moons, and manifold-like shapes better.
  • Cons: needs similarity choices; can be expensive and less transparent.

PCA / t-SNE

Algorithm: project data to a visual space before or after clustering.

  • PCA: linear, stable, interpretable, variance-focused.
  • t-SNE: nonlinear, neighborhood-focused, not reliable for global distances.