scipy.cluster.hierarchy.
leaves_list#
- scipy.cluster.hierarchy.leaves_list(Z)[source]#
返回葉節點 ID 的列表。
返回值對應於從左到右在樹中顯示的觀察向量索引。Z 是一個連結矩陣。
- 參數:
- Zndarray
階層式分群編碼為矩陣。Z 是一個連結矩陣。 參見
linkage
以獲得更多資訊。
- 返回值:
- leaves_listndarray
葉節點 ID 的列表。
另請參閱
dendrogram
以取得關於 dendrogram 結構的資訊。
範例
>>> from scipy.cluster.hierarchy import ward, dendrogram, leaves_list >>> from scipy.spatial.distance import pdist >>> from matplotlib import pyplot as plt
>>> 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
代表一個 dendrogram,也就是一個樹狀結構,編碼了執行分群的結構。scipy.cluster.hierarchy.leaves_list
顯示了X
資料集中的索引與 dendrogram 中葉子之間的映射>>> leaves_list(Z) array([ 2, 0, 1, 5, 3, 4, 8, 6, 7, 11, 9, 10], dtype=int32)
>>> fig = plt.figure(figsize=(25, 10)) >>> dn = dendrogram(Z) >>> plt.show()