This is a pre-release version (2.0.0.dev0+git20260907.41eeb59).Go to latest (1.18.0)
{ } Raw JSON

bundles / scipy 2.0.0.dev0+git20260907.41eeb59 / scipy / cluster / hierarchy / _hierarchy_impl / is_monotonic

function

scipy.cluster.hierarchy._hierarchy_impl:is_monotonic

source: build-install/usr/lib/python3.14/site-packages/scipy/cluster/hierarchy/_hierarchy_impl.py:1904

Signature

def is_monotonic(Z)

Summary

Return True if the linkage passed is monotonic.

Extended Summary

The linkage is monotonic if for every cluster and joined, the distance between them is no less than the distance between any previously joined clusters.

Parameters

Z : ndarray

The linkage matrix to check for monotonicity.

Returns

b : bool

A boolean indicating whether the linkage is monotonic.

Examples

from scipy.cluster.hierarchy import median, ward, is_monotonic
from scipy.spatial.distance import pdist
✓
By definition, some hierarchical clustering algorithms - such as `scipy.cluster.hierarchy.ward` - produce monotonic assignments of samples to clusters; however, this is not always true for other hierarchical methods - e.g. `scipy.cluster.hierarchy.median`. Given a linkage matrix ``Z`` (as the result of a hierarchical clustering method) we can test programmatically whether it has the monotonicity property or not, using `scipy.cluster.hierarchy.is_monotonic`:
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_monotonic(Z)
✗
np.True_
Z = median(pdist(X))
✓
Z
is_monotonic(Z)
✗
np.False_
Note that this method is equivalent to just verifying that the distances in the third column of the linkage matrix appear in a monotonically increasing order.

See also

linkage

for a description of what a linkage matrix is.

Aliases

  • scipy.cluster.hierarchy.is_monotonic