K-Means Clustering
Unsupervised grouping of data points.
For a small example, six coordinates form two visible groups. Choose initial centers, assign by distance, recompute means, and repeat until assignments stop changing.
Unsupervised grouping of data points.
For a small example, six coordinates form two visible groups. Choose initial centers, assign by distance, recompute means, and repeat until assignments stop changing.
Customers differ in frequency, value, and recency. Scale features, compare k with stability and domain usefulness, inspect centroids, and avoid treating clusters as natural truth.
Initializing Interactive Playground...
Predict which centroid the outlined point will join and where that centroid will move.
The starter deliberately follows a tempting but flawed assumption: Choosing k only from an elbow plot
Check: The browser checks pass and you can name the failure mechanism before opening the explanation.
Failing starter lab
Assign points to the nearest centroid and recompute each centroid as an assigned-point mean.
Run the incomplete starter before opening hints. Use the first wrong output or failed check to form a repair hypothesis.
Explanations after each answer · 80% to pass · K-Means Clustering
Chapter 1 of 5 · Core
Clustering is the primary unsupervised learning task, aimed at partitioning an unlabeled dataset into distinct homogeneous groups. Formally, given a dataset where , the objective is to partition the samples into disjoint subsets (where and for ).
The quality of a partition is measured by the Within-Cluster Sum of Squares (WCSS), also known as the K-means objective function: where is the representative prototype (centroid) of cluster .
NP-Hardness & Combinatorial Complexity: To find the global minimizer of , one could theoretically evaluate all possible partitions of elements into non-empty sets. The number of such partitions is given by the Stirling numbers of the second kind: For extremely modest datasets (e.g., ), , which exceeds the computational capabilities of any physical computer. Thus, global minimization of the WCSS objective is NP-hard in Euclidean space, even for in arbitrary dimensions, or for in arbitrary cluster counts. We must therefore rely on iterative, heuristic optimization algorithms.
Key equations
K-means Clustering
StatQuest
Watch assignment and centroid updates converge, then consider initialization and cluster-shape limits.
Primary sources and free technical reading for K-Means Clustering.
Free R and Python editions with worked labs for regression, classification, trees, support vectors, and clustering.
Official algorithms, examples, and evaluation guidance for K-means and alternative clustering methods.