bundles / scipy 1.17.1 / scipy / cluster / hierarchy / leaders
function
scipy.cluster.hierarchy:leaders
Signature
def leaders ( Z , T ) Summary
Return the root nodes in a hierarchical clustering.
Extended Summary
Returns the root nodes in a hierarchical clustering corresponding to a cut defined by a flat cluster assignment vector T. See the fcluster function for more information on the format of T.
For each flat cluster of the flat clusters represented in the n-sized flat cluster assignment vector T, this function finds the lowest cluster node in the linkage tree Z, such that:
leaf descendants belong only to flat cluster j (i.e.,
T[p]==jfor all in , where is the set of leaf ids of descendant leaf nodes with cluster node )there does not exist a leaf that is not a descendant with that also belongs to cluster (i.e.,
T[q]!=jfor all not in ). If this condition is violated,Tis not a valid cluster assignment vector, and an exception will be thrown.
Parameters
Z: ndarrayThe hierarchical clustering encoded as a matrix. See
linkagefor more information.T: ndarrayThe flat cluster assignment vector.
Returns
L: ndarrayThe leader linkage node id's stored as a k-element 1-D array, where
kis the number of flat clusters found inT.L[j]=iis the linkage cluster node id that is the leader of flat cluster with id M[j]. Ifi < n,icorresponds to an original observation, otherwise it corresponds to a non-singleton cluster.M: ndarrayThe leader linkage node id's stored as a k-element 1-D array, where
kis the number of flat clusters found inT. This allows the set of flat cluster ids to be any arbitrary set ofkintegers.For example: if
L[3]=2andM[3]=8, the flat cluster with id 8's leader is linkage node 2.
Notes
Array API support (experimental): This function returns arrays with data-dependent shape. In JAX, at the moment of writing this makes it impossible to execute it inside @jax.jit.
Array API Standard Support
leaders 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.
==================== ==================== ==================== Library CPU GPU ==================== ==================== ==================== NumPy ✅ n/a CuPy n/a ⛔ PyTorch ✅ ⛔ JAX ⚠️ no JIT ⛔ Dask ⚠️ merges chunks n/a ==================== ==================== ====================
See
dev-arrayapifor more information.
Examples
from scipy.cluster.hierarchy import ward, fcluster, leaders from scipy.spatial.distance import pdist✓
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
✗T = fcluster(Z, 3, criterion='distance') T✓
L, M = leaders(Z, T) L✓
M
✓See also
- fcluster
for the creation of flat cluster assignments.
Aliases
-
scipy.cluster.hierarchy.leaders