<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Clustering on davethehuman.com</title><link>https://davethehuman.com/tags/clustering/</link><description>Recent content in Clustering on davethehuman.com</description><generator>Hugo -- gohugo.io</generator><language>en</language><copyright>© 2026 Dave the human</copyright><lastBuildDate>Sat, 10 Oct 2026 00:00:00 +0000</lastBuildDate><atom:link href="https://davethehuman.com/tags/clustering/index.xml" rel="self" type="application/rss+xml"/><item><title>K-means</title><link>https://davethehuman.com/notes/k-means/</link><pubDate>Sat, 10 Oct 2026 00:00:00 +0000</pubDate><guid>https://davethehuman.com/notes/k-means/</guid><description>&lt;p&gt;&lt;em&gt;K-means&lt;/em&gt; is an &lt;a href="https://davethehuman.com/coming-soon/" rel="nofollow"&gt;unsupervised learning&lt;/a&gt; algorithm that assigns labels to the data points (&lt;a href="https://davethehuman.com/coming-soon/" rel="nofollow"&gt;clustering&lt;/a&gt;) with an iterative optimization that repeats until &lt;a href="https://davethehuman.com/coming-soon/" rel="nofollow"&gt;convergence&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;figure&gt;&lt;img
 class="my-0 rounded-md"
 loading="lazy"
 decoding="async"
 fetchpriority="low"
 alt=""
 src="k-means.png"
 &gt;&lt;/figure&gt;
&lt;/p&gt;

&lt;h2 class="relative group"&gt;Optimization
 &lt;div id="optimization" class="anchor"&gt;&lt;/div&gt;
 
 &lt;span
 class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none"&gt;
 &lt;a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#optimization" aria-label="Anchor"&gt;#&lt;/a&gt;
 &lt;/span&gt;
 
&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;initialize &lt;a href="https://davethehuman.com/coming-soon/" rel="nofollow"&gt;clusters&lt;/a&gt; centers (also called &lt;a href="https://davethehuman.com/coming-soon/" rel="nofollow"&gt;centroids&lt;/a&gt;) randomly&lt;/li&gt;
&lt;li&gt;update cluster labels by assigning points to the nearest cluster center&lt;/li&gt;
&lt;li&gt;update cluster centers by setting the center to the mean of the corresponding cluster&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 class="relative group"&gt;Loss function
 &lt;div id="loss-function" class="anchor"&gt;&lt;/div&gt;
 
 &lt;span
 class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none"&gt;
 &lt;a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#loss-function" aria-label="Anchor"&gt;#&lt;/a&gt;
 &lt;/span&gt;
 
&lt;/h2&gt;
&lt;p&gt;&lt;a href="https://davethehuman.com/notes/euclidean-distance/"&gt;Euclidean distance&lt;/a&gt; is the metric used to find the nearest centroids.&lt;/p&gt;</description></item><item><title>K-means from scratch</title><link>https://davethehuman.com/notes/k-means-from-scratch/</link><pubDate>Sat, 10 Oct 2026 00:00:00 +0000</pubDate><guid>https://davethehuman.com/notes/k-means-from-scratch/</guid><description>&lt;h2 class="relative group"&gt;Algorithm and implementation
 &lt;div id="algorithm-and-implementation" class="anchor"&gt;&lt;/div&gt;
 
 &lt;span
 class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none"&gt;
 &lt;a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#algorithm-and-implementation" aria-label="Anchor"&gt;#&lt;/a&gt;
 &lt;/span&gt;
 
&lt;/h2&gt;
&lt;p&gt;&lt;a href="https://davethehuman.com/notes/k-means/"&gt;K-means&lt;/a&gt; algorithm assigns labels to data points by iteratively updating &lt;a href="https://davethehuman.com/coming-soon/" rel="nofollow"&gt;centroids&lt;/a&gt; and &lt;a href="https://davethehuman.com/coming-soon/" rel="nofollow"&gt;clusters&lt;/a&gt;&amp;rsquo; labels and using &lt;a href="https://davethehuman.com/notes/euclidean-distance/"&gt;euclidean distance&lt;/a&gt; to calculate their proximity until &lt;a href="https://davethehuman.com/coming-soon/" rel="nofollow"&gt;convergence&lt;/a&gt; (or until a maximum number of iterations is reached).&lt;/p&gt;

&lt;h2 class="relative group"&gt;Class structure
 &lt;div id="class-structure" class="anchor"&gt;&lt;/div&gt;
 
 &lt;span
 class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none"&gt;
 &lt;a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#class-structure" aria-label="Anchor"&gt;#&lt;/a&gt;
 &lt;/span&gt;
 
&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;main attributes&lt;/strong&gt;:
&lt;ul&gt;
&lt;li&gt;the number of clusters &lt;code&gt;K&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;n_iterations&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;clusters&lt;/code&gt;, which is the list of sample indices for each cluster initialized in the &lt;code&gt;__init__&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;centroid&lt;/code&gt; which is the center (or mean vector) for each cluster&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;code&gt;predict&lt;/code&gt; method
It is worth to note that &lt;strong&gt;there&amp;rsquo;s no &lt;code&gt;fit&lt;/code&gt; method&lt;/strong&gt; and we can do the &lt;code&gt;predict&lt;/code&gt; straight away.&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight-wrapper"&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#f92672"&gt;import&lt;/span&gt; numpy &lt;span style="color:#66d9ef"&gt;as&lt;/span&gt; np
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;class&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;KMeans&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;	&lt;span style="color:#66d9ef"&gt;def&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;__init__&lt;/span&gt;(self, K&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#ae81ff"&gt;5&lt;/span&gt;, n_iterations&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#ae81ff"&gt;100&lt;/span&gt;):
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;		self&lt;span style="color:#f92672"&gt;.&lt;/span&gt;K &lt;span style="color:#f92672"&gt;=&lt;/span&gt; K
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;		self&lt;span style="color:#f92672"&gt;.&lt;/span&gt;n_iterations &lt;span style="color:#f92672"&gt;=&lt;/span&gt; n_iterations
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;		
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;		&lt;span style="color:#75715e"&gt;#list of sample indices for each cluster&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;		self&lt;span style="color:#f92672"&gt;.&lt;/span&gt;clusters &lt;span style="color:#f92672"&gt;=&lt;/span&gt; [[] &lt;span style="color:#66d9ef"&gt;for&lt;/span&gt; _ &lt;span style="color:#f92672"&gt;in&lt;/span&gt; range(self&lt;span style="color:#f92672"&gt;.&lt;/span&gt;K)]
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;		
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;		&lt;span style="color:#75715e"&gt;#mean vector for each cluster&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;		self&lt;span style="color:#f92672"&gt;.&lt;/span&gt;centroids &lt;span style="color:#f92672"&gt;=&lt;/span&gt; []
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;		
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;	&lt;span style="color:#66d9ef"&gt;def&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;predict&lt;/span&gt;(self, X):
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;		&lt;span style="color:#66d9ef"&gt;pass&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 class="relative group"&gt;Inference
 &lt;div id="inference" class="anchor"&gt;&lt;/div&gt;
 
 &lt;span
 class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none"&gt;
 &lt;a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#inference" aria-label="Anchor"&gt;#&lt;/a&gt;
 &lt;/span&gt;
 
&lt;/h2&gt;
&lt;p&gt;The &lt;code&gt;predict&lt;/code&gt; method consists of the following steps:&lt;/p&gt;</description></item></channel></rss>