Dendrogram

A dendrogram is a tree-like diagram that records the sequence of merges (or splits) performed by a hierarchical clustering algorithm. It's one of the most intuitive ways to visualize how clusters form step by step.

How to Read a Dendrogram

  • Each leaf at the bottom represents a single data point.
  • Each merge point (called a "node") shows two clusters being combined.
  • The height of a merge represents the distance (or dissimilarity) between the two clusters being joined.

Plotting a Dendrogram in Python

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

Z = linkage(data, method="ward")
dendrogram(Z)
plt.title("Dendrogram")
plt.xlabel("Data Points")
plt.ylabel("Distance")
plt.show()

Choosing the Number of Clusters

One of the most useful things a dendrogram tells you is how many clusters to pick. By drawing a horizontal line across the tallest vertical lines that aren't crossed by any merge, you can count how many clusters that cut produces.

A good rule of thumb is to cut the dendrogram where the vertical distance between two consecutive merges is largest - this usually indicates the most natural grouping in the data.

Coming Up Next

With clustering covered, the course now shifts to another major unsupervised learning theme - reducing the number of features in your data through Dimensionality Reduction.

Ready to Master Data Science?

Join Uncodemy's Data Science Course and build real, job-ready skills with expert mentors.

Explore Course