complete — SciPy v1.16.0 Manual (original) (raw)
scipy.cluster.hierarchy.
scipy.cluster.hierarchy.complete(y)[source]#
Perform complete/max/farthest point linkage on a condensed distance matrix.
Parameters:
yndarray
The upper triangular of the distance matrix. The result ofpdist
is returned in this form.
Returns:
Zndarray
A linkage matrix containing the hierarchical clustering. See the linkage function documentation for more information on its structure.
Notes
complete has experimental support for Python Array API Standard compatible backends in addition to NumPy. Please consider testing these features by setting an environment variable SCIPY_ARRAY_API=1
and providing CuPy, PyTorch, JAX, or Dask arrays as array arguments. The following combinations of backend and device (or other capability) are supported.
See Support for the array API standard for more information.
Examples
from scipy.cluster.hierarchy import complete, fcluster from scipy.spatial.distance import pdist
First, we need a toy dataset to play with:
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]]
Then, we get a condensed distance matrix from this dataset:
Finally, we can perform the clustering:
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. ]])
The linkage matrix Z
represents a dendrogram - seescipy.cluster.hierarchy.linkage for a detailed explanation of its contents.
We can use scipy.cluster.hierarchy.fcluster to see to which cluster each initial point would belong given a distance threshold:
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)
Also, scipy.cluster.hierarchy.dendrogram can be used to generate a plot of the dendrogram.