<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Sangeetha Puzhakkal on Medium]]></title>
        <description><![CDATA[Stories by Sangeetha Puzhakkal on Medium]]></description>
        <link>https://medium.com/@sangeethapuzhakkal?source=rss-14151dd25b1c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*QaUTxQcjLFrngamN</url>
            <title>Stories by Sangeetha Puzhakkal on Medium</title>
            <link>https://medium.com/@sangeethapuzhakkal?source=rss-14151dd25b1c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 01:42:39 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sangeethapuzhakkal/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Hierarchical Clustering]]></title>
            <link>https://sangeethapuzhakkal.medium.com/hierarchical-clustering-7ee6d3093c5f?source=rss-14151dd25b1c------2</link>
            <guid isPermaLink="false">https://medium.com/p/7ee6d3093c5f</guid>
            <category><![CDATA[india]]></category>
            <category><![CDATA[clustering]]></category>
            <category><![CDATA[hierarchical-clustering]]></category>
            <dc:creator><![CDATA[Sangeetha Puzhakkal]]></dc:creator>
            <pubDate>Wed, 03 Feb 2021 18:05:02 GMT</pubDate>
            <atom:updated>2021-02-03T18:05:02.563Z</atom:updated>
            <content:encoded><![CDATA[<p>Clustering enables us to group data based on similar traits. Hierarchical Clustering help us in building a hierarchy of clusters and gives the flexibility in deciding the number of clusters after seeing the cluster layout. Clustering falls into unsupervised category of algorithms.</p><p>We will explore the features of Hierarchical Clustering by taking a data set having various attributes of Indian states (Data Ref:<a href="https://data.gov.in/">https://data.gov.in/</a>)).</p><p><strong>Types of Hierarchical Clustering:</strong></p><ul><li><strong>Agglomerative</strong>: Bottom up approach . Algorithm starts by considering individual data points as a cluster(n data points n clusters). Algorithm iterates through the data points by combining “similar” data points.</li><li><strong>Divisive</strong>: Top down approach. All observations are considered as one cluster and as we iterate through the data , splits are performed based on “dissimilarity” and n clusters are formed finally.</li></ul><p>Measure of “similarity” of data points and grouping is achieved using “Metric” and “Linkage”</p><p><strong>Metric/Distance function:</strong></p><p><em>How the distance between the Data Points are measured. </em>Algorithm measures the distance between the data points using any one of the following methods. Euclidean distance, Squared Euclidean, Mahalanobis Distance, Minkowski Distance are few commonly used metrics.</p><p><strong>Linkage:</strong></p><p><em>How clusters are formed. </em>The linkage of clusters are measured in terms of one of the following ways. In all the below methods, a cluster is linked to the one which returns the minimum value.</p><figure><img alt="Single Linkage: Clusters are compared by measuring the distance between the closest data points in its neighbour cluster." src="https://cdn-images-1.medium.com/max/331/1*SAYwGGfRstjGlmi4VGc3Kg.png" /><figcaption>Single Linkage</figcaption></figure><ul><li><strong><em>Single Linkage: </em></strong>Clusters are compared by measuring the distance with the closest data points in its neighbour cluster.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/320/1*3620hRcrY-SIAt5tfxLEog.png" /><figcaption>Complete Linkage</figcaption></figure><ul><li><strong><em>Complete Linkage: </em></strong>Clusters are compared by measuring the distance with the farthest data point in its neighbour cluster.</li><li><strong><em>Average Linkage: </em></strong>Distance between each pair of data points in each cluster is measured and averaged (by dividing by the number of data points) .</li><li><strong><em>Centroid Linkage: </em></strong>The distance is measured between the centroid of the clusters.</li><li><strong><em>Ward Linkage:</em></strong> Two data points or clusters with the minimum distance are combined.</li></ul><p><strong>How Algorithm works:</strong></p><p>1) Calculate the distance between each cluster (Each data point is a cluster, we start with n clusters ). n x n Distance matrix will be created. <br>2) Repeat the following steps until one cluster :<br><em> a) Combine the clusters based on Linkage criteria.<br> b) Recalculate &amp; update the Distance matrix.</em></p><p>The output of the Hierarchical Cluster is called a <strong>Dendrogram</strong>.</p><p><strong>Pros:</strong></p><p>We need not specify the number of clusters beforehand as in the case of KMeans Clustering, We can review the Dendrogram and decide the number of clusters based on our data scenario.</p><p><strong>Cons:</strong></p><p>When the data volume is high, the algorithm will become resource intensive as it needs to calculate the distance between each data point, where as in KMeans we need to calculate the distance between the data point and the centroid.</p><p><strong>Other Algorithms in this genre</strong>: KMeans Clustering (Centroid Based Clustering) , Mean-Shift Clustering, DBSCAN, GMM</p><h3>Implementation with Scipy</h3><p><strong>Data Preparation</strong></p><p>This data is prepared by wrangling public data sets from <a href="https://data.gov.in/">https://data.gov.in/</a>. Unemployment data attribute is taken from: <a href="https://www.statista.com/statistics/642853/urban-unemployement-rate-by-state-india/">https://www.statista.com/statistics/642853/urban-unemployement-rate-by-state-india/</a></p><p># Import the Packages<br>import pandas as pd<br>import numpy as np<br>import matplotlib.pyplot as plt<br>import seaborn as sns</p><p># Load the data<br>data=pd.read_csv(‘state_wise_data.csv’)</p><pre>Data columns (total 17 columns):<br>State                            34 non-null object<br>Gross_enroll_higher_sec_girls    34 non-null float64<br>Gross_enroll_higher_sec_total    34 non-null float64<br>Percent_schools_with_computer    34 non-null float64<br>Urban_unemployment_Rate          34 non-null float64<br>Electricity                      34 non-null float64<br>Drinking_water                   34 non-null float64<br>Sanitation                       34 non-null float64<br>Clean_Cooking_fuel               34 non-null float64<br>Iodized_Salt                     34 non-null float64<br>Women_Literacy                   34 non-null float64<br>Men_Literacy                     34 non-null float64<br>Immunization                     34 non-null float64<br>Knowledge_HIV_Women              34 non-null float64<br>Knowledge_HIV_Men                34 non-null float64<br>Tobacco_alcohol_women            34 non-null float64<br>Tobacco_alcohol_men              34 non-null float64</pre><p>#Create a copy of the data by removing the State Column<br>data_copy=data.iloc[:,1:]</p><p>#<strong> Scale the variables</strong><br>from sklearn.preprocessing import StandardScaler<br>scaler=StandardScaler()<br>data_scaled=scaler.fit_transform(data_copy)</p><p># <strong>Import Hierarchical Clustering Packages</strong><br>from scipy.cluster.hierarchy import linkage,dendrogram,cut_tree</p><p># <strong>Modelling</strong></p><p>plt.figure(figsize=(10,3))<br>mergings=linkage(data_scaled,method=’average’,metric=’minkowski’)<br>dendrogram(mergings, labels=tuple(data[‘State’]),leaf_rotation=90)<br>plt.show()</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/654/1*VggkMXlq_Bj8UvRiu8F3fg.png" /><figcaption>Dendron with method=’average’ &amp; metric=’minkowski’</figcaption></figure><p>plt.figure(figsize=(10,3))<br>mergings=linkage(data_scaled,method=’complete’,metric=’euclidean’)<br>dendrogram(mergings, labels=tuple(data[‘State’]),leaf_rotation=90)<br>plt.show()</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*ufyoiVOgCcapiHUDvm2NFA.png" /><figcaption>Dendron with method=’complete’ &amp; metric=’euclidean’</figcaption></figure><p>We could see that by changing the metric &amp; method how the clusters has changed. method=’complete’, metric=’euclidean’ seems to be giving a better picture of the state grouping based on my understanding of the states.</p><p>For <strong>method </strong>parameter we can try single, complete, average, weighted, centroid &amp; median.<br>For <strong>metric </strong>parameter we can try :’braycurtis’, ‘canberra’, ‘chebyshev’, ‘cityblock’, ‘correlation’, ‘cosine’, ‘dice’, ‘euclidean’, ‘hamming’, ‘jaccard’, ‘jensenshannon’, ‘kulsinski’, ‘mahalanobis’,‘matching’, ‘minkowski’, rogerstanimoto’, ‘russellrao’, ‘seuclidean’, ‘sokalmichener’, ‘sokalsneath’, <br>‘sqeuclidean’, ‘yule’.</p><p># Cut the tree at 6 clusters &amp; assign the clusters to the main data set<br>h_lables=cut_tree(mergings,n_clusters=6).reshape(-1,)<br>data[‘Cluster’]=h_lables<br>data.Cluster=data.Cluster+1<br>data[‘Cluster’].value_counts()</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/787/1*pgCp1wELkdKBtv7hCWDDoQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zq1-Wr-3TH4ufJ-XeIIrLA.png" /></figure><p>By analysing the boxplot we can see that these 6 clusters really hold differences /similarities based on the attributes. Eg: Cluster 2 is exhibiting a different pattern with respect to Unemployment Rate, Usage of Iodized Salt &amp; Alcohol/Tobacco usage by women. Cluster 6 displays a distinct pattern on “Knowledge About HIV:Women”,“Knowledge About HIV:Men”,”Alcohol/Tobacco usage by women” &amp; “Alcohol/Tobacco usage by Men”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/372/1*LxTOT0DfpoVt9TiC-oTlag.png" /><figcaption>Clustering shown Statewise</figcaption></figure><p><strong>Conclusions</strong></p><p>We could see how Hierarchical Clustering enabled us in finding the natural patterns within the dataset we have chosen. This approach in analysis will be really helpful in studies to identify the best course of action based on key characteristics of clusters.</p><p>Code available in:</p><p><a href="https://github.com/spuzhakkal/My_Projects/blob/master/Hierarchical_Clustering_Indian_States_Map.ipynb">https://github.com/spuzhakkal/My_Projects/blob/master/Hierarchical_Clustering_Indian_States_Map.ipynb</a></p><p><strong>References</strong></p><p><a href="https://docs.scipy.org/doc/scipy/reference/generated/scipy.cluster.hierarchy.linkage.html">scipy.cluster.hierarchy.linkage - SciPy v1.6.0 Reference Guide</a></p><p><a href="https://data.gov.in/">https://data.gov.in/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7ee6d3093c5f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>