In today’s world, data is everything – from businesses to healthcare, we rely on data to make informed decisions. However, sometimes the sheer volume of data can be overwhelming, and it can be challenging to derive meaningful insights from it.

This is where clustering comes in – a technique that helps group similar data points together, making it easier to identify patterns and extract useful information.

## Clustering

Clustering is a technique used in unsupervised machine learning to group data points that share similar characteristics. Essentially, a cluster refers to a group of objects in a dataset that share common features or similarities.

Clustering is a powerful tool that helps to identify patterns and relationships within a dataset. Types of

## Clustering

There are two primary types of clustering – unsupervised clustering and supervised clustering.

In unsupervised clustering, the algorithm is tasked with finding patterns and relationships without any prior knowledge of the data. Hierarchical clustering is a subcategory of unsupervised clustering that can be further divided into agglomerative and divisive clustering.

## 1) Agglomerative Hierarchical Clustering

Agglomerative hierarchical clustering is a bottom-up approach that starts by considering each data point as an individual cluster and gradually merging them into larger clusters based on similarity metrics. The similarity of two clusters is calculated by using a distance matrix, which measures the distance between two data points.

## 2) Working Mechanism of Agglomerative Hierarchical Clustering

### Agglomerative hierarchical clustering works in the following steps:

- All data points are treated as individual clusters.
- Similarity metrics are used to calculate the distance between each pair of data points.
- The two closest clusters are merged to form a larger cluster.
- The distance matrix is updated to reflect the similarity of the newly formed clusters.
- Steps 3-4 are repeated until the desired number of clusters is formed.

## 3) Dendrogram

A dendrogram is a tree-like diagram that depicts the relationship between data points and clusters in agglomerative hierarchical clustering. The dendrogram shows the order in which the clusters were merged, branching out to show the different clusters and sub-clusters.

## 4) Conclusion

Agglomerative hierarchical clustering is a powerful tool that can be used to group similar data points together and derive meaningful insights. By understanding the basics of agglomerative hierarchical clustering and the overall workings of clustering, businesses and researchers can derive deeper insights and make more informed decisions based on their data.

## 5) Divisive Hierarchical Clustering

Just like agglomerative hierarchical clustering, divisive hierarchical clustering is also a form of unsupervised clustering. The difference lies in the approach, as divisive hierarchical clustering starts with the entire dataset as a single cluster and then iteratively splits it into smaller clusters based on dissimilarity metrics.

## 6) Working Mechanism of Divisive HC

### Divisive hierarchical clustering works in the following steps:

- The entire dataset is treated as a single cluster.
- Dissimilarity metrics are used to calculate the distance between each pair of data points.
- The dataset is split into two clusters, with the data points that are most dissimilar to each other being placed in separate clusters.
- The dissimilarity metric is updated to reflect the distance between the newly formed clusters.
- Steps 3-4 are repeated until a specific number of desired clusters is formed.

Divisive hierarchical clustering can be more challenging than agglomerative hierarchical clustering, as the initial cluster is much larger and more difficult to manage. However, it can be useful for datasets where the number of clusters is not apparent and must be discovered through an iterative approach.

## 7) Steps to Perform Hierarchical Clustering

Hierarchical clustering can be performed in several different steps, and the optimal approach can vary depending on the data being analyzed.

### Example Visualization of Hierarchical Clustering

One common approach involves using distance metrics such as Euclidean distance to measure the distances between data points in a multidimensional space.

The marks of two data points are represented by a distance metric, which allows us to identify the distance between them. Once the distance matrix has been calculated, we can use a dendrogram to visualize the hierarchical clustering process.

The dendrogram displays each group of data points that are merged to form a larger cluster. Branches of the dendrogram represent the distance between clusters, and the length of the branches corresponds to the distance between the two merged clusters.

Clusters with long branches represent groups of data points that are less similar to each other, while clusters with short branches represent groups of data points that have a high degree of similarity.

## 8) Optimal Number of Clusters

Determining the optimal number of clusters is a crucial part of the clustering process. Some experts advocate that the optimal number of clusters is determined through expert knowledge and a contextual understanding of the data being analyzed.

