{ } Raw JSON

bundles / scipy 1.17.1 / scipy / cluster / hierarchy / optimal_leaf_ordering

function

scipy.cluster.hierarchy:optimal_leaf_ordering

source: /scipy/cluster/hierarchy.py :1499

Signature

def   optimal_leaf_ordering ( Z y metric = euclidean )

Summary

Given a linkage matrix Z and distance, reorder the cut tree.

Parameters

Z : ndarray

The hierarchical clustering encoded as a linkage matrix. See linkage for more information on the return structure and algorithm.

y : ndarray

The 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, optional

The distance metric to use in the case that y is a collection of observation vectors; ignored otherwise. See the pdist function for a list of valid distance metrics. A custom distance function can also be used.

Returns

Z_ordered : ndarray

A copy of the linkage matrix Z, reordered to minimize the distance between adjacent leaves.

Notes

Array API Standard Support

optimal_leaf_ordering 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                   ✅                     ⛔                   
Dask                  ⚠️ merges chunks      n/a                 
====================  ====================  ====================

See dev-arrayapi for more information.

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))

Aliases

  • scipy.cluster.hierarchy.optimal_leaf_ordering

Referenced by

This package