bundles / scipy 2.0.0.dev0+git20260907.41eeb59 / scipy / cluster / hierarchy / _hierarchy_impl / optimal_leaf_ordering
function
scipy.cluster.hierarchy._hierarchy_impl:optimal_leaf_ordering
source: build-install/usr/lib/python3.14/site-packages/scipy/cluster/hierarchy/_hierarchy_impl.py:1408
Signature
def optimal_leaf_ordering(Z, y, metric = euclidean)Summary
Given a linkage matrix Z and distance, reorder the cut tree.
Parameters
Z: ndarrayThe hierarchical clustering encoded as a linkage matrix. See linkage for more information on the return structure and algorithm.
y: ndarrayThe condensed distance matrix from which Z was generated. Alternatively, a collection of m observation vectors in n dimensions may be passed as an m by n array.
metric: str or function, optionalThe distance metric to use in the case that y is a collection of observation vectors; ignored otherwise. See the
pdistfunction for a list of valid distance metrics. A custom distance function can also be used.
Returns
Z_ordered: ndarrayA copy of the linkage matrix Z, reordered to minimize the distance between adjacent leaves.
Examples
import numpy as np from scipy.cluster import hierarchy rng = np.random.default_rng() X = rng.standard_normal((10, 10)) Z = hierarchy.ward(X)✓
hierarchy.leaves_list(Z) hierarchy.leaves_list(hierarchy.optimal_leaf_ordering(Z, X))✗
array([6, 7, 1, 2, 5, 8, 3, 4, 0, 9], dtype=int32)
Aliases
scipy.cluster.hierarchy.optimal_leaf_ordering