{"id":202312,"date":"2024-02-20T17:20:30","date_gmt":"2024-02-20T22:20:30","guid":{"rendered":"https:\/\/ibkrcampus.com\/?p=202312"},"modified":"2024-02-20T19:23:13","modified_gmt":"2024-02-21T00:23:13","slug":"hierarchical-clustering-in-python-a-comprehensive-implementation-guide-part-iii","status":"publish","type":"post","link":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/hierarchical-clustering-in-python-a-comprehensive-implementation-guide-part-iii\/","title":{"rendered":"Hierarchical Clustering in Python: A Comprehensive Implementation Guide &#8211; Part III"},"content":{"rendered":"\n<p>Author: Updated by Chainika Thakar (Originally written by Vibhu Singh)<\/p>\n\n\n\n<p><em>Get started with an <a href=\"\/campus\/ibkr-quant-news\/hierarchical-clustering-in-python-a-comprehensive-implementation-guide-part-i\/\">overview<\/a> and <a href=\"\/campus\/ibkr-quant-news\/hierarchical-clustering-in-python-a-comprehensive-implementation-guide-part-ii\/\">key concepts<\/a> of hierarchical clustering.<\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"types-of-hierarchical-clustering\">Types of hierarchical clustering<\/h2>\n\n\n\n<p>There are two types of hierarchical clustering:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Agglomerative hierarchical clustering<\/li>\n\n\n\n<li>Divisive hierarchical clustering<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"720\" height=\"352\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2024\/02\/Types-of-hierarchical-clustering-quantinsti.png\" alt=\"\" class=\"wp-image-202320 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/02\/Types-of-hierarchical-clustering-quantinsti.png 720w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/02\/Types-of-hierarchical-clustering-quantinsti-700x342.png 700w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/02\/Types-of-hierarchical-clustering-quantinsti-300x147.png 300w\" data-sizes=\"(max-width: 720px) 100vw, 720px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 720px; aspect-ratio: 720\/352;\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"agglomerative-hierarchical-clustering\">Agglomerative Hierarchical Clustering<\/h2>\n\n\n\n<p>Agglomerative Hierarchical Clustering is the most common type of hierarchical clustering used to group objects in clusters based on their similarity. It&#8217;s a bottom-up approach where each observation starts in its own cluster, and pairs of clusters are merged as one moves up the hierarchy.<\/p>\n\n\n\n<p>Let us find out a few important subpoints in this type of clustering as shown below.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"how-does-agglomerative-hierarchical-clustering-work\">How does Agglomerative Hierarchical Clustering work?<\/h3>\n\n\n\n<p>Suppose you have data points which you want to group in similar clusters.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2024\/02\/Data-points-quantinsti.png\" alt=\"\" class=\"wp-image-202322 lazyload\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" \/><\/figure>\n\n\n\n<p><strong>Step 1: The first step is to consider each data point to be a cluster.<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2024\/02\/Each-point-as-cluster-quantinsti.png\" alt=\"\" class=\"wp-image-202323 lazyload\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" \/><\/figure>\n\n\n\n<p><strong>Step 2: Identify the two clusters that are similar and make them one cluster.<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2024\/02\/Two-clusters-merged-as-one-quantinsti.png\" alt=\"\" class=\"wp-image-202324 lazyload\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" \/><\/figure>\n\n\n\n<p><strong>Step 3: Repeat the process until only single clusters remain<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"720\" height=\"212\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2024\/02\/Repeat-quantinsti.png\" alt=\"\" class=\"wp-image-202325 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/02\/Repeat-quantinsti.png 720w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/02\/Repeat-quantinsti-700x206.png 700w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/02\/Repeat-quantinsti-300x88.png 300w\" data-sizes=\"(max-width: 720px) 100vw, 720px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 720px; aspect-ratio: 720\/212;\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"divisive-hierarchical-clustering\">Divisive Hierarchical Clustering<\/h2>\n\n\n\n<p>Divisive hierarchical clustering is not used much in solving real-world problems. It works in the opposite way of agglomerative clustering. In this, we start with all the data points as a single cluster.<\/p>\n\n\n\n<p>At each iteration, we separate the farthest points or clusters which are not similar until each data point is considered as an individual cluster. Here we are dividing the single clusters into n clusters, therefore the name divisive clustering.<\/p>\n\n\n\n<p><strong>Example of Divisive Hierarchical Clustering<\/strong><\/p>\n\n\n\n<p>In the context of trading, Divisive Hierarchical Clustering can be illustrated by starting with a cluster of all available stocks. As the algorithm progresses, it recursively divides this cluster into smaller subclusters based on dissimilarities in key financial indicators such as volatility, earnings growth, and price-to-earnings ratio. The process continues until individual stocks are isolated in distinct clusters, allowing traders to identify unique groups with similar financial characteristics for more targeted portfolio management.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-how-to-do-hierarchical-clustering-in-python\">How to do hierarchical clustering in Python?<\/h2>\n\n\n\n<p>To demonstrate the application of hierarchical clustering in Python, we will use the Iris dataset. The Iris dataset is one of the most common datasets that is used in machine learning for illustration purposes.<\/p>\n\n\n\n<p>The Iris data has three types of Iris flowers which are three classes in the dependent variable. And it contains four independent variables which are sepal length, sepal width, petal length and petal width, all in cm. We will compare the original classes with the classes formed using hierarchical clustering methods.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2024\/02\/How-to-do-hierarchical-clustering-in-Python-quantinsti.png\" alt=\"\" class=\"wp-image-202327 lazyload\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" \/><\/figure>\n\n\n\n<p>Let us take a look at the Python code with the steps below.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"step-1-import-data\">Step 1 &#8211; Import data<\/h3>\n\n\n\n<p>We will import the dataset from the sklearn library.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Import libraries\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom sklearn import datasets\n\n# Import iris data \niris = datasets.load_iris()\n\niris_data = pd.DataFrame(iris.data)\niris_data.columns = iris.feature_names\niris_data['flower_type'] = iris.target\niris_data.head()<\/pre>\n\n\n\n<p><a href=\"https:\/\/gist.github.com\/quantra-go-algo\/377d33b035ea4bd8851a167eba1a7b13#file-import_data-py\">Import_data.py\u00a0<\/a>hosted with \u2764 by\u00a0<a href=\"https:\/\/github.com\/\">GitHub<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th><br>&nbsp;<\/th><th>sepal length (cm)<\/th><th>sepal width (cm)<\/th><th>petal length (cm)<\/th><th>petal width (cm)<\/th><th>flower_type<\/th><\/tr><\/thead><tbody><tr><th>0<\/th><td>5.1<\/td><td>3.5<\/td><td>1.4<\/td><td>0.2<\/td><td>0<\/td><\/tr><tr><th>1<\/th><td>4.9<\/td><td>3.0<\/td><td>1.4<\/td><td>0.2<\/td><td>0<\/td><\/tr><tr><th>2<\/th><td>4.7<\/td><td>3.2<\/td><td>1.3<\/td><td>0.2<\/td><td>0<\/td><\/tr><tr><th>3<\/th><td>4.6<\/td><td>3.1<\/td><td>1.5<\/td><td>0.2<\/td><td>0<\/td><\/tr><tr><th>4<\/th><td>5.0<\/td><td>3.6<\/td><td>1.4<\/td><td>0.2<\/td><td>0<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-step-2-visualise-the-classes\"><strong>Step 2 &#8211; Visualise the classes<\/strong><\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">iris_X = iris_data.iloc[:, [0, 1, 2,3]].values\niris_Y = iris_data.iloc[:,4].values\n\nimport matplotlib.pyplot as plt\nplt.figure(figsize=(10, 7))\nplt.scatter(iris_X[iris_Y == 0, 0], iris_X[iris_Y == 0, 1], s=100, c='blue', label='Type 1')\nplt.scatter(iris_X[iris_Y == 1, 0], iris_X[iris_Y == 1, 1], s=100, c='yellow', label='Type 2')\nplt.scatter(iris_X[iris_Y == 2, 0], iris_X[iris_Y == 2, 1], s=100, c='green', label='Type 3')\nplt.legend()\nplt.xlabel('Sample Index')\nplt.ylabel('Euclidean Distance')\nplt.show()<\/pre>\n\n\n\n<p><a href=\"https:\/\/gist.github.com\/quantra-go-algo\/92845cbbe038c418028e0de060085aad#file-visualise_classes-py\">Visualise_Classes.py\u00a0<\/a>hosted with \u2764 by\u00a0<a href=\"https:\/\/github.com\/\">GitHub<\/a><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"720\" height=\"507\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2024\/02\/Visualise-classes-quantinsti.png\" alt=\"\" class=\"wp-image-202329 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/02\/Visualise-classes-quantinsti.png 720w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/02\/Visualise-classes-quantinsti-700x493.png 700w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/02\/Visualise-classes-quantinsti-300x211.png 300w\" data-sizes=\"(max-width: 720px) 100vw, 720px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 720px; aspect-ratio: 720\/507;\" \/><\/figure>\n\n\n\n<p>The above scatter plot shows that all three classes of Iris flowers overlap with each other. Our task is to form the cluster using hierarchical clustering and compare them with the original classes.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"step-3-create-a-dendrogram\">Step 3 &#8211; Create a dendrogram<\/h3>\n\n\n\n<p>We start by importing the library that will help to create dendrograms. The dendrogram helps to give a rough idea of the number of clusters.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import scipy.cluster.hierarchy as sc\n# Plot dendrogram\nplt.figure(figsize=(20, 7))\nplt.title(\"Dendrograms\")\n\n# Create dendrogram\nsc.dendrogram(sc.linkage(iris_X, method='ward'))\n\nplt.title('Dendrogram')\nplt.xlabel('Sample index')\nplt.ylabel('Euclidean distance')<\/pre>\n\n\n\n<p><a href=\"https:\/\/gist.github.com\/quantra-go-algo\/6884240a0882cab06603e49c6e66df12#file-create_dendrogram-py\">Create_dendrogram.py\u00a0<\/a>hosted with \u2764 by\u00a0<a href=\"https:\/\/github.com\/\">GitHub<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"720\" height=\"280\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2024\/02\/Create-dendogram-quantinsti.png\" alt=\"\" class=\"wp-image-202331 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/02\/Create-dendogram-quantinsti.png 720w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/02\/Create-dendogram-quantinsti-700x272.png 700w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/02\/Create-dendogram-quantinsti-300x117.png 300w\" data-sizes=\"(max-width: 720px) 100vw, 720px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 720px; aspect-ratio: 720\/280;\" \/><\/figure>\n\n\n\n<p>By looking at the above dendrogram, we divide the data into three clusters.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"step-4-fit-the-model\">Step 4 &#8211; Fit the model<\/h3>\n\n\n\n<p>We instantiate Agglomerative Clustering. Pass Euclidean distance as the measure of the distance between points and ward linkage to calculate clusters&#8217; proximity. Then we fit the model on our data points. Finally, we return an array of integers where the values correspond to the distinct categories using labels_ property.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">from sklearn.cluster import AgglomerativeClustering\n\ncluster = AgglomerativeClustering(\n    n_clusters=3, affinity='euclidean', linkage='ward')\n\ncluster.fit(iris_X)\nlabels = cluster.labels_\nlabels<\/pre>\n\n\n\n<p><a href=\"https:\/\/gist.github.com\/quantra-go-algo\/1c8fe8cb77a83bb5b06f0eea82b97a76#file-fit_model-py\">Fit_model.py\u00a0<\/a>hosted with \u2764 by\u00a0<a href=\"https:\/\/github.com\/\">GitHub<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n       1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 2, 2, 2, 2, 0, 2, 2, 2,\n       2, 2, 2, 0, 0, 2, 2, 2, 2, 0, 2, 0, 2, 0, 2, 2, 0, 0, 2, 2, 2, 2,\n       2, 0, 0, 2, 2, 2, 0, 2, 2, 2, 0, 2, 2, 2, 0, 2, 2, 0])<\/pre>\n\n\n\n<p>The above output shows the values of 0s, 1s, 2s, since we defined 4 clusters. 0 represents the points that belong to the first cluster and 1 represents points in the second cluster. Similarly, the third represents points in the third cluster.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-step-5-visualise-the-cluster\">Step 5 &#8211; Visualise the cluster<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">plt.figure(figsize=(10, 7))\nplt.scatter(iris_X[labels == 0, 0], iris_X[labels == 0, 1], s = 100, c = 'blue', label = 'Type 1')\nplt.scatter(iris_X[labels == 1, 0], iris_X[labels == 1, 1], s = 100, c = 'yellow', label = 'Type 2')\nplt.scatter(iris_X[labels == 2, 0], iris_X[labels == 2, 1], s = 100, c = 'green', label = 'Type 3')\nplt.legend()\nplt.xlabel('Sample Index')\nplt.ylabel('Euclidean Distance')\nplt.show()<\/pre>\n\n\n\n<p><a href=\"https:\/\/gist.github.com\/quantra-go-algo\/667fd584ace43ebaff4c0dc7d9168205#file-visualise_cluster-py\">Visualise_cluster.py\u00a0<\/a>hosted with \u2764 by\u00a0<a href=\"https:\/\/github.com\/\">GitHub<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"720\" height=\"497\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2024\/02\/Visualise-cluster-quantinsti.png\" alt=\"\" class=\"wp-image-202334 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/02\/Visualise-cluster-quantinsti.png 720w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/02\/Visualise-cluster-quantinsti-700x483.png 700w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/02\/Visualise-cluster-quantinsti-300x207.png 300w\" data-sizes=\"(max-width: 720px) 100vw, 720px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 720px; aspect-ratio: 720\/497;\" \/><\/figure>\n\n\n\n<p>There is still an overlap between Type 1 and Type 3 clusters.<\/p>\n\n\n\n<p>But if you compare with the original clusters in the Step 2 where we visualised the classes, the classification has improved quite a bit since the graph shows all three, i.e., Type 1, Type 2 and Type 3 not overlapping each other much. <\/p>\n\n\n\n<p><em>Stay tuned to learn about hierarchical clustering in trading.<\/em><\/p>\n\n\n\n<p><em>Originally posted on\u00a0<a href=\"https:\/\/blog.quantinsti.com\/hierarchical-clustering-python\/\">QuantInsti<\/a>\u00a0blog.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Agglomerative Hierarchical Clustering is the most common type of hierarchical clustering used to group objects in clusters based on their similarity.<\/p>\n","protected":false},"author":186,"featured_media":186094,"comment_status":"open","ping_status":"closed","sticky":true,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[339,343,349,338,341],"tags":[16684,851,806,16591,16685,16557,4659,1225,1224,595,6810],"contributors-categories":[13654],"class_list":{"0":"post-202312","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-science","8":"category-programing-languages","9":"category-python-development","10":"category-ibkr-quant-news","11":"category-quant-development","12":"tag-agglomerative-clustering","13":"tag-algo-trading","14":"tag-data-science","15":"tag-dendrogram","16":"tag-divisive-hierarchical-clustering","17":"tag-hierarchical-clustering","18":"tag-matplotlib","19":"tag-numpy","20":"tag-pandas","21":"tag-python","22":"tag-sklearn","23":"contributors-categories-quantinsti"},"pp_statuses_selecting_workflow":false,"pp_workflow_action":"current","pp_status_selection":"publish","acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v26.9 (Yoast SEO v27.3) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Hierarchical Clustering in Python: A Comprehensive Implementation Guide &#8211; Part III<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.interactivebrokers.com\/campus\/wp-json\/wp\/v2\/posts\/202312\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Hierarchical Clustering in Python: A Comprehensive Implementation Guide - Part III\" \/>\n<meta property=\"og:description\" content=\"Agglomerative Hierarchical Clustering is the most common type of hierarchical clustering used to group objects in clusters based on their similarity.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/hierarchical-clustering-in-python-a-comprehensive-implementation-guide-part-iii\/\" \/>\n<meta property=\"og:site_name\" content=\"IBKR Campus US\" \/>\n<meta property=\"article:published_time\" content=\"2024-02-20T22:20:30+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-02-21T00:23:13+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/03\/python-quant-laptop-coding.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1000\" \/>\n\t<meta property=\"og:image:height\" content=\"563\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Contributor Author\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Contributor Author\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\n\t    \"@context\": \"https:\\\/\\\/schema.org\",\n\t    \"@graph\": [\n\t        {\n\t            \"@type\": \"NewsArticle\",\n\t            \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/hierarchical-clustering-in-python-a-comprehensive-implementation-guide-part-iii\\\/#article\",\n\t            \"isPartOf\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/hierarchical-clustering-in-python-a-comprehensive-implementation-guide-part-iii\\\/\"\n\t            },\n\t            \"author\": {\n\t                \"name\": \"Contributor Author\",\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/person\\\/e823e46b42ca381080387e794318a485\"\n\t            },\n\t            \"headline\": \"Hierarchical Clustering in Python: A Comprehensive Implementation Guide &#8211; Part III\",\n\t            \"datePublished\": \"2024-02-20T22:20:30+00:00\",\n\t            \"dateModified\": \"2024-02-21T00:23:13+00:00\",\n\t            \"mainEntityOfPage\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/hierarchical-clustering-in-python-a-comprehensive-implementation-guide-part-iii\\\/\"\n\t            },\n\t            \"wordCount\": 737,\n\t            \"commentCount\": 0,\n\t            \"publisher\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#organization\"\n\t            },\n\t            \"image\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/hierarchical-clustering-in-python-a-comprehensive-implementation-guide-part-iii\\\/#primaryimage\"\n\t            },\n\t            \"thumbnailUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2023\\\/03\\\/python-quant-laptop-coding.jpg\",\n\t            \"keywords\": [\n\t                \"Agglomerative Clustering\",\n\t                \"Algo Trading\",\n\t                \"Data Science\",\n\t                \"Dendrogram\",\n\t                \"Divisive Hierarchical Clustering\",\n\t                \"Hierarchical Clustering\",\n\t                \"Matplotlib\",\n\t                \"NumPy\",\n\t                \"Pandas\",\n\t                \"Python\",\n\t                \"sklearn\"\n\t            ],\n\t            \"articleSection\": [\n\t                \"Data Science\",\n\t                \"Programming Languages\",\n\t                \"Python Development\",\n\t                \"Quant\",\n\t                \"Quant Development\"\n\t            ],\n\t            \"inLanguage\": \"en-US\",\n\t            \"potentialAction\": [\n\t                {\n\t                    \"@type\": \"CommentAction\",\n\t                    \"name\": \"Comment\",\n\t                    \"target\": [\n\t                        \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/hierarchical-clustering-in-python-a-comprehensive-implementation-guide-part-iii\\\/#respond\"\n\t                    ]\n\t                }\n\t            ]\n\t        },\n\t        {\n\t            \"@type\": \"WebPage\",\n\t            \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/hierarchical-clustering-in-python-a-comprehensive-implementation-guide-part-iii\\\/\",\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/hierarchical-clustering-in-python-a-comprehensive-implementation-guide-part-iii\\\/\",\n\t            \"name\": \"Hierarchical Clustering in Python: A Comprehensive Implementation Guide - Part III | IBKR Campus US\",\n\t            \"isPartOf\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#website\"\n\t            },\n\t            \"primaryImageOfPage\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/hierarchical-clustering-in-python-a-comprehensive-implementation-guide-part-iii\\\/#primaryimage\"\n\t            },\n\t            \"image\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/hierarchical-clustering-in-python-a-comprehensive-implementation-guide-part-iii\\\/#primaryimage\"\n\t            },\n\t            \"thumbnailUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2023\\\/03\\\/python-quant-laptop-coding.jpg\",\n\t            \"datePublished\": \"2024-02-20T22:20:30+00:00\",\n\t            \"dateModified\": \"2024-02-21T00:23:13+00:00\",\n\t            \"inLanguage\": \"en-US\",\n\t            \"potentialAction\": [\n\t                {\n\t                    \"@type\": \"ReadAction\",\n\t                    \"target\": [\n\t                        \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/hierarchical-clustering-in-python-a-comprehensive-implementation-guide-part-iii\\\/\"\n\t                    ]\n\t                }\n\t            ]\n\t        },\n\t        {\n\t            \"@type\": \"ImageObject\",\n\t            \"inLanguage\": \"en-US\",\n\t            \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/hierarchical-clustering-in-python-a-comprehensive-implementation-guide-part-iii\\\/#primaryimage\",\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2023\\\/03\\\/python-quant-laptop-coding.jpg\",\n\t            \"contentUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2023\\\/03\\\/python-quant-laptop-coding.jpg\",\n\t            \"width\": 1000,\n\t            \"height\": 563,\n\t            \"caption\": \"Python Quant\"\n\t        },\n\t        {\n\t            \"@type\": \"WebSite\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#website\",\n\t            \"url\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/\",\n\t            \"name\": \"IBKR Campus US\",\n\t            \"description\": \"Financial Education from Interactive Brokers\",\n\t            \"publisher\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#organization\"\n\t            },\n\t            \"potentialAction\": [\n\t                {\n\t                    \"@type\": \"SearchAction\",\n\t                    \"target\": {\n\t                        \"@type\": \"EntryPoint\",\n\t                        \"urlTemplate\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/?s={search_term_string}\"\n\t                    },\n\t                    \"query-input\": {\n\t                        \"@type\": \"PropertyValueSpecification\",\n\t                        \"valueRequired\": true,\n\t                        \"valueName\": \"search_term_string\"\n\t                    }\n\t                }\n\t            ],\n\t            \"inLanguage\": \"en-US\"\n\t        },\n\t        {\n\t            \"@type\": \"Organization\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#organization\",\n\t            \"name\": \"Interactive Brokers\",\n\t            \"alternateName\": \"IBKR\",\n\t            \"url\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/\",\n\t            \"logo\": {\n\t                \"@type\": \"ImageObject\",\n\t                \"inLanguage\": \"en-US\",\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/logo\\\/image\\\/\",\n\t                \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2024\\\/05\\\/ibkr-campus-logo.jpg\",\n\t                \"contentUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2024\\\/05\\\/ibkr-campus-logo.jpg\",\n\t                \"width\": 669,\n\t                \"height\": 669,\n\t                \"caption\": \"Interactive Brokers\"\n\t            },\n\t            \"image\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/logo\\\/image\\\/\"\n\t            },\n\t            \"publishingPrinciples\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/about-ibkr-campus\\\/\",\n\t            \"ethicsPolicy\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/cyber-security-notice\\\/\"\n\t        },\n\t        {\n\t            \"@type\": \"Person\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/person\\\/e823e46b42ca381080387e794318a485\",\n\t            \"name\": \"Contributor Author\",\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/author\\\/contributor-author\\\/\"\n\t        }\n\t    ]\n\t}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Hierarchical Clustering in Python: A Comprehensive Implementation Guide &#8211; Part III","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.interactivebrokers.com\/campus\/wp-json\/wp\/v2\/posts\/202312\/","og_locale":"en_US","og_type":"article","og_title":"Hierarchical Clustering in Python: A Comprehensive Implementation Guide - Part III","og_description":"Agglomerative Hierarchical Clustering is the most common type of hierarchical clustering used to group objects in clusters based on their similarity.","og_url":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/hierarchical-clustering-in-python-a-comprehensive-implementation-guide-part-iii\/","og_site_name":"IBKR Campus US","article_published_time":"2024-02-20T22:20:30+00:00","article_modified_time":"2024-02-21T00:23:13+00:00","og_image":[{"width":1000,"height":563,"url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/03\/python-quant-laptop-coding.jpg","type":"image\/jpeg"}],"author":"Contributor Author","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Contributor Author","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"NewsArticle","@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/hierarchical-clustering-in-python-a-comprehensive-implementation-guide-part-iii\/#article","isPartOf":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/hierarchical-clustering-in-python-a-comprehensive-implementation-guide-part-iii\/"},"author":{"name":"Contributor Author","@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/person\/e823e46b42ca381080387e794318a485"},"headline":"Hierarchical Clustering in Python: A Comprehensive Implementation Guide &#8211; Part III","datePublished":"2024-02-20T22:20:30+00:00","dateModified":"2024-02-21T00:23:13+00:00","mainEntityOfPage":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/hierarchical-clustering-in-python-a-comprehensive-implementation-guide-part-iii\/"},"wordCount":737,"commentCount":0,"publisher":{"@id":"https:\/\/ibkrcampus.com\/campus\/#organization"},"image":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/hierarchical-clustering-in-python-a-comprehensive-implementation-guide-part-iii\/#primaryimage"},"thumbnailUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/03\/python-quant-laptop-coding.jpg","keywords":["Agglomerative Clustering","Algo Trading","Data Science","Dendrogram","Divisive Hierarchical Clustering","Hierarchical Clustering","Matplotlib","NumPy","Pandas","Python","sklearn"],"articleSection":["Data Science","Programming Languages","Python Development","Quant","Quant Development"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/hierarchical-clustering-in-python-a-comprehensive-implementation-guide-part-iii\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/hierarchical-clustering-in-python-a-comprehensive-implementation-guide-part-iii\/","url":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/hierarchical-clustering-in-python-a-comprehensive-implementation-guide-part-iii\/","name":"Hierarchical Clustering in Python: A Comprehensive Implementation Guide - Part III | IBKR Campus US","isPartOf":{"@id":"https:\/\/ibkrcampus.com\/campus\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/hierarchical-clustering-in-python-a-comprehensive-implementation-guide-part-iii\/#primaryimage"},"image":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/hierarchical-clustering-in-python-a-comprehensive-implementation-guide-part-iii\/#primaryimage"},"thumbnailUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/03\/python-quant-laptop-coding.jpg","datePublished":"2024-02-20T22:20:30+00:00","dateModified":"2024-02-21T00:23:13+00:00","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/hierarchical-clustering-in-python-a-comprehensive-implementation-guide-part-iii\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/hierarchical-clustering-in-python-a-comprehensive-implementation-guide-part-iii\/#primaryimage","url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/03\/python-quant-laptop-coding.jpg","contentUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/03\/python-quant-laptop-coding.jpg","width":1000,"height":563,"caption":"Python Quant"},{"@type":"WebSite","@id":"https:\/\/ibkrcampus.com\/campus\/#website","url":"https:\/\/ibkrcampus.com\/campus\/","name":"IBKR Campus US","description":"Financial Education from Interactive Brokers","publisher":{"@id":"https:\/\/ibkrcampus.com\/campus\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/ibkrcampus.com\/campus\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/ibkrcampus.com\/campus\/#organization","name":"Interactive Brokers","alternateName":"IBKR","url":"https:\/\/ibkrcampus.com\/campus\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/logo\/image\/","url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/05\/ibkr-campus-logo.jpg","contentUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/05\/ibkr-campus-logo.jpg","width":669,"height":669,"caption":"Interactive Brokers"},"image":{"@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/logo\/image\/"},"publishingPrinciples":"https:\/\/www.interactivebrokers.com\/campus\/about-ibkr-campus\/","ethicsPolicy":"https:\/\/www.interactivebrokers.com\/campus\/cyber-security-notice\/"},{"@type":"Person","@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/person\/e823e46b42ca381080387e794318a485","name":"Contributor Author","url":"https:\/\/www.interactivebrokers.com\/campus\/author\/contributor-author\/"}]}},"jetpack_featured_media_url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/03\/python-quant-laptop-coding.jpg","_links":{"self":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts\/202312","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/users\/186"}],"replies":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/comments?post=202312"}],"version-history":[{"count":0,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts\/202312\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/media\/186094"}],"wp:attachment":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/media?parent=202312"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/categories?post=202312"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/tags?post=202312"},{"taxonomy":"contributors-categories","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/contributors-categories?post=202312"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}