I am trying to visualise clusters in a dendrogram. I would usually use scipy.cluster.hierarchy.dendrogram
to visualise them, but since I've created those clusters with my own logic (below), I am having trouble to visualise clustered dimensions.
I've tried to use dendrogram(link_color_func=...)
to assign the same link colour to dimensions of the same cluster, but I am struggling to convert the index passed into link_color_func
to the clusters (dim_clusters
). Perhaps there's even a better way to visualise those clusters.
# x.shape = (num dimensions, num observations)
z = hac.linkage(x.T, method='complete', metric='correlation')
cluster_ids = hac.fcluster(z, 1.0, criterion='distance')
# Try different clustering to limit number of clusters
if cluster_ids.max() > max_clusters:
cluster_ids = hac.fcluster(z, max_clusters, criterion='maxclust')
# Get original dimension's indices.
for c in np.unique(cluster_ids):
ixs = np.where(cluster_ids == c).tolist()
dim_clusters.append(ixs)
pythonscipyhierarchical-clusteringdendrogram