k-Means and Hierarchical Clustering

k-Means and Hierarchial clustering

Clustering is an unsupervised machine learning method used to group similar instances (data points) into clusters without having prior labels for these groups. The idea is that data in one cluster is more similar to each other than to those in other clusters. Let’s delve into two of the most popular clustering algorithms: k-Means and Hierarchical Clustering.

k-Means Clustering:

How k-Means Works:

  1. Initialization: Choose ‘k’ initial centroids, where ‘k’ is the number of clusters you want. This can be done randomly or based on some heuristic.
  2. Assignment: Assign each data point to the nearest centroid. This forms ‘k’ clusters.
  3. Update: For each cluster, compute the mean of all its data points. This mean becomes the new centroid for that cluster.
  4. Repeat: Continue the assignment and update steps until centroids do not change significantly or a certain number of iterations are reached.


  • Simple and easy to implement.
  • Efficient for large datasets.


  • The number of clusters ‘k’ must be specified beforehand.
  • Sensitive to the initial placement of centroids. It can converge to local optima. Running the algorithm multiple times with different initializations can help.
  • Assumes that clusters are spherical and equally sized, which might not always be the case.
  • Sensitive to outliers.

Hierarchical Clustering:

How Hierarchical Clustering Works:

  1. Initialization: Treat each data point as a single cluster. Hence, if there are ‘N’ data points, we have ‘N’ clusters.
  2. Agglomeration: Find the two clusters that are closest to each other and merge them into a single cluster. This reduces the total number of clusters by one.
  3. Repeat: Continue the process until only one single cluster remains.
  4. The result is a tree-like diagram called a dendrogram, which gives a multi-level hierarchy of clusters.

Two Main Approaches:

  • Agglomerative: Starts with individual data points and merges them (as described above).
  • Divisive: Start with one single cluster and divide it until each cluster only contains a single data point.


  • Does not require the number of clusters to be specified a priori.
  • Provides a dendrogram, which can be useful for understanding hierarchical relationships.


  • Generally more computationally intensive than k-means.
  • Doesn’t work well for large datasets.
  • Once clusters are merged in agglomerative clustering (or split in divisive), the decision cannot be undone in subsequent steps.

Practical Considerations:

  • Distance Metrics: Both clustering methods require a way to compute the distance or similarity between data points. Common metrics include Euclidean, Manhattan, and cosine similarity.
  • Linkage Criteria in Hierarchical Clustering: Defines the distance between groups of data points. Common methods include:
    • Single Linkage: Distance between the closest members of two clusters.
    • Complete Linkage: Distance between the farthest members of two clusters.
    • Average Linkage: Average distance between members of two clusters.
    • Ward’s Linkage: Minimizes the variance between clusters.
  • Determining Number of Clusters: For k-means, methods like the elbow method or silhouette analysis can help. For hierarchical clustering, examining the dendrogram can provide insights.

In summary, clustering algorithms like k-means and hierarchical clustering are valuable tools for exploratory data analysis, especially when we don’t have prior labels and want to identify inherent groupings within the data.

Leave a Reply

Your email address will not be published. Required fields are marked *