Hierarchical Clustering in Machine Learning - GeeksforGeeks
Hierarchical Clustering in Machine Learning - GeeksforGeeks
Hierarchical clustering
Hierarchical clustering is a connectivity-based clustering model that
groups the data points together that are close to each other based on
the measure of similarity or distance. The assumption is that data points
Courses @90% Refund Data Science IBM Certification Data Science Data Science Projects Data Analy
that are close to each other are more similar or related than data points
that are farther apart.
We can look at the dendrogram and measure the height at which the
branches of the dendrogram form distinct clusters to calculate the ideal
number of clusters. The dendrogram can be sliced at this height to
determine the number of clusters.
1. Agglomerative Clustering
2. Divisive clustering
https://www.geeksforgeeks.org/hierarchical-clustering/ 2/14
1/16/25, 3:40 PM Hierarchical Clustering in Machine Learning - GeeksforGeeks
Algorithm :
Steps:
https://www.geeksforgeeks.org/hierarchical-clustering/ 3/14
1/16/25, 3:40 PM Hierarchical Clustering in Machine Learning - GeeksforGeeks
Python3
Output :
https://www.geeksforgeeks.org/hierarchical-clustering/ 4/14
1/16/25, 3:40 PM Hierarchical Clustering in Machine Learning - GeeksforGeeks
[1, 1, 1, 0, 0, 0]
Algorithm :
While merging two clusters we check the distance between two every
pair of clusters and merge the pair with the least distance/most
https://www.geeksforgeeks.org/hierarchical-clustering/ 5/14
1/16/25, 3:40 PM Hierarchical Clustering in Machine Learning - GeeksforGeeks
1. Min Distance: Find the minimum distance between any two points of
the cluster.
2. Max Distance: Find the maximum distance between any two points
of the cluster.
3. Group Average: Find the average distance between every two points
of the clusters.
4. Ward’s Method: The similarity of two clusters is based on the
increase in squared error when two clusters are merged.
Implementations code
Python3
import numpy as np
from scipy.cluster.hierarchy import dendrogram, linkage
import matplotlib.pyplot as plt
https://www.geeksforgeeks.org/hierarchical-clustering/ 6/14
1/16/25, 3:40 PM Hierarchical Clustering in Machine Learning - GeeksforGeeks
# Plot dendrogram
dendrogram(Z)
Output:
Similar Reads
https://www.geeksforgeeks.org/hierarchical-clustering/ 8/14