Clustering is an essential method in machine learning where we gather comparative informationĀ andĀ focus together based on their characteristics. Out of all the clustering methods, Hierarchical Clustering is special because it can create a hierarchy of relationships among data points. In this article, we will learn about Hierarchical Clustering in Python. We will understand what is Hierarchical Clustering in Python, how it works, its pros and cons, and how to use it using well-known libraries.

## What is Hierarchical Clustering?

Hierarchical Clustering is a way of grouping things together based on similarities. It makes a tree-shaped structure called a dendrogram. In K-Means, you have to decide how many clusters you want beforehand. But with Hierarchical Clustering, you can create clusters with different levels of detail.

## Types of Hierarchical Clustering

There are two primary types of Hierarchical Clustering:

### 1. Agglomerative Hierarchical Clustering:

Agglomerative Hierarchical Clustering begins by treating each data point as its own separate group. This process combines clusters together one at a time until all the data points are in just one cluster. The process keeps going until every single data point is in the same group or until a certain number of groups is reached.

The **steps included in Agglomerative Hierarchical Clustering** are as follows:

- Start by considering each individual piece of information as a separate group.
- Find out how similar or different each pair of clusters are by measuring the distance between them.
- Combine the two clusters that are closest together into one single cluster.
- Calculate the distance again between the new group and all the other groups.
- Repeat the merging and distance recalculation steps until the desired number of clusters is reached.
- The outcome is a tree-shaped structure called a dendrogram that shows how clusters are related in a hierarchy.

### 2. Divisive Hierarchical Clustering:

Divisive Hierarchical Clustering is different from Agglomerative Hierarchical Clustering. It begins by putting all data points together in one group and then keeps separating them into smaller groups until each data point is in its own group.

The **steps involved in Divisive Hierarchical Clustering** are as follows:

- Begin with all the data points in one gather.
- Find out how far apart each data point is in the cluster.
- Divide the cluster into two smaller clusters depending on how different the data points are from each other.
- Keep splitting the data points and calculating the distances until each data point is its own cluster.
- Divisive Hierarchical Clustering creates a dendrogram that looks like Agglomerative Hierarchical Clustering but with the clusters arranged in the opposite order.

In real life, people often use Agglomerative Hierarchical Clustering because it is faster and usually works well with most sets of data. Agglomerative Hierarchical Clustering can be easily done using well-known Python libraries like sci-kit-learn and scipy. This makes it a versatile and easy-to-use clustering technique in analyzing data and machine learning applications.

## Advantages and Disadvantages of Hierarchical Clustering

### Advantages:

- You don’t need to say how many groups there are before starting, which allows you to explore the data more freely.
- Creates a diagram called a dendrogram that shows how data points are related to each other in a hierarchical way.
- This is good for datasets that are not too big or too small (medium size).

### Disadvantages:

- Computers can take a long time and need a lot of memory to process big sets of data.
- Being easily affected by loud sounds and unusual data points, which can make the grouping less accurate.

## Hierarchical Clustering Algorithms

Hierarchical Clustering uses different ways to measure the distance between groups when they are combined. The most common linkage methods include:

### 1. Single Linkage:

Find the smallest distance between two points in each group.

### 2. Complete Linkage:

Think about the farthest distance between any two points in the two groups.

### 3. Average Linkage:

Calculates the typical distance between all pairs of points in both clusters.

### 4. Ward Linkage:

Reduces the differences within the merged groups.

### 5. Centroid Linkage:

Calculates the separation between the centroids of the two clusters.

## Hierarchical Clustering in Python

Python has great resources for Hierarchical Clustering through different libraries. Some of the most commonly used libraries for implementing Hierarchical Clustering are:

### 1. sci-kit-learn:

A widely-used machine learning library that provides easy-to-use functions for Hierarchical Clustering and other clustering algorithms.

### 2. scipy:

A library for scientific computing that has helpful tools for making dendrograms and doing Hierarchical Clustering more effectively.

## Implementation of Hierarchical Clustering in Python

Using Hierarchical Clustering in Python is easy with the support of commonly used libraries such as sci-kit-learn and scipy. In this part, we will go through a process using a small set of data and look at the dendrogram to understand the clustering order.

### Step 1: Import the Required Libraries

First, bring in the libraries we need to work with data, create visualizations, and use Hierarchical Clustering.

### Step 2: Generate or Load the Dataset

To explain this, we will make a pretend dataset using the make_blobs tool from sci-kit-learn. Instead, you can use your own dataset in a format that works with NumPy or pandas.

### Step 3: Perform Hierarchical Clustering

Afterwards, use the linkage function from scipy to do Hierarchical Clustering. The linkage function measures the distances between data points and gives a linkage matrix. This matrix is used to create the dendrogram.

### Step 4: Visualize the Dendrogram

To understand how the clusters are organized, you can use the dendrogram function from scipy to create a visual representation of the hierarchical structure.

### Step 5: Determine the Number of Clusters

To determine how many groups to use, find a flat line in the tree diagram that intersects with the vertical lines. The number of vertical lines that cross the horizontal line shows how many clusters there are.

### Step 6: Perform Clustering

After you figure out how many groups there are, you can use the f-cluster function from scipy to do the last step of grouping and match the data points to these groups.

### Step 7: Visualize the Clusters

Finally, use Matplotlib to show the clusters in different colors.

In this program, we used the linkage function from scipy in Python to make a dendrogram and find out the hierarchical connections between data points through Hierarchical Clustering. By looking at the dendrogram, we learned about the way things are grouped together, and then we used a special function to group them again in a final way.

## Conclusion

Hierarchical Clustering is a useful clustering method that allows you to easily explore how things are related to each other. In this article, we talked about how Hierarchical Clustering works, the good things about it, and the not-so-good things. We also discussed different ways to combine clusters. We also talked about how to use Hierarchical Clustering in Python using common libraries like sci-kit-learn and scipy. When you learn more about data analysis and machine learning, Hierarchical Clustering can be a useful tool that helps you understand and find patterns in complicated data.