Hierarchical Clustering
Hierarchical clustering groups observations by repeatedly joining or separating clusters. Its result depends on distance, linkage, feature scales, and the chosen cut.
Understand the idea
Agglomerative clustering starts with individual observations and merges according to its linkage rule. The example asks for two groups from a tiny one-dimensional set. Cluster numbers are arbitrary labels, not rankings or intrinsic class names. Domain interpretation and stability checks are required before treating groups as meaningful segments.
Where to try it
Run locally with scikit-learn and NumPy installed in your virtual environment (py -m pip install scikit-learn numpy). These examples are executed during course review, but the online runner does not currently preload scikit-learn. The small data is for learning the API, not evidence of production model quality.
A worked example
from sklearn.cluster import AgglomerativeClustering
labels = AgglomerativeClustering(n_clusters=2).fit_predict([[0], [1], [10], [11]])
print(labels[0] == labels[1])
print(labels[0] != labels[2])
Read the result
Both checks are True: nearby pairs share groups, and distant pairs are separated.
Try one change
Rescale one feature in a two-feature example and inspect how distance-based grouping changes.
For package options, see the scikit-learn documentation.