K-Means

K-means splits data points into clusters. Each point gets an assignment , and each cluster gets a center . K-means chooses both to minimize the within-cluster sum of squares,

where and . Libraries call the inertia. Finding the global minimum is NP-hard, so Lloyd’s algorithm searches for a local minimum. It starts from initial centers and repeats two steps until no assignment changes:

  • Assignment step: each point joins its nearest center, . A tied point keeps its current cluster.
  • Update step: each center moves to the mean of its points, , where counts the points in cluster . An empty cluster gets a new center, often the point farthest from its own center.

Neither step can increase , and there are at most partitions. Lloyd’s algorithm therefore stops after finitely many iterations, at a local minimum that depends on the starting centers. Each iteration costs . Practitioners run several starts and keep the result with the lowest .

k-means++ seeding (Arthur and Vassilvitskii 2007) chooses the starting centers in two steps:

  • First center: a data point chosen uniformly at random.
  • Each next center: a data point drawn with probability proportional to , its squared distance to the nearest chosen center.

The seeding alone gives , where is the optimal objective. Lloyd’s algorithm then improves on it.

The number of clusters is a hyperparameter. Two common rules choose it:

  • Elbow method: plot the best found against , and pick the after which stops falling quickly.
  • Silhouette: for point , let be its mean distance to the other points of its cluster. Let be its smallest mean distance to the points of another cluster. The point’s score is , and a point alone in its cluster gets . The best has the highest mean score.

K-means measures Euclidean distance, so features need comparable scales. It works best on compact, roughly spherical clusters of similar size. It is the hard-assignment limit of a Gaussian mixture model with equal weights and shared covariance as .

Intuition

The name describes the method. There are clusters, and the mean of its points summarizes each one. Picture customers on a map and pins. Each customer joins the nearest pin. Each pin then moves to the middle of the customers who joined it. That move changes the nearest pin for some customers, so the two moves repeat until nothing changes. Each move is the best choice for one set of unknowns while the other set stays fixed. Lloyd’s algorithm is therefore coordinate descent on , with the assignments and the centers as the two blocks of variables.

Each cluster ends up as the set of points closer to its center than to any other center. These regions are convex, and the boundary between two of them is flat. K-means therefore suits compact groups. It cuts long or curved groups into pieces, and it cannot separate a ring from a group inside it.

The animation runs k-means++ seeding and then Lloyd’s algorithm on 45 points. Each step lowers or leaves it unchanged. The run stops when no point switches clusters.

