本文簡要介紹 python 語言中 scipy.cluster.hierarchy.ward
的用法。
用法:
scipy.cluster.hierarchy.ward(y)#
在壓縮距離矩陣上執行 Ward 的鏈接。
有關返回結構和算法的更多信息,請參閱
linkage
。以下是常見的調用約定:
Z = ward(y)
在壓縮距離矩陣y
上執行 Ward 的鏈接。Z = ward(X)
使用歐幾裏得距離作為距離度量,對觀察矩陣X
執行 Ward 鏈接。
- y: ndarray
一個壓縮的距離矩陣。壓縮距離矩陣是包含距離矩陣的上三角形的平麵陣列。這是
pdist
返回的形式。或者,可以將 n 維中的 m 個觀察向量的集合作為 m × n 數組傳遞。
- Z: ndarray
層次聚類編碼為鏈接矩陣。有關返回結構和算法的更多信息,請參閱
linkage
。
參數 ::
返回 ::
例子:
>>> from scipy.cluster.hierarchy import ward, fcluster >>> from scipy.spatial.distance import pdist
首先,我們需要一個玩具數據集來玩:
x x x x x x x x x x x x
>>> 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]]
然後,我們從這個數據集中得到一個壓縮的距離矩陣:
>>> y = pdist(X)
最後,我們可以執行聚類:
>>> Z = ward(y) >>> 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. ]])
鏈接矩陣
Z
表示樹狀圖 - 有關其內容的詳細說明,請參閱scipy.cluster.hierarchy.linkage
。我們可以使用
scipy.cluster.hierarchy.fcluster
來查看給定距離閾值的每個初始點屬於哪個集群:>>> fcluster(Z, 0.9, criterion='distance') array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], dtype=int32) >>> fcluster(Z, 1.1, criterion='distance') array([1, 1, 2, 3, 3, 4, 5, 5, 6, 7, 7, 8], dtype=int32) >>> fcluster(Z, 3, criterion='distance') array([1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4], dtype=int32) >>> fcluster(Z, 9, criterion='distance') array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], dtype=int32)
此外,
scipy.cluster.hierarchy.dendrogram
可用於生成樹狀圖。
相關用法
- Python SciPy hierarchy.weighted用法及代碼示例
- Python SciPy hierarchy.maxRstat用法及代碼示例
- Python SciPy hierarchy.set_link_color_palette用法及代碼示例
- Python SciPy hierarchy.fclusterdata用法及代碼示例
- Python SciPy hierarchy.median用法及代碼示例
- Python SciPy hierarchy.DisjointSet用法及代碼示例
- Python SciPy hierarchy.correspond用法及代碼示例
- Python SciPy hierarchy.is_isomorphic用法及代碼示例
- Python SciPy hierarchy.optimal_leaf_ordering用法及代碼示例
- Python SciPy hierarchy.maxinconsts用法及代碼示例
- Python SciPy hierarchy.cut_tree用法及代碼示例
- Python SciPy hierarchy.fcluster用法及代碼示例
- Python SciPy hierarchy.to_tree用法及代碼示例
- Python SciPy hierarchy.average用法及代碼示例
- Python SciPy hierarchy.dendrogram用法及代碼示例
- Python SciPy hierarchy.num_obs_linkage用法及代碼示例
- Python SciPy hierarchy.inconsistent用法及代碼示例
- Python SciPy hierarchy.complete用法及代碼示例
- Python SciPy hierarchy.linkage用法及代碼示例
- Python SciPy hierarchy.maxdists用法及代碼示例
- Python SciPy hierarchy.is_valid_im用法及代碼示例
- Python SciPy hierarchy.centroid用法及代碼示例
- Python SciPy hierarchy.single用法及代碼示例
- Python SciPy hierarchy.is_monotonic用法及代碼示例
- Python SciPy hierarchy.cophenet用法及代碼示例
注:本文由純淨天空篩選整理自scipy.org大神的英文原創作品 scipy.cluster.hierarchy.ward。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。