What Is Hierarchical Clustering In Machine Learning?
4.9 out of 5 based on 13456 votesLast updated on 11th Dec 2025 29.5K Views
- Bookmark
Machine learning helps computers learn from data and make smart choices. One key idea in this field is clustering.
Machine learning helps computers learn from data and make smart choices. One key idea in this field is clustering. It means putting similar things together. It helps find patterns and makes big data easy to study.
Hierarchical clustering is a special type of clustering. It works in small steps. First, each item starts as its own group. Then, the most similar groups are joined together. This keeps going until everything becomes one big group. The result looks like a tree chart called a dendrogram. It shows how groups come together.
You can learn this in a Machine Learning Online Course. The course explains clustering with easy examples. You also learn how it helps in real life. For example, you can group customers by habits, sort photos, or study research data. These skills help you understand data better and make smart decisions.
Understanding Hierarchical Clustering
Hierarchical clustering means forming groups step by step. First, each data point is treated as one small group. Then, groups that look most alike are joined together. This goes on until one big group is made that holds all the data.
The final tree helps people see how near or far one data point is from another. It shows relationships between points and helps to understand patterns that may not be clear at first. Many companies use it for things like customer studies, product research, and even medical data analysis.
How Hierarchical Clustering Works?
Hierarchical clustering has two main parts. The first part finds how near or far each data point is from another. This is done using a distance formula. If the number is small, the points are close and belong together.
The second part joins these close points into one group. The same steps keep repeating until all points come together into one big group. When this happens, the result can be seen in a tree-like figure.
This figure helps to find links between data and see which points are most alike. It is easy to read and gives a clear view of how the data fits together.
Dendrogram in Hierarchical Clustering
A dendrogram looks like a tree chart. It shows how groups were made at each step. Each branch stands for one cluster or one small group of data.
If the branch is short, the data points are very similar. If the branch is tall, they are very different. You can cut the dendrogram at any level to decide how many clusters you need. This helps when working with very large data sets.
For example, if you are studying customer behavior, you can use the dendrogram to find groups of customers with similar choices. If you work with medical data, it helps to find how patients with similar results are connected.
Types of Hierarchical Clustering
There are two main types of hierarchical clustering:
-
Agglomerative Clustering
This type starts by keeping every data point as its own small cluster. Then it joins the closest ones step by step until only one cluster remains. It is also called a bottom-up approach. -
Divisive Clustering
This type starts with all data points in one cluster. Then it slowly breaks them into smaller clusters step by step.
Agglomerative clustering is more common because it is simple and easier to understand.
1. Hierarchical Agglomerative Clustering (HAC)
Also known as the bottom-up approach, Hierarchical Agglomerative Clustering starts by treating each data point as a separate cluster. These clusters are then merged step by step based on their similarity until only one large cluster remains.
Workflow for HAC
- Start with each data point as its own cluster.
- Calculate the distance between all clusters.
- Merge the two closest clusters based on minimum distance.
- Update the distance matrix after merging.
- Repeat the process until only one cluster remains.
- Visualize the cluster hierarchy using a dendrogram.
Implementation Example
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import AgglomerativeClustering
from scipy.cluster.hierarchy import dendrogram
from sklearn.datasets import make_blobs
# Sample data
X, _ = make_blobs(n_samples=30, centers=3, cluster_std=10, random_state=42)
# Model
clustering = AgglomerativeClustering(n_clusters=3)
labels = clustering.fit_predict(X)
# Create dendrogram
agg = AgglomerativeClustering(distance_threshold=0, n_clusters=None)
agg.fit(X)
def plot_dendrogram(model, **kwargs):
counts = np.zeros(model.children_.shape[0])
n_samples = len(model.labels_)
for i, merge in enumerate(model.children_):
current_count = 0
for child_idx in merge:
if child_idx < n_samples:
current_count += 1
else:
current_count += counts[child_idx - n_samples]
counts[i] = current_count
linkage_matrix = np.column_stack([model.children_, model.distances_, counts]).astype(float)
dendrogram(linkage_matrix, **kwargs)
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(14, 6))
ax1.scatter(X[:, 0], X[:, 1], c=labels, cmap='viridis', s=70)
ax1.set_title("Agglomerative Clustering")
plt.sca(ax2)
plot_dendrogram(agg, truncate_mode='level', p=5)
plt.title("Hierarchical Clustering Dendrogram")
plt.show()
Output:
- The left plot shows the clustered data.
- The right plot shows the dendrogram illustrating cluster merging.
2. Hierarchical Divisive Clustering
Also called the top-down approach, Divisive Clustering begins with all data points in one large cluster and then splits them into smaller clusters recursively.
Workflow for Divisive Clustering
- Start with all data points in one big cluster.
- Split the cluster into two smaller clusters based on dissimilarity.
- Repeat splitting for the largest cluster until desired clusters are achieved.
- Continue until every data point forms its own cluster.
- Use a dendrogram to visualize the splitting process.
Implementation Example
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
from sklearn.datasets import make_blobs
from scipy.cluster.hierarchy import dendrogram, linkage
# Create data
X, _ = make_blobs(n_samples=30, centers=5, cluster_std=10, random_state=42)
def divisive_clustering(data, max_clusters=3):
clusters = [data]
while len(clusters) < max_clusters:
cluster_to_split = max(clusters, key=lambda x: len(x))
clusters.remove(cluster_to_split)
kmeans = KMeans(n_clusters=2, random_state=42).fit(cluster_to_split)
cluster1 = cluster_to_split[kmeans.labels_ == 0]
cluster2 = cluster_to_split[kmeans.labels_ == 1]
clusters.extend([cluster1, cluster2])
return clusters
clusters = divisive_clustering(X, max_clusters=3)
plt.figure(figsize=(12, 5))
plt.subplot(1, 2, 1)
colors = ['r', 'g', 'b']
for i, cluster in enumerate(clusters):
plt.scatter(cluster[:, 0], cluster[:, 1], s=50, c=colors[i], label=f'Cluster {i+1}')
plt.title('Divisive Clustering Result')
plt.legend()
linked = linkage(X, method='ward')
plt.subplot(1, 2, 2)
dendrogram(linked, orientation='top', distance_sort='descending', show_leaf_counts=True)
plt.title('Hierarchical Clustering Dendrogram')
plt.tight_layout()
plt.show()
Output:
- The first plot shows clusters formed after splitting.
- The second plot represents the hierarchical structure using a dendrogram.
Computing Distance Matrix
Before we put data into groups, we must see how close or far each point is from the others. We do this using a distance matrix. A distance matrix is like a table. It shows the distance between every pair of data points. Small numbers mean the points are near each other. Big numbers mean they are far apart. This step helps the computer know which data points should be in the same group later.
Point A | Point B | Distance |
1 | 2 | 1.5 |
1 | 3 | 2.0 |
2 | 3 | 1.0 |
3 | 4 | 2.5 |
4 | 5 | 3.0 |
This table helps to start the clustering process. It guides which points should join first.
Learning in Different Cities
If you live in Chennai, you can join a Machine Learning Course in Chennai to learn hierarchical clustering. The course covers both theory and practice. You learn to use clustering tools and create dendrograms. Trainers guide students through real examples. You also work on small projects to see how data forms groups in real life.
In Bangalore, the Machine Learning Course in Bangalore focuses on practical industry work. You learn how clustering is used in business to study customer data and patterns. The course includes live projects. Trainers explain how companies use clustering to make better decisions. Bangalore is a good place to learn because it has many tech firms and skilled teachers.
If you are from Delhi, the Machine Learning Course in Delhi uses simple examples to teach every concept. Teachers explain each step slowly and clearly. You learn to build clusters, draw dendrograms, and understand distance matrices. It is perfect for students who are new to data science.
In Hyderabad, the Machine Learning Training in Noida gives full training on clustering. You learn to use tools like Python and R to handle real data. Hyderabad has many analytics companies, so students get good exposure. The course helps you build the right skills for jobs in data and machine learning.
You May Also Read:
Artificial Intelligence and Machine Learning
Machine Learning and Deep Learning
Machine Learning Interview Questions and Answers
Why Hierarchical Clustering Matters?
Hierarchical clustering helps find patterns that are not easy to see. It helps businesses know how customers behave. It helps doctors group patients by symptoms. It helps scientists group animals by their features. It can be used in many fields because it gives a clear visual picture of data groups. The dendrogram helps make better choices based on data.
Other Related Course:
Artificial Intelligence Online Training
Conclusion
Hierarchical clustering is a method that helps to group data step by step. It has two main types called agglomerative and divisive. It uses a dendrogram to show how clusters are formed. You can learn it easily in a Machine Learning Course. It is useful for many jobs and helps understand data better. It is simple but very powerful for anyone who wants to learn data science and machine learning.
Subscribe For Free Demo
Free Demo for Corporate & Online Trainings.