scipy.cluster.hierarchy.
complete#
- scipy.cluster.hierarchy.complete(y)[source]#
对压缩距离矩阵执行 complete/max/最远点连接。
- 参数:
- yndarray
距离矩阵的上三角部分。
pdist
的结果以这种形式返回。
- 返回值:
- Zndarray
包含层次聚类的连接矩阵。 有关其结构的更多信息,请参见
linkage
函数文档。
参见
linkage
用于层次聚类的高级创建。
scipy.spatial.distance.pdist
成对距离度量
注释
complete
除了 NumPy 之外,还对 Python Array API Standard 兼容后端提供实验性支持。 请考虑通过设置环境变量SCIPY_ARRAY_API=1
并提供 CuPy、PyTorch、JAX 或 Dask 数组作为数组参数来测试这些功能。 支持以下后端和设备(或其他能力)的组合。库
CPU
GPU
NumPy
✅
不适用
CuPy
不适用
⛔
PyTorch
✅
⛔
JAX
✅
⛔
Dask
⚠️ 合并块
不适用
有关更多信息,请参见 支持数组 API 标准。
示例
>>> from scipy.cluster.hierarchy import complete, 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 = complete(y) >>> Z array([[ 0. , 1. , 1. , 2. ], [ 3. , 4. , 1. , 2. ], [ 6. , 7. , 1. , 2. ], [ 9. , 10. , 1. , 2. ], [ 2. , 12. , 1.41421356, 3. ], [ 5. , 13. , 1.41421356, 3. ], [ 8. , 14. , 1.41421356, 3. ], [11. , 15. , 1.41421356, 3. ], [16. , 17. , 4.12310563, 6. ], [18. , 19. , 4.12310563, 6. ], [20. , 21. , 5.65685425, 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.5, criterion='distance') array([1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4], dtype=int32) >>> fcluster(Z, 4.5, criterion='distance') array([1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2], 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
可用于生成树状图的图。