Others suggest using methods such as the elbow method or silhouette analysis to determine the optimal number of clusters. The elbow method involves plotting the sum of squared errors (SSE) against the number of clusters.

SSE is the sum of the squared distances between each data point and its assigned cluster center. As the number of clusters increases, the SSE decreases.

The optimal number of clusters is the point at which the SSE begins to level off, forming an ‘elbow’ shape. The number of clusters at this point represents a good balance between accuracy and parsimony.

Silhouette analysis is another method used to determine the optimal number of clusters. It is based on the average distance between the data points within a cluster and the distance between the data points of neighboring clusters.

A silhouette score closer to 1 indicates that the data point is well-clustered, while a score closer to -1 indicates that the data point is an outlier.

## 9) Conclusion

Hierarchical clustering is a powerful tool that can be used to identify patterns and relationships within a dataset. The agglomerative and divisive hierarchical clustering methods provide flexibility in approach, allowing users to choose the method that best suits their data.

With careful analysis and consideration of the optimum number of clusters, hierarchical clustering can provide meaningful insights that can inform decision-making.

## 10) Hierarchical Clustering with Python

Hierarchical clustering is a popular clustering technique used in machine learning.

It can be easily implemented using Python, a widely used language in the field of data science. In this section, we will explore how to perform hierarchical clustering with Python using the agglomerative clustering algorithm.

### Plotting and Creating Clusters

The first step in performing hierarchical clustering is to import the necessary libraries. Next, we load the dataset we wish to cluster.

For this example, we will use the iris dataset, which can be downloaded using the scikit-learn library.

```
# Import necessary libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn.cluster import AgglomerativeClustering
# Load iris dataset
iris = datasets.load_iris()
X = iris.data
```

Next, we will use the AgglomerativeClustering class to create the clusters. We need to specify the number of clusters we want to form using the n_clusters parameter.

We can also specify the affinity metric and linkage criterion we want to use.

```
# Create clusters using AgglomerativeClustering method
n_clusters = 3
agc = AgglomerativeClustering(n_clusters=n_clusters, affinity='euclidean', linkage='ward')
agc.fit(X)
```

### Plotting Dendrogram

After creating the clusters, we can use the scipy.cluster hierarchy class and the dendrogram method to visualize the hierarchical clustering process.

We first need to calculate the linkage matrix using the linkage method.

```
# Calculate linkage matrix
from scipy.cluster.hierarchy import linkage
link_mat = linkage(X, method='ward')
```

We can then pass the linkage matrix to the dendrogram method to plot the dendrogram.

```
# Plot dendrogram
from scipy.cluster.hierarchy import dendrogram
plt.figure(figsize=(15,10))
dendrogram(link_mat)
plt.show()
```

The resulting dendrogram will show the relationships between the formed clusters.

## 11) Conclusion

In this article, we discussed the basics of hierarchical clustering, including its definition and types. We explored the working mechanism of both agglomerative and divisive hierarchical clustering and discussed how to determine the optimal number of clusters.

We then delved into the implementation of hierarchical clustering with Python, using the agglomerative clustering algorithm and the iris dataset as an example. We learned how to create clusters and how to plot a dendrogram to visualize the hierarchical clustering process.

Overall, hierarchical clustering is a powerful tool for identifying patterns and relationships within datasets, and its implementation in Python provides users with a flexible and accessible way to perform the clustering process.

Hierarchical clustering is a popular unsupervised machine learning technique used to group data points based on similarities or dissimilarities.

This article explored the working mechanism of both agglomerative and divisive hierarchical clustering, and we discussed how to determine the optimal number of clusters. We also discussed how to implement hierarchical clustering in Python, using the agglomerative clustering algorithm and the iris dataset as an example.

Overall, hierarchical clustering is a powerful tool for identifying patterns and relationships within datasets, and Python provides users with a flexible and accessible way to perform the clustering process.

Takeaway: with a thorough understanding of both hierarchical clustering and Python, users can perform meaningful analysis and make more informed decisions based on their data.