\usepackage{tikz}
\usetikzlibrary{animations, arrows.meta}
\begin{document}
\definecolor{hot}{HTML}{D9483B}
\definecolor{cold}{HTML}{2F80ED}
\definecolor{grn}{HTML}{2F9E44}
\definecolor{mid}{HTML}{8A8A8A}
\newcommand{\pu}[1]{\filldraw[draw=black, fill=white] (#1) circle (2.3pt);}
\newcommand{\pa}[2]{\filldraw[draw=black, fill=#2] (#1) circle (2.3pt);}
\newcommand{\ce}[2]{\filldraw[draw=black, thick, fill=#2] (#1) ++(0,0.21) -- ++(0.21,-0.21) -- ++(-0.21,-0.21) -- ++(-0.21,0.21) -- cycle;}
\newcommand{\cg}[2]{\draw[#2, thick] (#1) ++(0,0.16) -- ++(0.16,-0.16) -- ++(-0.16,-0.16) -- ++(-0.16,0.16) -- cycle;}
\begin{tikzpicture}[font=\small, >={Stealth[length=2.2mm]}]
\draw (0,0) rectangle (6.75,5.22);
\node[anchor=west] at (6.80,0.2) {$x_1$};
\node[anchor=south] at (0.25,5.22) {$x_2$};
\node[anchor=west] at (7.65,5.05) {objective $J = \sum_i \|\mathbf x_i - \mu_{z_i}\|^2$};
\node[anchor=west] at (7.65,4.60) {\footnotesize within-cluster sum of squares after each step};
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 10.2s = "true", 12.8s = "false", 29.98s = "false", repeats}}}]
\draw[mid, thick] (3.58,3.30) -- (0.00,0.21);
\draw[mid, thick] (6.75,2.98) -- (3.58,3.30);
\draw[mid, thick] (3.58,3.30) -- (3.43,5.22);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 10.2s = "true", 29.98s = "true", repeats}}}]
\node[anchor=west] at (7.65,4.10) {assign 1};
\filldraw[draw=black, fill=mid] (9.40,3.95) rectangle (12.30,4.25);
\node[anchor=west] at (12.35,4.10) {$104.2$};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 10.2s = "true", 15.66s = "false", 29.98s = "false", repeats}}}]
\pa{0.76,3.46}{cold}
\pa{1.42,3.02}{cold}
\pa{1.77,4.00}{cold}
\pa{1.57,2.58}{cold}
\pa{1.85,3.06}{cold}
\pa{2.34,3.12}{cold}
\pa{2.05,2.59}{cold}
\pa{2.73,2.83}{cold}
\pa{1.19,3.61}{cold}
\pa{3.15,3.33}{cold}
\pa{1.72,1.86}{cold}
\pa{2.10,3.20}{cold}
\pa{2.04,4.55}{cold}
\pa{0.45,3.15}{cold}
\pa{2.40,4.32}{cold}
\pa{5.88,2.73}{hot}
\pa{5.08,2.96}{hot}
\pa{5.77,3.81}{grn}
\pa{5.74,3.55}{grn}
\pa{5.86,2.99}{hot}
\pa{5.65,2.11}{hot}
\pa{5.11,4.00}{grn}
\pa{5.95,4.33}{grn}
\pa{4.55,3.99}{grn}
\pa{4.86,3.90}{grn}
\pa{4.26,2.95}{hot}
\pa{5.38,2.33}{hot}
\pa{4.92,4.77}{grn}
\pa{6.32,3.71}{grn}
\pa{5.34,3.13}{grn}
\pa{3.32,2.56}{hot}
\pa{3.98,1.38}{hot}
\pa{3.34,0.95}{hot}
\pa{4.04,0.64}{hot}
\pa{3.77,0.81}{hot}
\pa{3.57,0.46}{hot}
\pa{4.06,0.89}{hot}
\pa{4.31,1.13}{hot}
\pa{3.06,1.13}{hot}
\pa{3.90,1.64}{hot}
\pa{3.82,1.17}{hot}
\pa{4.59,1.59}{hot}
\pa{3.56,1.97}{hot}
\pa{3.53,0.77}{hot}
\pa{4.36,0.87}{hot}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 13.46s = "true", 29.98s = "true", repeats}}}]
\node[anchor=west] at (7.65,3.60) {update 1};
\filldraw[draw=black, fill=mid] (9.40,3.45) rectangle (11.02,3.75);
\node[anchor=west] at (11.07,3.60) {$58.2$};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 15.66s = "true", 18.26s = "false", 29.98s = "false", repeats}}}]
\draw[mid, thick] (3.66,3.35) -- (1.41,0.00);
\draw[mid, thick] (6.75,1.80) -- (3.66,3.35);
\draw[mid, thick] (3.66,3.35) -- (3.31,5.22);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 15.66s = "true", 29.98s = "true", repeats}}}]
\node[anchor=west] at (7.65,3.10) {assign 2};
\filldraw[draw=black, fill=mid] (9.40,2.95) rectangle (10.77,3.25);
\node[anchor=west] at (10.82,3.10) {$49.3$};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 15.66s = "true", 21.12s = "false", 29.98s = "false", repeats}}}]
\pa{0.76,3.46}{cold}
\pa{1.42,3.02}{cold}
\pa{1.77,4.00}{cold}
\pa{1.57,2.58}{cold}
\pa{1.85,3.06}{cold}
\pa{2.34,3.12}{cold}
\pa{2.05,2.59}{cold}
\pa{2.73,2.83}{cold}
\pa{1.19,3.61}{cold}
\pa{3.15,3.33}{cold}
\pa{1.72,1.86}{cold}
\pa{2.10,3.20}{cold}
\pa{2.04,4.55}{cold}
\pa{0.45,3.15}{cold}
\pa{2.40,4.32}{cold}
\pa{5.88,2.73}{grn}
\pa{5.08,2.96}{grn}
\pa{5.77,3.81}{grn}
\pa{5.74,3.55}{grn}
\pa{5.86,2.99}{grn}
\pa{5.65,2.11}{hot}
\pa{5.11,4.00}{grn}
\pa{5.95,4.33}{grn}
\pa{4.55,3.99}{grn}
\pa{4.86,3.90}{grn}
\pa{4.26,2.95}{hot}
\pa{5.38,2.33}{hot}
\pa{4.92,4.77}{grn}
\pa{6.32,3.71}{grn}
\pa{5.34,3.13}{grn}
\pa{3.32,2.56}{hot}
\pa{3.98,1.38}{hot}
\pa{3.34,0.95}{hot}
\pa{4.04,0.64}{hot}
\pa{3.77,0.81}{hot}
\pa{3.57,0.46}{hot}
\pa{4.06,0.89}{hot}
\pa{4.31,1.13}{hot}
\pa{3.06,1.13}{hot}
\pa{3.90,1.64}{hot}
\pa{3.82,1.17}{hot}
\pa{4.59,1.59}{hot}
\pa{3.56,1.97}{hot}
\pa{3.53,0.77}{hot}
\pa{4.36,0.87}{hot}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 18.92s = "true", 29.98s = "true", repeats}}}]
\node[anchor=west] at (7.65,2.60) {update 2};
\filldraw[draw=black, fill=mid] (9.40,2.45) rectangle (10.69,2.75);
\node[anchor=west] at (10.74,2.60) {$46.2$};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 21.12s = "true", 23.72s = "false", 29.98s = "false", repeats}}}]
\draw[mid, thick] (3.67,3.21) -- (0.98,0.00);
\draw[mid, thick] (6.75,1.26) -- (3.67,3.21);
\draw[mid, thick] (3.67,3.21) -- (3.44,5.22);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 21.12s = "true", 29.98s = "true", repeats}}}]
\node[anchor=west] at (7.65,2.10) {assign 3};
\filldraw[draw=black, fill=mid] (9.40,1.95) rectangle (10.61,2.25);
\node[anchor=west] at (10.66,2.10) {$43.6$};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 21.12s = "true", 26.58s = "false", 29.98s = "false", repeats}}}]
\pa{0.76,3.46}{cold}
\pa{1.42,3.02}{cold}
\pa{1.77,4.00}{cold}
\pa{1.57,2.58}{cold}
\pa{1.85,3.06}{cold}
\pa{2.34,3.12}{cold}
\pa{2.05,2.59}{cold}
\pa{2.73,2.83}{cold}
\pa{1.19,3.61}{cold}
\pa{3.15,3.33}{cold}
\pa{1.72,1.86}{cold}
\pa{2.10,3.20}{cold}
\pa{2.04,4.55}{cold}
\pa{0.45,3.15}{cold}
\pa{2.40,4.32}{cold}
\pa{5.88,2.73}{grn}
\pa{5.08,2.96}{grn}
\pa{5.77,3.81}{grn}
\pa{5.74,3.55}{grn}
\pa{5.86,2.99}{grn}
\pa{5.65,2.11}{grn}
\pa{5.11,4.00}{grn}
\pa{5.95,4.33}{grn}
\pa{4.55,3.99}{grn}
\pa{4.86,3.90}{grn}
\pa{4.26,2.95}{grn}
\pa{5.38,2.33}{grn}
\pa{4.92,4.77}{grn}
\pa{6.32,3.71}{grn}
\pa{5.34,3.13}{grn}
\pa{3.32,2.56}{hot}
\pa{3.98,1.38}{hot}
\pa{3.34,0.95}{hot}
\pa{4.04,0.64}{hot}
\pa{3.77,0.81}{hot}
\pa{3.57,0.46}{hot}
\pa{4.06,0.89}{hot}
\pa{4.31,1.13}{hot}
\pa{3.06,1.13}{hot}
\pa{3.90,1.64}{hot}
\pa{3.82,1.17}{hot}
\pa{4.59,1.59}{hot}
\pa{3.56,1.97}{hot}
\pa{3.53,0.77}{hot}
\pa{4.36,0.87}{hot}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 24.38s = "true", 29.98s = "true", repeats}}}]
\node[anchor=west] at (7.65,1.60) {update 3};
\filldraw[draw=black, fill=mid] (9.40,1.45) rectangle (10.54,1.75);
\node[anchor=west] at (10.59,1.60) {$40.8$};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 26.58s = "true", 29.98s = "false", 29.98s = "false", repeats}}}]
\draw[mid, thick] (3.62,2.99) -- (0.52,0.00);
\draw[mid, thick] (6.75,0.79) -- (3.62,2.99);
\draw[mid, thick] (3.62,2.99) -- (3.52,5.22);
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 26.58s = "true", 29.98s = "true", repeats}}}]
\node[anchor=west] at (7.65,1.10) {assign 4};
\filldraw[draw=black, fill=mid] (9.40,0.95) rectangle (10.54,1.25);
\node[anchor=west] at (10.59,1.10) {$40.8$};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 26.58s = "true", 29.98s = "true", repeats}}}]
\pa{0.76,3.46}{cold}
\pa{1.42,3.02}{cold}
\pa{1.77,4.00}{cold}
\pa{1.57,2.58}{cold}
\pa{1.85,3.06}{cold}
\pa{2.34,3.12}{cold}
\pa{2.05,2.59}{cold}
\pa{2.73,2.83}{cold}
\pa{1.19,3.61}{cold}
\pa{3.15,3.33}{cold}
\pa{1.72,1.86}{cold}
\pa{2.10,3.20}{cold}
\pa{2.04,4.55}{cold}
\pa{0.45,3.15}{cold}
\pa{2.40,4.32}{cold}
\pa{5.88,2.73}{grn}
\pa{5.08,2.96}{grn}
\pa{5.77,3.81}{grn}
\pa{5.74,3.55}{grn}
\pa{5.86,2.99}{grn}
\pa{5.65,2.11}{grn}
\pa{5.11,4.00}{grn}
\pa{5.95,4.33}{grn}
\pa{4.55,3.99}{grn}
\pa{4.86,3.90}{grn}
\pa{4.26,2.95}{grn}
\pa{5.38,2.33}{grn}
\pa{4.92,4.77}{grn}
\pa{6.32,3.71}{grn}
\pa{5.34,3.13}{grn}
\pa{3.32,2.56}{hot}
\pa{3.98,1.38}{hot}
\pa{3.34,0.95}{hot}
\pa{4.04,0.64}{hot}
\pa{3.77,0.81}{hot}
\pa{3.57,0.46}{hot}
\pa{4.06,0.89}{hot}
\pa{4.31,1.13}{hot}
\pa{3.06,1.13}{hot}
\pa{3.90,1.64}{hot}
\pa{3.82,1.17}{hot}
\pa{4.59,1.59}{hot}
\pa{3.56,1.97}{hot}
\pa{3.53,0.77}{hot}
\pa{4.36,0.87}{hot}
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "true", 2.2s = "false", 29.98s = "false", repeats}}}]
\pu{0.76,3.46}
\pu{1.42,3.02}
\pu{1.77,4.00}
\pu{1.57,2.58}
\pu{1.85,3.06}
\pu{2.34,3.12}
\pu{2.05,2.59}
\pu{2.73,2.83}
\pu{1.19,3.61}
\pu{3.15,3.33}
\pu{1.72,1.86}
\pu{2.10,3.20}
\pu{2.04,4.55}
\pu{0.45,3.15}
\pu{2.40,4.32}
\pu{5.88,2.73}
\pu{5.08,2.96}
\pu{5.77,3.81}
\pu{5.74,3.55}
\pu{5.86,2.99}
\pu{5.65,2.11}
\pu{5.11,4.00}
\pu{5.95,4.33}
\pu{4.55,3.99}
\pu{4.86,3.90}
\pu{4.26,2.95}
\pu{5.38,2.33}
\pu{4.92,4.77}
\pu{6.32,3.71}
\pu{5.34,3.13}
\pu{3.32,2.56}
\pu{3.98,1.38}
\pu{3.34,0.95}
\pu{4.04,0.64}
\pu{3.77,0.81}
\pu{3.57,0.46}
\pu{4.06,0.89}
\pu{4.31,1.13}
\pu{3.06,1.13}
\pu{3.90,1.64}
\pu{3.82,1.17}
\pu{4.59,1.59}
\pu{3.56,1.97}
\pu{3.53,0.77}
\pu{4.36,0.87}
\ce{4.59,1.59}{hot}
\node[anchor=west] at (-0.1,-0.62) {k-means++ seeding: center 1 is a data point chosen uniformly at random.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 2.2s = "true", 4.8s = "false", 29.98s = "false", repeats}}}]
\draw[mid] (0.76,3.46) -- (4.59,1.59);
\draw[mid] (1.42,3.02) -- (4.59,1.59);
\draw[mid] (1.77,4.00) -- (4.59,1.59);
\draw[mid] (1.57,2.58) -- (4.59,1.59);
\draw[mid] (1.85,3.06) -- (4.59,1.59);
\draw[mid] (2.34,3.12) -- (4.59,1.59);
\draw[mid] (2.05,2.59) -- (4.59,1.59);
\draw[mid] (2.73,2.83) -- (4.59,1.59);
\draw[mid] (1.19,3.61) -- (4.59,1.59);
\draw[mid] (3.15,3.33) -- (4.59,1.59);
\draw[mid] (1.72,1.86) -- (4.59,1.59);
\draw[mid] (2.10,3.20) -- (4.59,1.59);
\draw[mid] (2.04,4.55) -- (4.59,1.59);
\draw[mid] (0.45,3.15) -- (4.59,1.59);
\draw[mid] (2.40,4.32) -- (4.59,1.59);
\draw[mid] (5.88,2.73) -- (4.59,1.59);
\draw[mid] (5.08,2.96) -- (4.59,1.59);
\draw[mid] (5.77,3.81) -- (4.59,1.59);
\draw[mid] (5.74,3.55) -- (4.59,1.59);
\draw[mid] (5.86,2.99) -- (4.59,1.59);
\draw[mid] (5.65,2.11) -- (4.59,1.59);
\draw[mid] (5.11,4.00) -- (4.59,1.59);
\draw[mid] (5.95,4.33) -- (4.59,1.59);
\draw[mid] (4.55,3.99) -- (4.59,1.59);
\draw[mid] (4.86,3.90) -- (4.59,1.59);
\draw[mid] (4.26,2.95) -- (4.59,1.59);
\draw[mid] (5.38,2.33) -- (4.59,1.59);
\draw[mid] (4.92,4.77) -- (4.59,1.59);
\draw[mid] (6.32,3.71) -- (4.59,1.59);
\draw[mid] (5.34,3.13) -- (4.59,1.59);
\draw[mid] (3.32,2.56) -- (4.59,1.59);
\draw[mid] (3.98,1.38) -- (4.59,1.59);
\draw[mid] (3.34,0.95) -- (4.59,1.59);
\draw[mid] (4.04,0.64) -- (4.59,1.59);
\draw[mid] (3.77,0.81) -- (4.59,1.59);
\draw[mid] (3.57,0.46) -- (4.59,1.59);
\draw[mid] (4.06,0.89) -- (4.59,1.59);
\draw[mid] (4.31,1.13) -- (4.59,1.59);
\draw[mid] (3.06,1.13) -- (4.59,1.59);
\draw[mid] (3.90,1.64) -- (4.59,1.59);
\draw[mid] (3.82,1.17) -- (4.59,1.59);
\draw[mid] (3.56,1.97) -- (4.59,1.59);
\draw[mid] (3.53,0.77) -- (4.59,1.59);
\draw[mid] (4.36,0.87) -- (4.59,1.59);
\pu{0.76,3.46}
\pu{1.42,3.02}
\pu{1.77,4.00}
\pu{1.57,2.58}
\pu{1.85,3.06}
\pu{2.34,3.12}
\pu{2.05,2.59}
\pu{2.73,2.83}
\pu{1.19,3.61}
\pu{3.15,3.33}
\pu{1.72,1.86}
\pu{2.10,3.20}
\pu{2.04,4.55}
\pu{0.45,3.15}
\pu{2.40,4.32}
\pu{5.88,2.73}
\pu{5.08,2.96}
\pu{5.77,3.81}
\pu{5.74,3.55}
\pu{5.86,2.99}
\pu{5.65,2.11}
\pu{5.11,4.00}
\pu{5.95,4.33}
\pu{4.55,3.99}
\pu{4.86,3.90}
\pu{4.26,2.95}
\pu{5.38,2.33}
\pu{4.92,4.77}
\pu{6.32,3.71}
\pu{5.34,3.13}
\pu{3.32,2.56}
\pu{3.98,1.38}
\pu{3.34,0.95}
\pu{4.04,0.64}
\pu{3.77,0.81}
\pu{3.57,0.46}
\pu{4.06,0.89}
\pu{4.31,1.13}
\pu{3.06,1.13}
\pu{3.90,1.64}
\pu{3.82,1.17}
\pu{4.59,1.59}
\pu{3.56,1.97}
\pu{3.53,0.77}
\pu{4.36,0.87}
\ce{4.59,1.59}{hot}
\node[anchor=west] at (-0.1,-0.62) {Each point gets weight $D(\mathbf x)^2$, its squared distance to the nearest center (gray).};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 4.8s = "true", 7.4s = "false", 29.98s = "false", repeats}}}]
\draw[mid] (0.76,3.46) -- (4.59,1.59);
\draw[mid] (1.42,3.02) -- (4.59,1.59);
\draw[mid] (1.77,4.00) -- (4.59,1.59);
\draw[mid] (1.57,2.58) -- (4.59,1.59);
\draw[mid] (1.85,3.06) -- (4.59,1.59);
\draw[mid] (2.34,3.12) -- (4.59,1.59);
\draw[mid] (2.05,2.59) -- (4.59,1.59);
\draw[mid] (2.73,2.83) -- (4.59,1.59);
\draw[mid] (1.19,3.61) -- (4.59,1.59);
\draw[mid] (3.15,3.33) -- (4.59,1.59);
\draw[mid] (1.72,1.86) -- (4.59,1.59);
\draw[mid] (2.10,3.20) -- (4.59,1.59);
\draw[mid] (2.04,4.55) -- (4.59,1.59);
\draw[mid] (0.45,3.15) -- (4.59,1.59);
\draw[mid] (2.40,4.32) -- (4.59,1.59);
\draw[mid] (5.88,2.73) -- (4.59,1.59);
\draw[mid] (5.08,2.96) -- (4.59,1.59);
\draw[mid] (5.77,3.81) -- (4.59,1.59);
\draw[mid] (5.74,3.55) -- (4.59,1.59);
\draw[mid] (5.86,2.99) -- (4.59,1.59);
\draw[mid] (5.65,2.11) -- (4.59,1.59);
\draw[mid] (5.11,4.00) -- (4.59,1.59);
\draw[mid] (5.95,4.33) -- (4.59,1.59);
\draw[mid] (4.55,3.99) -- (4.59,1.59);
\draw[mid] (4.86,3.90) -- (4.59,1.59);
\draw[mid] (4.26,2.95) -- (4.59,1.59);
\draw[mid] (5.38,2.33) -- (4.59,1.59);
\draw[mid] (4.92,4.77) -- (4.59,1.59);
\draw[mid] (6.32,3.71) -- (4.59,1.59);
\draw[mid] (5.34,3.13) -- (4.59,1.59);
\draw[mid] (3.32,2.56) -- (4.59,1.59);
\draw[mid] (3.98,1.38) -- (4.59,1.59);
\draw[mid] (3.34,0.95) -- (4.59,1.59);
\draw[mid] (4.04,0.64) -- (4.59,1.59);
\draw[mid] (3.77,0.81) -- (4.59,1.59);
\draw[mid] (3.57,0.46) -- (4.59,1.59);
\draw[mid] (4.06,0.89) -- (4.59,1.59);
\draw[mid] (4.31,1.13) -- (4.59,1.59);
\draw[mid] (3.06,1.13) -- (4.59,1.59);
\draw[mid] (3.90,1.64) -- (4.59,1.59);
\draw[mid] (3.82,1.17) -- (4.59,1.59);
\draw[mid] (3.56,1.97) -- (4.59,1.59);
\draw[mid] (3.53,0.77) -- (4.59,1.59);
\draw[mid] (4.36,0.87) -- (4.59,1.59);
\pu{0.76,3.46}
\pu{1.42,3.02}
\pu{1.77,4.00}
\pu{1.57,2.58}
\pu{1.85,3.06}
\pu{2.34,3.12}
\pu{2.05,2.59}
\pu{2.73,2.83}
\pu{1.19,3.61}
\pu{3.15,3.33}
\pu{1.72,1.86}
\pu{2.10,3.20}
\pu{2.04,4.55}
\pu{0.45,3.15}
\pu{2.40,4.32}
\pu{5.88,2.73}
\pu{5.08,2.96}
\pu{5.77,3.81}
\pu{5.74,3.55}
\pu{5.86,2.99}
\pu{5.65,2.11}
\pu{5.11,4.00}
\pu{5.95,4.33}
\pu{4.55,3.99}
\pu{4.86,3.90}
\pu{4.26,2.95}
\pu{5.38,2.33}
\pu{4.92,4.77}
\pu{6.32,3.71}
\pu{5.34,3.13}
\pu{3.32,2.56}
\pu{3.98,1.38}
\pu{3.34,0.95}
\pu{4.04,0.64}
\pu{3.77,0.81}
\pu{3.57,0.46}
\pu{4.06,0.89}
\pu{4.31,1.13}
\pu{3.06,1.13}
\pu{3.90,1.64}
\pu{3.82,1.17}
\pu{4.59,1.59}
\pu{3.56,1.97}
\pu{3.53,0.77}
\pu{4.36,0.87}
\ce{4.59,1.59}{hot}
\ce{2.04,4.55}{cold}
\node[anchor=west] at (-0.1,-0.62) {Center 2 is sampled with probability $\propto D(\mathbf x)^2$. The two far groups held $93\%$ of it.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 7.4s = "true", 10.2s = "false", 29.98s = "false", repeats}}}]
\draw[mid] (0.76,3.46) -- (2.04,4.55);
\draw[mid] (1.42,3.02) -- (2.04,4.55);
\draw[mid] (1.77,4.00) -- (2.04,4.55);
\draw[mid] (1.57,2.58) -- (2.04,4.55);
\draw[mid] (1.85,3.06) -- (2.04,4.55);
\draw[mid] (2.34,3.12) -- (2.04,4.55);
\draw[mid] (2.05,2.59) -- (2.04,4.55);
\draw[mid] (2.73,2.83) -- (2.04,4.55);
\draw[mid] (1.19,3.61) -- (2.04,4.55);
\draw[mid] (3.15,3.33) -- (2.04,4.55);
\draw[mid] (1.72,1.86) -- (2.04,4.55);
\draw[mid] (2.10,3.20) -- (2.04,4.55);
\draw[mid] (0.45,3.15) -- (2.04,4.55);
\draw[mid] (2.40,4.32) -- (2.04,4.55);
\draw[mid] (5.88,2.73) -- (4.59,1.59);
\draw[mid] (5.08,2.96) -- (4.59,1.59);
\draw[mid] (5.77,3.81) -- (4.59,1.59);
\draw[mid] (5.74,3.55) -- (4.59,1.59);
\draw[mid] (5.86,2.99) -- (4.59,1.59);
\draw[mid] (5.65,2.11) -- (4.59,1.59);
\draw[mid] (5.11,4.00) -- (4.59,1.59);
\draw[mid] (5.95,4.33) -- (4.59,1.59);
\draw[mid] (4.55,3.99) -- (4.59,1.59);
\draw[mid] (4.86,3.90) -- (4.59,1.59);
\draw[mid] (4.26,2.95) -- (4.59,1.59);
\draw[mid] (5.38,2.33) -- (4.59,1.59);
\draw[mid] (4.92,4.77) -- (2.04,4.55);
\draw[mid] (6.32,3.71) -- (4.59,1.59);
\draw[mid] (5.34,3.13) -- (4.59,1.59);
\draw[mid] (3.32,2.56) -- (4.59,1.59);
\draw[mid] (3.98,1.38) -- (4.59,1.59);
\draw[mid] (3.34,0.95) -- (4.59,1.59);
\draw[mid] (4.04,0.64) -- (4.59,1.59);
\draw[mid] (3.77,0.81) -- (4.59,1.59);
\draw[mid] (3.57,0.46) -- (4.59,1.59);
\draw[mid] (4.06,0.89) -- (4.59,1.59);
\draw[mid] (4.31,1.13) -- (4.59,1.59);
\draw[mid] (3.06,1.13) -- (4.59,1.59);
\draw[mid] (3.90,1.64) -- (4.59,1.59);
\draw[mid] (3.82,1.17) -- (4.59,1.59);
\draw[mid] (3.56,1.97) -- (4.59,1.59);
\draw[mid] (3.53,0.77) -- (4.59,1.59);
\draw[mid] (4.36,0.87) -- (4.59,1.59);
\pu{0.76,3.46}
\pu{1.42,3.02}
\pu{1.77,4.00}
\pu{1.57,2.58}
\pu{1.85,3.06}
\pu{2.34,3.12}
\pu{2.05,2.59}
\pu{2.73,2.83}
\pu{1.19,3.61}
\pu{3.15,3.33}
\pu{1.72,1.86}
\pu{2.10,3.20}
\pu{2.04,4.55}
\pu{0.45,3.15}
\pu{2.40,4.32}
\pu{5.88,2.73}
\pu{5.08,2.96}
\pu{5.77,3.81}
\pu{5.74,3.55}
\pu{5.86,2.99}
\pu{5.65,2.11}
\pu{5.11,4.00}
\pu{5.95,4.33}
\pu{4.55,3.99}
\pu{4.86,3.90}
\pu{4.26,2.95}
\pu{5.38,2.33}
\pu{4.92,4.77}
\pu{6.32,3.71}
\pu{5.34,3.13}
\pu{3.32,2.56}
\pu{3.98,1.38}
\pu{3.34,0.95}
\pu{4.04,0.64}
\pu{3.77,0.81}
\pu{3.57,0.46}
\pu{4.06,0.89}
\pu{4.31,1.13}
\pu{3.06,1.13}
\pu{3.90,1.64}
\pu{3.82,1.17}
\pu{4.59,1.59}
\pu{3.56,1.97}
\pu{3.53,0.77}
\pu{4.36,0.87}
\ce{4.59,1.59}{hot}
\ce{2.04,4.55}{cold}
\ce{4.92,4.77}{grn}
\node[anchor=west] at (-0.1,-0.62) {Center 3 is sampled the same way. The group with no center held $55\%$ of the weight.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 10.2s = "true", 12.8s = "false", 29.98s = "false", repeats}}}]
\ce{4.59,1.59}{hot}
\ce{2.04,4.55}{cold}
\ce{4.92,4.77}{grn}
 
