Matching dendrogram with cluster number in Python's scipy.cluster.hierarchy

前端 未结 1 779
野趣味
野趣味 2021-02-04 09:22

The following code generates a simple hierarchical cluster dendrogram with 10 leaf nodes:

import scipy
import scipy.cluster.hierarchy as sch
import matplotlib.py         


        
相关标签:
1条回答
  • 2021-02-04 10:22

    Here is a solution that appropriately colors the clusters and labels the leaves of the dendrogram with the appropriate cluster name (leaves are labeled: 'point number, cluster number'). These techniques can be used independently or together. I modified your original example to include both:

    import scipy
    import scipy.cluster.hierarchy as sch
    import matplotlib.pylab as plt
    
    n=10
    k=3
    X = scipy.randn(n,2)
    d = sch.distance.pdist(X)
    Z= sch.linkage(d,method='complete')
    T = sch.fcluster(Z, k, 'maxclust')
    
    # calculate labels
    labels=list('' for i in range(n))
    for i in range(n):
        labels[i]=str(i)+ ',' + str(T[i])
    
    # calculate color threshold
    ct=Z[-(k-1),2]  
    
    #plot
    P =sch.dendrogram(Z,labels=labels,color_threshold=ct)
    plt.show()
    
    0 讨论(0)
提交回复
热议问题