{ } Raw JSON

bundles / scipy 1.17.1 / scipy / cluster / hierarchy / cut_tree

function

scipy.cluster.hierarchy:cut_tree

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

Signature

def   cut_tree ( Z n_clusters = None height = None )

Summary

Given a linkage matrix Z, return the cut tree.

Parameters

Z : scipy.cluster.linkage array

The linkage matrix.

n_clusters : array_like, optional

Number of clusters in the tree at the cut point.

height : array_like, optional

The height at which to cut the tree. Only possible for ultrametric trees.

Returns

cutree : array

An array indicating group membership at each agglomeration step. I.e., for a full cut tree, in the first column each data point is in its own cluster. At the next step, two nodes are merged. Finally, all singleton and non-singleton clusters are in one group. If n_clusters or height are given, the columns correspond to the columns of n_clusters or height.

Notes

Array API Standard Support

cut_tree 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                  ⛔                     n/a                 
====================  ====================  ====================

See dev-arrayapi for more information.

Examples

from scipy import cluster
import numpy as np
from numpy.random import default_rng
rng = default_rng()
X = rng.random((50, 4))
Z = cluster.hierarchy.ward(X)
cutree = cluster.hierarchy.cut_tree(Z, n_clusters=[5, 10])
cutree[:10]

Aliases

  • scipy.cluster.hierarchy.cut_tree

Referenced by

This package