How to Use Hierarchical Clustering For Customer Segmentation in Python

Hierarchical clustering builds a tree of nested groups and is useful when you want to inspect several possible segmentation levels. This updated tutorial uses a deterministic customer fixture, current SciPy/scikit-learn APIs, feature scaling, and a dendrogram.
Prepare customer features
The fixture includes annual spending and visit frequency. StandardScaler prevents one unit from dominating the distance calculation.
Build the hierarchy
linkage_matrix = linkage(scaled_features, method="ward")
model = AgglomerativeClustering(n_clusters=3, linkage="ward")
segments = model.fit_predict(scaled_features)
Ward linkage merges groups that cause the smallest increase in within-cluster variance. The dendrogram shows the merge distances and helps explain a possible cut.

Customer segments are descriptive, not permanent identities. Before operational use, validate stability over time, avoid sensitive attributes and proxy discrimination, and test whether segment-specific actions create measurable value.