\node[anchor=west] at (-0.1,-0.62) {Assignment: each point takes the color of its nearest center. Gray: Voronoi edges.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 12.8s = "true", 13.02s = "false", 29.98s = "false", repeats}}}]
\cg{4.59,1.59}{hot}
\cg{2.04,4.55}{cold}
\cg{4.92,4.77}{grn}
\ce{4.54,1.60}{hot}
\ce{2.01,4.35}{cold}
\ce{4.99,4.64}{grn}
\node[anchor=west] at (-0.1,-0.62) {Update: each center moves to the mean of its points. $J$ falls.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 13.02s = "true", 13.24s = "false", 29.98s = "false", repeats}}}]
\cg{4.59,1.59}{hot}
\cg{2.04,4.55}{cold}
\cg{4.92,4.77}{grn}
\ce{4.42,1.61}{hot}
\ce{1.94,3.90}{cold}
\ce{5.16,4.34}{grn}
\node[anchor=west] at (-0.1,-0.62) {Update: each center moves to the mean of its points. $J$ falls.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 13.24s = "true", 13.46s = "false", 29.98s = "false", repeats}}}]
\cg{4.59,1.59}{hot}
\cg{2.04,4.55}{cold}
\cg{4.92,4.77}{grn}
\ce{4.31,1.62}{hot}
\ce{1.87,3.45}{cold}
\ce{5.32,4.05}{grn}
\node[anchor=west] at (-0.1,-0.62) {Update: each center moves to the mean of its points. $J$ falls.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 13.46s = "true", 15.66s = "false", 29.98s = "false", repeats}}}]
\cg{4.59,1.59}{hot}
\cg{2.04,4.55}{cold}
\cg{4.92,4.77}{grn}
\draw[->, thick] (2.01,4.37) -- (1.88,3.50);
\draw[->, thick] (5.00,4.62) -- (5.27,4.14);
\ce{4.25,1.62}{hot}
\ce{1.84,3.25}{cold}
\ce{5.40,3.91}{grn}
\node[anchor=west] at (-0.1,-0.62) {Update: each center moves to the mean of its points. $J$ falls.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 15.66s = "true", 18.26s = "false", 29.98s = "false", repeats}}}]
\ce{4.25,1.62}{hot}
\ce{1.84,3.25}{cold}
\ce{5.40,3.91}{grn}
\draw[thick] (5.88,2.73) circle (4.2pt);
\draw[thick] (5.08,2.96) circle (4.2pt);
\draw[thick] (5.86,2.99) circle (4.2pt);
\node[anchor=west] at (-0.1,-0.62) {Assignment: 3 points (ringed) switch to a nearer center. $J$ falls.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 18.26s = "true", 18.48s = "false", 29.98s = "false", repeats}}}]
\cg{4.25,1.62}{hot}
\cg{1.84,3.25}{cold}
\cg{5.40,3.91}{grn}
\ce{4.22,1.59}{hot}
\ce{1.84,3.25}{cold}
\ce{5.40,3.87}{grn}
\node[anchor=west] at (-0.1,-0.62) {Update: each center moves to the mean of its points. $J$ falls.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 18.48s = "true", 18.7s = "false", 29.98s = "false", repeats}}}]
\cg{4.25,1.62}{hot}
\cg{1.84,3.25}{cold}
\cg{5.40,3.91}{grn}
\ce{4.14,1.51}{hot}
\ce{1.84,3.25}{cold}
\ce{5.42,3.78}{grn}
\node[anchor=west] at (-0.1,-0.62) {Update: each center moves to the mean of its points. $J$ falls.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 18.7s = "true", 18.92s = "false", 29.98s = "false", repeats}}}]
\cg{4.25,1.62}{hot}
\cg{1.84,3.25}{cold}
\cg{5.40,3.91}{grn}
\ce{4.06,1.44}{hot}
\ce{1.84,3.25}{cold}
\ce{5.44,3.70}{grn}
\node[anchor=west] at (-0.1,-0.62) {Update: each center moves to the mean of its points. $J$ falls.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 18.92s = "true", 21.12s = "false", 29.98s = "false", repeats}}}]
\cg{4.25,1.62}{hot}
\cg{1.84,3.25}{cold}
\cg{5.40,3.91}{grn}
\ce{4.03,1.41}{hot}
\ce{1.84,3.25}{cold}
\ce{5.45,3.66}{grn}
\node[anchor=west] at (-0.1,-0.62) {Update: each center moves to the mean of its points. $J$ falls.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 21.12s = "true", 23.72s = "false", 29.98s = "false", repeats}}}]
\ce{4.03,1.41}{hot}
\ce{1.84,3.25}{cold}
\ce{5.45,3.66}{grn}
\draw[thick] (5.65,2.11) circle (4.2pt);
\draw[thick] (4.26,2.95) circle (4.2pt);
\draw[thick] (5.38,2.33) circle (4.2pt);
\node[anchor=west] at (-0.1,-0.62) {Assignment: 3 points (ringed) switch to a nearer center. $J$ falls.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 23.72s = "true", 23.94s = "false", 29.98s = "false", repeats}}}]
\cg{4.03,1.41}{hot}
\cg{1.84,3.25}{cold}
\cg{5.45,3.66}{grn}
\ce{3.99,1.37}{hot}
\ce{1.84,3.25}{cold}
\ce{5.44,3.62}{grn}
\node[anchor=west] at (-0.1,-0.62) {Update: each center moves to the mean of its points. $J$ falls.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 23.94s = "true", 24.16s = "false", 29.98s = "false", repeats}}}]
\cg{4.03,1.41}{hot}
\cg{1.84,3.25}{cold}
\cg{5.45,3.66}{grn}
\ce{3.92,1.30}{hot}
\ce{1.84,3.25}{cold}
\ce{5.41,3.54}{grn}
\node[anchor=west] at (-0.1,-0.62) {Update: each center moves to the mean of its points. $J$ falls.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 24.16s = "true", 24.38s = "false", 29.98s = "false", repeats}}}]
\cg{4.03,1.41}{hot}
\cg{1.84,3.25}{cold}
\cg{5.45,3.66}{grn}
\ce{3.85,1.23}{hot}
\ce{1.84,3.25}{cold}
\ce{5.39,3.46}{grn}
\node[anchor=west] at (-0.1,-0.62) {Update: each center moves to the mean of its points. $J$ falls.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 24.38s = "true", 26.58s = "false", 29.98s = "false", repeats}}}]
\cg{4.03,1.41}{hot}
\cg{1.84,3.25}{cold}
\cg{5.45,3.66}{grn}
\ce{3.81,1.20}{hot}
\ce{1.84,3.25}{cold}
\ce{5.38,3.42}{grn}
\node[anchor=west] at (-0.1,-0.62) {Update: each center moves to the mean of its points. $J$ falls.};
\end{scope}
\begin{scope}[animate = {myself: = {:visible = {0s = "false", 26.58s = "true", 29.98s = "false", 29.98s = "false", repeats}}}]
\ce{3.81,1.20}{hot}
\ce{1.84,3.25}{cold}
\ce{5.38,3.42}{grn}
 
