bundles / scipy latest / scipy / cluster / hierarchy / is_valid_linkage
function
scipy.cluster.hierarchy:is_valid_linkage
Signature
def is_valid_linkage ( Z , warning = False , throw = False , name = None ) Summary
Check the validity of a linkage matrix.
Extended Summary
A linkage matrix is valid if it is a 2-D array (type double) with rows and 4 columns. The first two columns must contain indices between 0 and . For a given row i, the following two expressions have to hold:
I.e., a cluster cannot join another cluster unless the cluster being joined has been generated.
The fourth column of Z represents the number of original observations in a cluster, so a valid Z[i, 3] value may not exceed the number of original observations.
Parameters
Z: array_likeLinkage matrix.
warning: bool, optionalWhen True, issues a Python warning if the linkage matrix passed is invalid.
throw: bool, optionalWhen True, throws a Python exception if the linkage matrix passed is invalid.
name: str, optionalThis string refers to the variable name of the invalid linkage matrix.
Returns
b: boolTrue if the inconsistency matrix is valid; False otherwise.
Notes
Array API support (experimental): If the input is a lazy Array (e.g. Dask or JAX), the return value may be a 0-dimensional bool Array. When warning=True or throw=True, calling this function materializes the array.
Array API Standard Support
is_valid_linkage 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 ⚠️ see notes ⚠️ see notes Dask ⚠️ see notes n/a ==================== ==================== ====================
See
dev-arrayapifor more information.
Examples
from scipy.cluster.hierarchy import ward, is_valid_linkage 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
✗is_valid_linkage(Z)
✓Z[3][1] = 20 # the cluster number 20 is not defined at this point is_valid_linkage(Z)✓
See also
- linkage
for a description of what a linkage matrix is.
Aliases
-
scipy.cluster.hierarchy.is_valid_linkage