SwadhyAI Notes

All Python notes

Hierarchical Clustering 🔗

Unsupervised Learning

Hierarchical clustering groups similar data points into clusters. Unlike K-Means, you don't need to specify the number of clusters beforehand. It builds a hierarchy of clusters.

✨ Agglomerative Clustering

This is a "bottom-up" approach:

  1. Start with each data point as its own cluster.
  2. Find the two closest clusters.
  3. Merge them into a single cluster.
  4. Repeat until only one big cluster remains.

🌳 The Dendrogram

A dendrogram is a tree diagram that shows the hierarchical relationship between data. The height of the branches represents the distance between clusters.

python
import matplotlib.pyplot as plt
from scipy.cluster.hierarchy import dendrogram, linkage

# Data points
x = [4, 5, 10, 4, 3, 11, 14 , 6, 10, 12]
y = [21, 19, 24, 17, 16, 25, 24, 22, 21, 21]
data = list(zip(x, y))

# Calculate linkage (distance between clusters)
linkage_data = linkage(data, method='ward', metric='euclidean')

# Plot dendrogram
dendrogram(linkage_data)
plt.show()

💡 Interpreting the Dendrogram

You can choose the number of clusters by drawing a horizontal line across the dendrogram. The number of vertical lines it intersects is the number of clusters.