Skip to main content
Course/Module 4/Topic 2 of 4Beginner

Hierarchical Clustering

Explore hierarchical clustering for building a tree of clusters.

45 minBy Priygop TeamLast updated: Feb 2026

What is Hierarchical Clustering?

Hierarchical clustering builds a tree of clusters by either merging smaller clusters into larger ones (agglomerative) or splitting larger clusters into smaller ones (divisive).

Types of Hierarchical Clustering

  • Agglomerative: Bottom-up approach
  • Divisive: Top-down approach

Implementation

Example
from sklearn.cluster import AgglomerativeClustering
from scipy.cluster.hierarchy import dendrogram, linkage
from scipy.spatial.distance import pdist
import matplotlib.pyplot as plt

# Generate data
X, y_true = make_blobs(n_samples=50, centers=3, cluster_std=0.60, random_state=0)

# Agglomerative clustering
clustering = AgglomerativeClustering(n_clusters=3)
y_pred = clustering.fit_predict(X)

# Create linkage matrix for dendrogram
linkage_matrix = linkage(X, method='ward')

# Plot dendrogram
plt.figure(figsize=(10, 6))
dendrogram(linkage_matrix)
plt.title('Hierarchical Clustering Dendrogram')
plt.xlabel('Sample Index')
plt.ylabel('Distance')
plt.show()

# Visualize clusters
plt.figure(figsize=(10, 6))
plt.scatter(X[:, 0], X[:, 1], c=y_pred, s=50, cmap='viridis')
plt.title('Hierarchical Clustering Results')
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.show()

Additional Resources

Recommended Reading

  • Pattern Recognition and Machine Learning by Bishop
  • The Elements of Statistical Learning by Hastie et al.
  • Scikit-learn Clustering Documentation

Online Resources

  • K-Means Clustering Tutorial
  • Hierarchical Clustering Guide
  • PCA Explanation
Chat on WhatsApp
Priygop - Leading Professional Development Platform | Expert Courses & Interview Prep