代码之家  ›  专栏  ›  技术社区  ›  PDRX

scipy的替代方案。簇等级制度cut_tree()

  •  5
  • PDRX  · 技术社区  · 7 年前

    我在Python 3中做了一个凝聚层次聚类实验,发现 scipy.cluster.hierarchy.cut_tree() 未返回某些输入链接矩阵的请求簇数。所以,现在我知道 cut_tree() 功能(如所述 here ).

    然而,我需要能够获得一个平面聚类,并分配 k 我的数据点有不同的标签。你知道用什么算法得到平面聚类吗 k 来自任意输入链接矩阵的标签 Z ? 我的问题可以归结为:我如何计算什么 cut_tree() 从头开始计算没有bug吗?

    您可以使用 this dataset

    from scipy.cluster.hierarchy import linkage, is_valid_linkage
    from scipy.spatial.distance import pdist
    
    ## Load dataset
    X = np.load("dataset.npy")
    
    ## Hierarchical clustering
    dists = pdist(X)
    Z = linkage(dists, method='centroid', metric='euclidean')
    
    print(is_valid_linkage(Z))
    
    ## Now let's say we want the flat cluster assignement with 10 clusters.
    #  If cut_tree() was working we would do
    from scipy.cluster.hierarchy import cut_tree
    cut = cut_tree(Z, 10)
    

    旁注: 另一种方法可能是使用 rpy2 cutree() 作为scipy的替代品 cut_tree() ,但我从未用过。你怎么认为?

    1 回复  |  直到 7 年前
        1
  •  4
  •   σηγ    7 年前

    获得的一种方法 k 使用平面簇 scipy.cluster.hierarchy.fcluster 具有 criterion='maxclust' :

    from scipy.cluster.hierarchy import fcluster
    clust = fcluster(Z, k, criterion='maxclust')