{"id":215,"date":"2026-11-13T12:34:49","date_gmt":"2026-11-13T04:34:49","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-lloyds-algorithm-runs-k-means\/"},"modified":"2026-11-13T21:52:58","modified_gmt":"2026-11-13T13:52:58","slug":"how-lloyds-algorithm-runs-k-means","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-lloyds-algorithm-runs-k-means\/","title":{"rendered":"How Lloyd&#8217;s algorithm runs k-means"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> <strong>Lloyd&#8217;s algorithm<\/strong> is the standard k-means iteration. Assign every point to the nearest of k centers, then replace each center by the mean of the points assigned to it. Repeat until the assignment stops changing or a step budget runs out. Each step decreases the sum of squared distances, so the loop reaches a local minimum. It does not reach the global minimum. <strong>k-means++<\/strong> chooses the first centers far apart and is the usual initialization.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the problem actually asks<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">The k-means objective partitions points into k clusters and places each center at the mean of its cluster, minimizing the within-cluster sum of squares. The joint problem is NP-hard. Lloyd&#8217;s algorithm alternates the two easy pieces: the best assignment given the centers is the nearest center, and the best center given the assignment is the mean. Either piece alone is optimal for the other piece held fixed. The pair of pieces is only locally optimal.<\/p>\n\n\n<p class=\"wp-block-paragraph\">A bad initialization puts two centers in one clump and none in another. The iteration cannot move a center across an empty gap if no point is assigned to it. Empty clusters need a documented restart rule.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the algorithm guarantees<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Each full iteration costs O(n\u00b7k\u00b7d) distance computations in d dimensions and does not increase the objective. The number of iterations until exact convergence can be exponential in contrived cases and is small on typical data. The result depends on the start. k-means++ samples the next center with probability proportional to the squared distance to the nearest chosen center and has a proven approximation factor in expectation relative to the optimal objective. It is still not an exact solver.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The objective uses squared Euclidean distance. Substituting cosine distance or a median, which is k-medians, changes the update. The mean is the minimizer of squared error. The median is the minimizer of absolute error. Using a mean with an absolute-error objective, or a median with squared error, optimizes the wrong center.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>The working steps<\/strong><\/h4>\n\n<ol class=\"wp-block-list\">\n<li>Initialize k distinct centers, preferably with k-means++: choose the first uniformly, and each later center with probability proportional to its squared distance to the closest already chosen center.<\/li>\n<li>Assign each point to the nearest center under Euclidean distance.<\/li>\n<li>Replace each center by the coordinate-wise mean of its assigned points. If a center received no points, reseed it, for example at a random point, or the run is stuck.<\/li>\n<li>Repeat the assignment and the update until the assignment is unchanged or the objective drop is negligible.<\/li>\n<li>Run several random restarts if k-means++ is not used, and keep the lowest objective. The iteration itself does not escape a bad basin.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">The descent is local. A lower objective from a different start is possible and common. The algorithm is not DBSCAN, which finds dense regions without a preset k and without using means. It is not a Gaussian mixture, which uses soft assignments and the EM algorithm. Squared Euclidean k-means has a spherical-cluster bias.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Approaches that look simpler and fail<\/strong><\/h4>\n\n<figure class=\"wp-block-table\">\n<table>\n<thead>\n<tr>\n<th>Approach<\/th>\n<th>Why it fails when the center is updated with a median under squared error<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Replace the center by a coordinate-wise median while scoring squared error<\/td>\n<td>The mean is the unique minimizer of the sum of squares. A median optimizes a different objective, and the alternation may not descend on the k-means score.<\/td>\n<\/tr>\n<tr>\n<td>Promise the global minimum after one Lloyd run<\/td>\n<td>The objective decreases to a local minimum that depends on initialization. k-means++ improves the start and does not exhaust the search.<\/td>\n<\/tr>\n<tr>\n<td>Leave an empty cluster in place<\/td>\n<td>Its center never receives a gradient from any point. The effective number of clusters has dropped. Reseed or accept the smaller k explicitly.<\/td>\n<\/tr>\n<tr>\n<td>Use k-means on a distance that is not squared Euclidean and keep the mean update<\/td>\n<td>The mean step is derived from squared error. Another distance needs the center that actually minimizes that distance.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">Density-connected clusters without a preset k are DBSCAN. Soft Gaussian clusters are expectation-maximization. Lloyd&#8217;s algorithm is hard k-means with a mean update.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>A working example<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">This Python fragment alternates assignment and the mean. Points 0, 1 and 10, 11 with k = 2 move to centers near 0.5 and 10.5. It prints those centers rounded, <code>0.5 10.5<\/code>.<\/p>\n\n<pre><code class=\"language-python\">points = [0.0, 1.0, 10.0, 11.0]\ncenters = [0.0, 10.0]\nfor _ in range(5):\n    groups = [[] for _ in centers]\n    for p in points:\n        groups[min(range(2), key=lambda i: abs(p - centers[i]))].append(p)\n    centers = [sum(g) \/ len(g) for g in groups]\nassert abs(centers[0] - 0.5) &lt; 1e-9 and abs(centers[1] - 10.5) &lt; 1e-9\nprint(f&quot;{centers[0]:.1f}&quot;, f&quot;{centers[1]:.1f}&quot;)\n<\/code><\/pre>\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Where it is used<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\"><strong>K-means<\/strong>, Lloyd&#8217;s algorithm, is the clustering in scikit-learn and the color quantization that builds a palette. Customer segmentation uses the same alternation of assign and average. DBSCAN is the density clusterer that does not ask for k and can leave noise out.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>When you can drop this algorithm<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Drop a single random initialization when the objective will be treated as the truth. Use k-means++ or several restarts and keep the best objective. Remember the result is local.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Why this stays published<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">We publish this so a search for k-means returns Lloyd&#8217;s alternation, the local minimum it reaches, and the role of k-means++. <strong>Lloyd&#8217;s algorithm alternates nearest-center assignment and mean updates, which descends the k-means objective to a local minimum and does not promise the global one.<\/strong><\/p>\n\n\n<p class=\"wp-block-paragraph\">\u672c\u6587\u7531 HoHo \u8207 AI \u5354\u4f5c\u6574\u7406\uff0c\u6700\u5f8c\u66f4\u65b0\u65bc 2026 \u5e74 11 \u6708 13 \u65e5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Short answer: Lloyd&#8217;s algorithm is the standard k-means iteration. Assign every point to the nearest of k centers, then replace each center by the mean of the points assigned to it. Repeat until the assignment stops changing or a step budget runs out. Each step decreases the sum of&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,48],"tags":[],"class_list":["post-215","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-clustering"],"_links":{"self":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/215","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/comments?post=215"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/215\/revisions"}],"predecessor-version":[{"id":614,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/215\/revisions\/614"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=215"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=215"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=215"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}