I made a dendrogram using scipy.cluster.hierarchy.dendrogram, using the following generated data:
a = np.random.multivariate_normal([10, 0], [[3, 1], [1, 4]], size=[100,])
b = np.random.multivariate_normal([0, 20], [[3, 1], [1, 4]], size=[50,])
c = np.random.multivariate_normal([8, 2], [[3, 1], [1, 4]], size=[80,])
X = np.concatenate((a, b, c),)
creating the linkage function:
from scipy.cluster.hierarchy import dendrogram, linkage
Z = linkage(X, 'ward')
and then:
dendrogram(
Z,
truncate_mode='lastp', # show only the last p merged clusters
p=5, # show only the last p merged clusters
show_leaf_counts=False, # otherwise numbers in brackets are counts
leaf_rotation=90.,
leaf_font_size=12.,
show_contracted=True, # to get a distribution impression in truncated branches
)
Now, I have overall 230 observations in my data that were splitted to p=5 clusters. I want to have, for each cluster, a list of all row indices of all observations that are in it. In addition, I'd like to know the structure of the hierarchy above those 5 clusters.
Thanks!