\node[anchor=west] at (-0.1,-0.62) {No point switches, so Lloyd's algorithm stops at a local minimum, $J = 40.8$.};
\end{scope}
\end{tikzpicture}
\end{document}

Properties

(Local Minimum Only) Lloyd’s algorithm can stop at a partition far worse than the best one.

Aloise et al. (2009) proved that minimizing exactly is NP-hard for in general dimension. Mahajan et al. (2009) proved it NP-hard for points in the plane when is part of the input. At a fixed point of Lloyd’s algorithm, no single point gains by switching and no center gains by moving. Take four points at the corners of a rectangle with and . Splitting into the top pair and the bottom pair is a fixed point with . Splitting into the left pair and the right pair gives . The ratio grows without bound as the rectangle gets wider. Restarts and k-means++ seeding lower the chance of a bad result, but neither removes it.

(Flat Boundaries) The assignment step cuts space into convex cells with flat boundaries.

A point is closer to than to when . Expanding both sides cancels and leaves a linear condition,

The boundary is the hyperplane halfway between the two centers, perpendicular to the segment that joins them. Each cluster is an intersection of half-spaces, called a Voronoi cell. No choice of centers separates two interleaved crescents. An elongated cluster also gets cut across, since the boundary ignores the shape of the clusters. A Gaussian mixture with full covariances fits elongated clusters with curved boundaries.

(Units Matter) Rescaling one feature can change the clustering completely.

The squared distance adds the squared differences of all features with equal weight. A feature measured in large units therefore dominates the sum. Suppose customers are described by income in dollars and age in years. An income gap of 1,000 dollars then counts a million times more than an age gap of one year. The clusters follow income alone. Standardizing each feature to mean and variance puts every feature on the same scale. This is also a modeling choice, since it amplifies features that are mostly noise. With many features, k-means on the top principal components drops noisy directions and makes each iteration cheaper.

(Hard-Assignment Limit of a Mixture) Lloyd’s algorithm is EM for a Gaussian mixture with equal weights and covariance , in the limit .

In that mixture, the probability that point belongs to cluster is

where is the shared variance of every coordinate. As shrinks, the term of the nearest center dominates all others. Then tends to for the nearest center and to for the rest. The E-step becomes the assignment step, and the M-step mean becomes the plain cluster mean. In mixture terms, k-means assumes that every cluster is a round Gaussian with the same spread and the same weight. K-means therefore handles clusters of very different spreads poorly.

(Objective Falls With K) The best achievable never increases when grows, so alone cannot choose .

Adding a center at any data point cannot raise . With , every point is its own center and . Choosing therefore needs a criterion that penalizes extra clusters or rewards separation. The elbow is often unclear on real data, because can fall smoothly. The silhouette can fall when is too large, since splitting a true cluster puts close points in different clusters. The gap statistic (Tibshirani, Walther and Hastie 2001) compares with its value on uniform data over the same range. A mixture model can use BIC, a penalized likelihood.

Derivation

We show that the update step is optimal and that Lloyd’s algorithm stops. Fix the assignments . The objective splits into one sum per cluster, and center appears only in its own sum:

Let be the mean of the points in cluster . Adding and subtracting inside each norm gives, for any center ,

The cross term vanishes because the deviations sum to zero. The right side is smallest at , so the mean is the best center. Setting the gradient to zero gives the same answer. By the same identity, a misplaced center adds times its squared distance from the mean to .

In the assignment step, each point picks its nearest center, so no distance grows. With the tie rule, falls strictly whenever an assignment changes. The update step cannot raise , by the identity above. Together,

where the superscript counts iterations. After each update step, the centers are the means of the current partition, so depends on the partition alone. No partition can repeat, because falls strictly at every change. There are at most partitions, so the algorithm stops after finitely many iterations. The final partition is a fixed point of both steps, and it need not be the global minimum.

Examples

(Easy) Placing Two Delivery Hubs

A bakery delivers along one road. Its six regular customers live at kilometers , , , , and . It wants hubs and starts Lloyd’s algorithm with centers at and .

(Medium) Choosing the Number of Hubs

For the same six customers, the best objective for is , , and .

(Medium) A Bad Fixed Point

Four warehouses sit at the corners of a rectangle, at , , and in kilometers. A company runs k-means with .

(Hard) From Soft to Hard Assignments

A vibration sensor on a pump reads , , , and mm/s on five days. The pump has two states with centers and . Each state is modeled as a Gaussian with the same variance , and the states are equally likely.

See Also