Loading…
DS · Clustering · Program 24 · In the record
Hierarchical clustering with dendrogram.
1import matplotlib.pyplot as plt2from scipy.cluster.hierarchy import linkage, dendrogram, fcluster3from sklearn.datasets import make_blobs4 5X, _ = make_blobs(n_samples=50, centers=3, cluster_std=2.5, random_state=7)6Z = linkage(X, method='ward')7print("Cluster labels:", fcluster(Z, t=3, criterion='maxclust'))8 9dendrogram(Z)10plt.title("Hierarchical Clustering Dendrogram")11plt.xlabel("Sample index")12plt.ylabel("Distance")13plt.show()Cluster labels: [3 1 3 2 3 3 3 1 2 1 3 1 2 2 3 2 3 3 2 1 3 3 1 2 3 3 3 3 1 3 1 2 1 2 3 3 1 3 2 3 1 3 2 1 3 1 3 1 1 1]

Run program 24 in your browser
Python 3.14 · Pyodide. Change the code or the input, run it, break it, and see what happens.