當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python SciPy hierarchy.leaders用法及代碼示例

本文簡要介紹 python 語言中 scipy.cluster.hierarchy.leaders 的用法。

用法:

scipy.cluster.hierarchy.leaders(Z, T)#

返回層次聚類中的根節點。

返回與平麵集群分配向量 T 定義的切割相對應的層次聚類中的根節點。有關 T 格式的更多信息,請參見 fcluster 函數。

對於n-sized平麵簇分配向量T中表示的 平麵簇中的每個平麵簇 ,此函數在鏈接樹Z中找到最低的簇節點 ,使得:

  • leaf descendants belong only to flat cluster j (i.e., T[p]==j for all in , where is the set of leaf ids of descendant leaf nodes with cluster node )

  • there does not exist a leaf that is not a descendant with that also belongs to cluster (i.e., T[q]!=j for all not in ). If this condition is violated, T is not a valid cluster assignment vector, and an exception will be thrown.

參數

Z ndarray

層次聚類編碼為矩陣。有關詳細信息,請參閱 linkage

T ndarray

平麵集群分配向量。

返回

L ndarray

領導者鏈接節點 id 存儲為 k-element 一維數組,其中 k 是在 T 中找到的平麵集群的數量。

L[j]=i是鏈接集群節點id,它是id為M[j]的扁平集群的領導者。如果 i < ni 對應於原始觀察,否則對應於非單例集群。

M ndarray

領導者鏈接節點 id 存儲為 k-element 一維數組,其中 k 是在 T 中找到的平麵集群的數量。這允許平麵集群 ID 集是任意的 k 整數集。

例如:如果 L[3]=2M[3]=8 ,則 id 為 8 的扁平集群的領導者是鏈接節點 2。

例子

>>> from scipy.cluster.hierarchy import ward, fcluster, leaders
>>> from scipy.spatial.distance import pdist

給定一個鏈接矩陣 Z - 在將聚類方法應用於數據集 X 後獲得 - 和一個平麵聚類分配數組 T

>>> X = [[0, 0], [0, 1], [1, 0],
...      [0, 4], [0, 3], [1, 4],
...      [4, 0], [3, 0], [4, 1],
...      [4, 4], [3, 4], [4, 3]]
>>> Z = ward(pdist(X))
>>> Z
array([[ 0.        ,  1.        ,  1.        ,  2.        ],
       [ 3.        ,  4.        ,  1.        ,  2.        ],
       [ 6.        ,  7.        ,  1.        ,  2.        ],
       [ 9.        , 10.        ,  1.        ,  2.        ],
       [ 2.        , 12.        ,  1.29099445,  3.        ],
       [ 5.        , 13.        ,  1.29099445,  3.        ],
       [ 8.        , 14.        ,  1.29099445,  3.        ],
       [11.        , 15.        ,  1.29099445,  3.        ],
       [16.        , 17.        ,  5.77350269,  6.        ],
       [18.        , 19.        ,  5.77350269,  6.        ],
       [20.        , 21.        ,  8.16496581, 12.        ]])
>>> T = fcluster(Z, 3, criterion='distance')
>>> T
array([1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4], dtype=int32)

scipy.cluster.hierarchy.leaders 返回樹狀圖中作為每個平麵簇的領導者的節點的索引:

>>> L, M = leaders(Z, T)
>>> L
array([16, 17, 18, 19], dtype=int32)

(請記住,索引 0-11 指向 X 中的 12 個數據點,而索引 12-22 指向 Z 中的 11 行)

scipy.cluster.hierarchy.leaders 還返回 T 中平坦簇的索引:

>>> M
array([1, 2, 3, 4], dtype=int32)

相關用法


注:本文由純淨天空篩選整理自scipy.org大神的英文原創作品 scipy.cluster.hierarchy.leaders。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。