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 / cut_tree

function

scipy.cluster.hierarchy._hierarchy_impl:cut_tree

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

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.

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]
✗
array([[0, 0],
       [1, 1],
       [2, 2],
       [3, 3],
       [3, 3],
       [4, 4],
       [1, 1],
       [3, 3],
       [1, 5],
       [1, 1]])

Aliases

  • scipy.cluster.hierarchy.cut_tree

Referenced by