{"id":113701,"date":"2026-06-04T22:53:42","date_gmt":"2026-06-04T17:23:42","guid":{"rendered":"https:\/\/www.guvi.in\/blog\/?p=113701"},"modified":"2026-11-20T19:48:32","modified_gmt":"2026-11-20T14:18:32","slug":"implementing-agglomerative-clustering-using-sklearn","status":"publish","type":"post","link":"https:\/\/www.guvi.in\/blog\/implementing-agglomerative-clustering-using-sklearn\/","title":{"rendered":"Implementing Agglomerative Clustering using Sklearn"},"content":{"rendered":"\n<p>Agglomerative clustering groups unlabeled data by treating every observation as its own cluster and joining the most similar ones step by step. The result is a hierarchy of nested groups instead of a single flat split.<\/p>\n\n\n\n<p>This makes agglomerative clustering useful when the number of natural groups is unknown, and the relationships between them matter as much as the groups themselves.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>TL;DR<\/strong><\/h2>\n\n\n\n<ul>\n<li>Agglomerative clustering works bottom-up, starting with each point alone and merging the closest clusters until a stopping condition is reached.<\/li>\n\n\n\n<li>The linkage method sets how cluster distance is measured, and Ward, the sklearn default, favors compact groups of similar size.<\/li>\n\n\n\n<li>A dendrogram records every merge and its distance, and cutting it at a chosen height yields the final clusters.<\/li>\n\n\n\n<li>Since no centroids are involved, results stay stable across runs, and a distance threshold removes the need to fix the cluster count in advance.<\/li>\n\n\n\n<li>Memory and time grow quickly with dataset size; merges cannot be undone, and sklearn has no predict method for new samples.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What is Agglomerative Clustering?<\/strong><\/h2>\n\n\n\n<p>Agglomerative clustering is a hierarchical clustering method used for unsupervised tasks. It starts with each data point as an individual cluster. The <a href=\"https:\/\/www.guvi.in\/blog\/what-is-an-algorithm\/\" target=\"_blank\" rel=\"noreferrer noopener\">algorithm<\/a> continuously merges clusters until all points belong to one cluster or a certain condition is met.<\/p>\n\n\n\n<p>Because it builds from smaller to larger clusters, it is often called bottom-up clustering.<\/p>\n\n\n\n<p>Unlike <a href=\"https:\/\/www.guvi.in\/blog\/k-means-clustering-algorithm-machine-learning\/\" target=\"_blank\" rel=\"noreferrer noopener\">k-means clustering<\/a>, agglomerative clustering does not need to set cluster centers. Instead, it relies on distance measurements and linkage methods to determine how clusters merge.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong><em>Spotting hidden structure in raw data is a skill worth owning. If clustering has sparked your curiosity, take the next step with HCL GUVI\u2019s Intel &amp; IITM Pravartak Certified AI\/ML Course and learn to turn patterns into real, working projects.<\/em><\/strong><\/p>\n\n\n<div class=\"wp-block-guvi-cta-button\" style=\"justify-content:center;\"><a href=\"https:\/\/www.guvi.in\/mlp\/artificial-intelligence-and-machine-learning?utm_source=blog&#038;utm_medium=cta_button&#038;utm_campaign=implementing-agglomerative-clustering\" class=\"guvi-btn-enquire\" target=\"_blank\" rel=\"noopener noreferrer\">Enroll in HCL GUVI&#039;s AI\/ML Course<svg class=\"guvi-btn-chevron\" style=\"transform:rotate(0deg);\" width=\"16\" height=\"16\" viewBox=\"0 0 24 24\" fill=\"none\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" aria-hidden=\"true\"><path fill-rule=\"evenodd\" clip-rule=\"evenodd\" d=\"M9.39935 18.1007C9.0674 17.7687 9.0674 17.2305 9.39935 16.8986L13.6922 12.6057C13.7508 12.5471 13.7508 12.4521 13.6922 12.3935L9.39935 8.10065C9.0674 7.7687 9.0674 7.23051 9.39935 6.89857C9.7313 6.56662 10.2695 6.56662 10.6014 6.89857L14.8943 11.1915C15.6168 11.9139 15.6168 13.0853 14.8943 13.8078L10.6014 18.1007C10.2695 18.4326 9.7313 18.4326 9.39935 18.1007Z\" fill=\"currentColor\"\/><\/svg><\/a><\/div>\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Understanding Hierarchical Clustering<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1200\" height=\"630\" src=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/image-132-1200x630.png\" alt=\"\" class=\"wp-image-133318\" srcset=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/image-132-1200x630.png 1200w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/image-132-300x158.png 300w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/image-132-768x403.png 768w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/image-132-1536x806.png 1536w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/image-132-2048x1075.png 2048w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/image-132-150x79.png 150w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" title=\"\"><\/figure>\n\n\n\n<p><a href=\"https:\/\/www.guvi.in\/blog\/introduction-to-hierarchical-clustering\/\" target=\"_blank\" rel=\"noreferrer noopener\">Hierarchical clustering<\/a> builds a hierarchy of clusters. It mainly comes in two forms:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Agglomerative Hierarchical Clustering<\/strong><\/h3>\n\n\n\n<p>This approach starts with individual points and merges them step by step.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Divisive Hierarchical Clustering<\/strong><\/h3>\n\n\n\n<p>This approach starts with one large cluster and breaks it down into smaller clusters.<\/p>\n\n\n\n<p>Among these two methods, agglomerative clustering is more popular because it is easier to use and more efficient.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>How Bottom-Up Clustering Works<\/strong><\/h2>\n\n\n\n<p>Bottom-up <a href=\"https:\/\/www.guvi.in\/blog\/what-is-clustering-in-machine-learning\/\" target=\"_blank\" rel=\"noreferrer noopener\">clustering<\/a> involves these steps:<\/p>\n\n\n\n<ol>\n<li>Treat each data point as a separate cluster.<\/li>\n\n\n\n<li>Calculate the distance matrix for all clusters.<\/li>\n\n\n\n<li>Identify the two closest clusters.<\/li>\n\n\n\n<li>Merge those clusters.<\/li>\n\n\n\n<li>Update the distance matrix.<\/li>\n\n\n\n<li>Repeat until the desired number of clusters is formed.<\/li>\n<\/ol>\n\n\n\n<p>The way the algorithm measures distances between clusters depends on the chosen linkage methods.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>How Cluster Merging Happens in Agglomerative Clustering<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1200\" height=\"630\" src=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/image-129-1200x630.png\" alt=\"\" class=\"wp-image-133312\" srcset=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/image-129-1200x630.png 1200w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/image-129-300x158.png 300w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/image-129-768x403.png 768w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/image-129-1536x806.png 1536w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/image-129-2048x1075.png 2048w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/image-129-150x79.png 150w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" title=\"\"><\/figure>\n\n\n\n<p>Cluster merging is the core process behind agglomerative clustering. Initially, every data point is its own cluster. The algorithm then calculates the distance between clusters and merges the two closest clusters.<\/p>\n\n\n\n<p>After every merge, the distance matrix gets updated to reflect the newly formed cluster. This process continues repeatedly until the required number of clusters is achieved.<\/p>\n\n\n\n<p>The merging behavior depends heavily on the selected linkage methods:<\/p>\n\n\n\n<ol>\n<li>Ward linkage focuses on minimizing cluster variance.<\/li>\n\n\n\n<li>Complete linkage uses the maximum distance between clusters.<\/li>\n\n\n\n<li>Single linkage uses the minimum distance between clusters.<\/li>\n\n\n\n<li>Average linkage considers the average distance between all points.<\/li>\n<\/ol>\n\n\n\n<p>Because of this step-by-step merging structure, agglomerative clustering is considered a bottom-up clustering technique.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Types of Linkage Methods<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1200\" height=\"630\" src=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/image-130-1200x630.png\" alt=\"\" class=\"wp-image-133315\" srcset=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/image-130-1200x630.png 1200w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/image-130-300x158.png 300w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/image-130-768x403.png 768w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/image-130-1536x806.png 1536w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/image-130-2048x1075.png 2048w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/image-130-150x79.png 150w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" title=\"\"><\/figure>\n\n\n\n<p>Linkage methods decide how the distance between two clusters is measured when they are merged.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. Ward Linkage<\/strong><\/h3>\n\n\n\n<p>Ward linkage merges the pair of clusters that causes the smallest increase in within-cluster variance, which tends to produce compact clusters of similar size. It is the default in sklearn\u2019s <code>AgglomerativeClustering<\/code> and works only with Euclidean distance.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. Complete Linkage<\/strong><\/h3>\n\n\n\n<p>Complete linkage uses the largest distance between any two points, one from each cluster. This favors compact clusters, but a single distant point can inflate that maximum, so outliers still affect it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. Single Linkage<\/strong><\/h3>\n\n\n\n<p>Single linkage uses the smallest distance between any two points across the clusters. It can follow elongated or irregular shapes, but noise and outliers can chain unrelated points into one cluster.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. Average Linkage<\/strong><\/h3>\n\n\n\n<p>Average linkage takes the mean distance across all point pairs between two clusters. It offers a middle ground between single and complete linkage.<\/p>\n\n\n\n<p>In sklearn, you choose the method with the <code>linkage<\/code> parameter, using <code>'ward'<\/code>, <code>'complete'<\/code>, <code>'average'<\/code>, or <code>'single'<\/code>.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Comparison of Linkage Methods in Agglomerative Clustering<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th><strong>Feature<\/strong><\/th><th><strong>Ward<\/strong><\/th><th><strong>Complete<\/strong><\/th><th><strong>Single<\/strong><\/th><th><strong>Average<\/strong><\/th><\/tr><\/thead><tbody><tr><td><strong>Distance Measured<\/strong><\/td><td>Increase in within-cluster variance<\/td><td>Largest distance between points<\/td><td>Smallest distance between points<\/td><td>Mean of all pairwise distances<\/td><\/tr><tr><td><strong>Cluster Shape<\/strong><\/td><td>Compact, similar size<\/td><td>Compact, roughly round<\/td><td>Elongated, chain-like<\/td><td>Moderately compact<\/td><\/tr><tr><td><strong>Outlier Sensitivity<\/strong><\/td><td>High<\/td><td>High<\/td><td>Very high<\/td><td>Moderate<\/td><\/tr><tr><td><strong>Metric Support<\/strong><\/td><td>Euclidean only<\/td><td>Any supported metric<\/td><td>Any supported metric<\/td><td>Any supported metric<\/td><\/tr><tr><td><strong>Best For<\/strong><\/td><td>Evenly sized, compact clusters<\/td><td>Well-separated, compact clusters<\/td><td>Irregular shapes in clean data<\/td><td>A balanced choice when unsure<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p><strong><em>Picking the right linkage and cut height gets easier with guided, hands-on work. HCL GUVI\u2019s Intel &amp; IITM Pravartak Certified AI\/ML Course pairs live classes from industry experts with 20+ end-to-end projects, so ideas like clustering turn into skills you can confidently apply.<\/em><\/strong><\/p>\n\n\n<div class=\"wp-block-guvi-cta-button\" style=\"justify-content:center;\"><a href=\"https:\/\/www.guvi.in\/mlp\/artificial-intelligence-and-machine-learning?utm_source=blog&#038;utm_medium=cta_button&#038;utm_campaign=implementing-agglomerative-clustering\" class=\"guvi-btn-enquire\" target=\"_blank\" rel=\"noopener noreferrer\">Enroll in HCL GUVI&#039;s AI\/ML Course<svg class=\"guvi-btn-chevron\" style=\"transform:rotate(0deg);\" width=\"16\" height=\"16\" viewBox=\"0 0 24 24\" fill=\"none\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" aria-hidden=\"true\"><path fill-rule=\"evenodd\" clip-rule=\"evenodd\" d=\"M9.39935 18.1007C9.0674 17.7687 9.0674 17.2305 9.39935 16.8986L13.6922 12.6057C13.7508 12.5471 13.7508 12.4521 13.6922 12.3935L9.39935 8.10065C9.0674 7.7687 9.0674 7.23051 9.39935 6.89857C9.7313 6.56662 10.2695 6.56662 10.6014 6.89857L14.8943 11.1915C15.6168 11.9139 15.6168 13.0853 14.8943 13.8078L10.6014 18.1007C10.2695 18.4326 9.7313 18.4326 9.39935 18.1007Z\" fill=\"currentColor\"\/><\/svg><\/a><\/div>\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What is a Distance Matrix?<\/strong><\/h2>\n\n\n\n<p>A distance matrix records the pairwise distances between all data points or clusters.<\/p>\n\n\n\n<p>During agglomerative clustering, the algorithm updates the distance matrix after each merging step. The distance matrix plays a crucial role in deciding which clusters to merge next.<\/p>\n\n\n\n<p>Common distance metrics include:<\/p>\n\n\n\n<ol>\n<li>Euclidean distance<\/li>\n\n\n\n<li>Manhattan distance<\/li>\n\n\n\n<li>Cosine distance<\/li>\n<\/ol>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What is a Dendrogram?<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1200\" height=\"630\" src=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/image-131-1200x630.png\" alt=\"\" class=\"wp-image-133316\" srcset=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/image-131-1200x630.png 1200w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/image-131-300x158.png 300w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/image-131-768x403.png 768w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/image-131-1536x806.png 1536w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/image-131-2048x1075.png 2048w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/image-131-150x79.png 150w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" title=\"\"><\/figure>\n\n\n\n<p>A dendrogram is a tree diagram that shows how hierarchical clustering merges data points, starting with each sample on its own and ending with one cluster.<\/p>\n\n\n\n<ul>\n<li><strong>Leaves:<\/strong> Each leaf at the bottom represents a single data point.<\/li>\n\n\n\n<li><strong>Merge Height:<\/strong> The height where two branches join shows the distance between those clusters, so taller joins mean less similar groups.<\/li>\n\n\n\n<li><strong>Choosing Clusters:<\/strong> Cutting the tree with a horizontal line splits it into clusters, and a common rule of thumb is to cut across the longest vertical gap.<\/li>\n<\/ul>\n\n\n\n<p>Sklearn does not draw this tree by itself, but SciPy\u2019s <code>dendrogram<\/code> function makes it easy to plot.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Implementing Agglomerative Clustering using Sklearn<\/strong><\/h2>\n\n\n\n<p>Now let us apply sklearn\u2019s <code><a href=\"https:\/\/scikit-learn.org\/stable\/modules\/generated\/sklearn.cluster.AgglomerativeClustering.html\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">AgglomerativeClustering<\/a><\/code> to a sample dataset.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 1: Import the Required Libraries<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import matplotlib.pyplot as plt\nfrom sklearn.datasets import make_blobs\nfrom sklearn.cluster import AgglomerativeClustering<\/code><\/pre>\n\n\n\n<ul>\n<li><code>matplotlib.pyplot<\/code> draws the plot.<\/li>\n\n\n\n<li><code>make_blobs<\/code> generates a sample dataset.<\/li>\n\n\n\n<li><code>AgglomerativeClustering<\/code> is the clustering model.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 2: Create a Sample Dataset<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>X, y = make_blobs(\n    n_samples=300,\n    centers=4,\n    cluster_std=1.2,\n    random_state=42\n)<\/code><\/pre>\n\n\n\n<ul>\n<li><code>n_samples=300<\/code> creates 300 data points.<\/li>\n\n\n\n<li><code>centers=4<\/code> forms four groups.<\/li>\n\n\n\n<li><code>cluster_std=1.2<\/code> controls how spread out each group is.<\/li>\n\n\n\n<li><code>random_state=42<\/code> gives the same data on every run.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 3: Apply Agglomerative Clustering<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>model = AgglomerativeClustering(\n    n_clusters=4,\n    linkage='ward'\n)\nlabels = model.fit_predict(X)<\/code><\/pre>\n\n\n\n<ul>\n<li><code>n_clusters=4<\/code> sets the number of clusters to find.<\/li>\n\n\n\n<li><code>linkage='ward'<\/code> merges the pair of clusters that increases variance the least.<\/li>\n\n\n\n<li><code>fit_predict<\/code> fits the model and returns a cluster label for each point.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 4: Visualize the Clusters<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>plt.scatter(X&#91;:, 0], X&#91;:, 1], c=labels)\nplt.title('Agglomerative Clustering')\nplt.xlabel('Feature 1')\nplt.ylabel('Feature 2')\nplt.show()<\/code><\/pre>\n\n\n\n<ul>\n<li><code>c=labels<\/code> colors each point by its cluster.<\/li>\n\n\n\n<li>The title and axis labels make the plot easier to read.<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Output:<\/strong><\/h4>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img decoding=\"async\" width=\"1050\" height=\"750\" src=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/11\/agglomerative_clusters.png\" alt=\"\" class=\"wp-image-149264\" style=\"aspect-ratio:1.4;width:360px;height:auto\" srcset=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/11\/agglomerative_clusters.png 1050w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/11\/agglomerative_clusters-300x214.png 300w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/11\/agglomerative_clusters-768x549.png 768w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/11\/agglomerative_clusters-150x107.png 150w\" sizes=\"(max-width: 1050px) 100vw, 1050px\" title=\"\"><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Visualizing Clusters using a SciPy Dendrogram<\/strong><\/h2>\n\n\n\n<p>A SciPy dendrogram shows how the data points merge into clusters, step by step. This section reuses <code>X<\/code> and <code>plt<\/code> from the previous section.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 1: Import SciPy Libraries<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>from scipy.cluster.hierarchy import dendrogram, linkage<\/code><\/pre>\n\n\n\n<ul>\n<li><code>linkage<\/code> calculates the merge steps.<\/li>\n\n\n\n<li><code>dendrogram<\/code> draws the tree from those steps.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 2: Create the Linkage Matrix<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>linked = linkage(X, method='ward')<\/code><\/pre>\n\n\n\n<ul>\n<li><code>method='ward'<\/code> matches the linkage used in the sklearn model.<\/li>\n\n\n\n<li>The result has one row per merge, showing which two clusters joined and at what distance.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 3: Plot the Dendrogram<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>plt.figure(figsize=(10, 5))\ndendrogram(linked, no_labels=True, color_threshold=30)\nplt.axhline(y=30, color='black', linestyle='--')\nplt.title('Dendrogram')\nplt.xlabel('Data Points')\nplt.ylabel('Distance')\nplt.show()<\/code><\/pre>\n\n\n\n<ul>\n<li><code>no_labels=True<\/code> hides the 300 leaf labels, which would overlap.<\/li>\n\n\n\n<li><code>color_threshold=30<\/code> gives each cluster below distance 30 its own color.<\/li>\n\n\n\n<li><code>plt.axhline<\/code> draws a dashed cut line at distance 30.<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Output:<\/strong><\/h4>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img decoding=\"async\" width=\"1200\" height=\"600\" src=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/11\/dendrogram-1200x600.png\" alt=\"\" class=\"wp-image-149265\" style=\"aspect-ratio:2;width:532px;height:auto\" srcset=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/11\/dendrogram-1200x600.png 1200w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/11\/dendrogram-300x150.png 300w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/11\/dendrogram-768x384.png 768w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/11\/dendrogram-150x75.png 150w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/11\/dendrogram.png 1500w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" title=\"\"><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>How to Choose the Number of Clusters in Agglomerative Clustering<\/strong><\/h2>\n\n\n\n<p>Unlike k-means, agglomerative clustering does not need the cluster count in advance. The full hierarchy is built first, and the count is decided afterwards using one of the methods below.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. Cut the Dendrogram<\/strong><\/h3>\n\n\n\n<p>Cut where the vertical gap between successive merges is largest. The number of branches the line crosses is the number of clusters.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from scipy.cluster.hierarchy import fcluster\n\nlabels = fcluster(linked, t=30, criterion='distance')<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. Set a Distance Threshold<\/strong><\/h3>\n\n\n\n<p>Instead of a count, set the distance at which merging stops. The number of clusters then follows from the data.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>model = AgglomerativeClustering(\n    n_clusters=None,\n    distance_threshold=30,\n    linkage='ward'\n)\nlabels = model.fit_predict(X)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. Use the Silhouette Score<\/strong><\/h3>\n\n\n\n<p>Fit the model for several cluster counts and score each one. The score ranges from -1 to 1, and the highest value marks the best-separated clusters.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from sklearn.metrics import silhouette_score\n\nfor k in range(2, 9):\n    labels = AgglomerativeClustering(n_clusters=k).fit_predict(X)\n    print(k, silhouette_score(X, labels))<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th><strong>Method<\/strong><\/th><th><strong>Based On<\/strong><\/th><th><strong>Best For<\/strong><\/th><\/tr><\/thead><tbody><tr><td><strong>Dendrogram cut<\/strong><\/td><td>Largest merge gap<\/td><td>Visual, quick inspection<\/td><\/tr><tr><td><strong>Distance threshold<\/strong><\/td><td>A fixed merge distance<\/td><td>Unknown cluster count<\/td><\/tr><tr><td><strong>Silhouette score<\/strong><\/td><td>Cluster separation<\/td><td>Comparing candidate counts<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>When the dendrogram and the silhouette score point to the same number, the choice is well supported.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Advantages of Agglomerative Clustering<\/strong><\/h2>\n\n\n\n<p>Agglomerative clustering builds a hierarchy of clusters from the bottom up, which gives it several practical strengths.<\/p>\n\n\n\n<ul>\n<li><strong>No Centroids Needed:<\/strong> It does not start from randomly placed centers, so results stay stable across runs.<\/li>\n\n\n\n<li><strong>Flexible Cluster Count:<\/strong> Setting <code>distance_threshold<\/code> lets you skip choosing the number of clusters in advance.<\/li>\n\n\n\n<li><strong>Clear Dendrograms:<\/strong> The merge tree shows how clusters form at each distance, which makes the structure easy to read.<\/li>\n\n\n\n<li><strong>Multiple Linkage Options:<\/strong> Ward, complete, average, and single linkage control how the distance between clusters is measured.<\/li>\n\n\n\n<li><strong>Suits Smaller Datasets:<\/strong> It performs well on small to medium data, where computing all pairwise distances is affordable.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Limitations of Agglomerative Clustering<\/strong><\/h2>\n\n\n\n<p>These drawbacks matter most when data is large, noisy, or constantly growing.<\/p>\n\n\n\n<ul>\n<li><strong>High Computational Cost:<\/strong> Pairwise distances must be stored and updated, so memory and time rise quickly as the dataset grows.<\/li>\n\n\n\n<li><strong>Sensitive to Outliers:<\/strong> Extreme points can distort merges, and single linkage may chain unrelated points into one cluster.<\/li>\n\n\n\n<li><strong>Irreversible Merges:<\/strong> The algorithm is greedy, so an early merge cannot be corrected later.<\/li>\n\n\n\n<li><strong>No Predict Method:<\/strong> Sklearn offers only <code>fit<\/code> and <code>fit_predict<\/code>, so new samples cannot be assigned to existing clusters directly.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Real-World Applications of Agglomerative Clustering<\/strong><\/h2>\n\n\n\n<p>Its hierarchical output suits problems where groups contain smaller sub-groups.<\/p>\n\n\n\n<ul>\n<li><strong>Customer Segmentation:<\/strong> Separates buyers by habits such as purchase frequency and spend, and the dendrogram hints at how many segments exist.<\/li>\n\n\n\n<li><strong>Document Clustering:<\/strong> Sorts articles, reviews, or support tickets by content similarity, often using cosine distance on text vectors.<\/li>\n\n\n\n<li><strong>Image Segmentation:<\/strong> Merges neighboring pixels or regions of similar color or texture into coherent segments.<\/li>\n\n\n\n<li><strong>Bioinformatics:<\/strong> Finds genes with similar expression patterns and shows how genes or species relate through dendrograms.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><strong><em>Understanding agglomerative clustering shows you can learn complex ideas, and that is exactly the mindset AI careers reward. HCL GUVI\u2019s Intel &amp; IITM Pravartak Certified AI\/ML Course turns it into a career with live mentoring, 20+ projects, a capstone, and placement support. Take the step and start building!<\/em><\/strong><\/p>\n\n\n<div class=\"wp-block-guvi-cta-button\" style=\"justify-content:center;\"><a href=\"https:\/\/www.guvi.in\/mlp\/artificial-intelligence-and-machine-learning?utm_source=blog&#038;utm_medium=cta_button&#038;utm_campaign=implementing-agglomerative-clustering\" class=\"guvi-btn-enquire\" target=\"_blank\" rel=\"noopener noreferrer\">Enroll in HCL GUVI&#039;s AI\/ML Course<svg class=\"guvi-btn-chevron\" style=\"transform:rotate(0deg);\" width=\"16\" height=\"16\" viewBox=\"0 0 24 24\" fill=\"none\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" aria-hidden=\"true\"><path fill-rule=\"evenodd\" clip-rule=\"evenodd\" d=\"M9.39935 18.1007C9.0674 17.7687 9.0674 17.2305 9.39935 16.8986L13.6922 12.6057C13.7508 12.5471 13.7508 12.4521 13.6922 12.3935L9.39935 8.10065C9.0674 7.7687 9.0674 7.23051 9.39935 6.89857C9.7313 6.56662 10.2695 6.56662 10.6014 6.89857L14.8943 11.1915C15.6168 11.9139 15.6168 13.0853 14.8943 13.8078L10.6014 18.1007C10.2695 18.4326 9.7313 18.4326 9.39935 18.1007Z\" fill=\"currentColor\"\/><\/svg><\/a><\/div>\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>Agglomerative clustering stands out because it exposes the nested structure of a dataset instead of a single flat partition. Its results are reproducible since no centroids are initialized, though storing pairwise distances limits its reach on large data. The linkage method and distance metric shape the clusters, so the outcome reflects the technique\u2019s assumptions as much as the data.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>FAQs<\/strong><\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1795181784445\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">1. What is agglomerative clustering?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>It is a bottom-up hierarchical method that starts with every point as its own cluster and repeatedly merges the closest pair until the stopping condition is met.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1795181786580\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">2. What is the difference between agglomerative and divisive clustering?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Agglomerative clustering builds clusters by merging from individual points upward, while divisive clustering begins with one cluster and splits it downward.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1795181790107\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">3. What is the difference between K-means and agglomerative clustering?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>K-means needs a preset number of clusters and iteratively updates centroids, whereas agglomerative clustering needs no centroids and produces a full merge hierarchy.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1795181791132\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">4. How do you choose the number of clusters in agglomerative clustering?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Cut the dendrogram at the height where the vertical gap between successive merges is largest, or set a distance threshold.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1795181792159\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">5. What is the time complexity of agglomerative clustering?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>The standard approach takes O(n\u00b3) time, or O(n\u00b2 log n) with a priority queue, and requires O(n\u00b2) memory.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Agglomerative clustering groups unlabeled data by treating every observation as its own cluster and joining the most similar ones step by step. The result is a hierarchy of nested groups instead of a single flat split. This makes agglomerative clustering useful when the number of natural groups is unknown, and the relationships between them matter [&hellip;]<\/p>\n","protected":false},"author":64,"featured_media":133319,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[933],"tags":[],"views":"1969","authorinfo":{"name":"Abhishek Pati","url":"https:\/\/www.guvi.in\/blog\/author\/abhishek-pati\/"},"thumbnailURL":"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/08\/Feature-image-3-300x116.png","_links":{"self":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/113701"}],"collection":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/users\/64"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/comments?post=113701"}],"version-history":[{"count":10,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/113701\/revisions"}],"predecessor-version":[{"id":149276,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/113701\/revisions\/149276"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media\/133319"}],"wp:attachment":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media?parent=113701"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/categories?post=113701"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/tags?post=113701"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}