本文簡要介紹 python 語言中 scipy.cluster.hierarchy.weighted
的用法。
用法:
scipy.cluster.hierarchy.weighted(y)#
對壓縮距離矩陣執行加權/WPGMA 鏈接。
有關返回結構和算法的更多信息,請參閱
linkage
。- y: ndarray
距離矩陣的上三角。
pdist
的結果以這種形式返回。
- Z: ndarray
包含層次聚類的鏈接矩陣。有關其結構的更多信息,請參閱
linkage
。
參數 ::
返回 ::
例子:
>>> from scipy.cluster.hierarchy import weighted, 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 = weighted(y) >>> Z array([[ 0. , 1. , 1. , 2. ], [ 6. , 7. , 1. , 2. ], [ 3. , 4. , 1. , 2. ], [ 9. , 11. , 1. , 2. ], [ 2. , 12. , 1.20710678, 3. ], [ 8. , 13. , 1.20710678, 3. ], [ 5. , 14. , 1.20710678, 3. ], [10. , 15. , 1.20710678, 3. ], [18. , 19. , 3.05595762, 6. ], [16. , 17. , 3.32379407, 6. ], [20. , 21. , 4.06357713, 12. ]])
鏈接矩陣
Z
表示樹狀圖 - 有關其內容的詳細說明,請參閱scipy.cluster.hierarchy.linkage
。我們可以使用
scipy.cluster.hierarchy.fcluster
來查看給定距離閾值的每個初始點屬於哪個集群:>>> fcluster(Z, 0.9, criterion='distance') array([ 7, 8, 9, 1, 2, 3, 10, 11, 12, 4, 6, 5], dtype=int32) >>> fcluster(Z, 1.5, criterion='distance') array([3, 3, 3, 1, 1, 1, 4, 4, 4, 2, 2, 2], dtype=int32) >>> fcluster(Z, 4, criterion='distance') array([2, 2, 2, 1, 1, 1, 2, 2, 2, 1, 1, 1], dtype=int32) >>> fcluster(Z, 6, criterion='distance') array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], dtype=int32)
此外,
scipy.cluster.hierarchy.dendrogram
可用於生成樹狀圖。
相關用法
- Python SciPy hierarchy.ward用法及代碼示例
- 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.weighted。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。