cut_tree#
- scipy.cluster.hierarchy.cut_tree(Z, n_clusters=None, height=None)[source]#
Given a linkage matrix Z, return the cut tree.
- Parameters:
- Zscipy.cluster.linkage array
The linkage matrix.
- n_clustersarray_like, optional
Number of clusters in the tree at the cut point.
- heightarray_like, optional
The height at which to cut the tree. Only merges at heights strictly below height are applied, whereas
fcluster(Z, height, criterion='distance')also applies merges at height, so the two agree unless height equals a merge height. For non-monotonic linkages, see Notes.
- Returns:
- cutreearray
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
Linkages from the
'centroid'and'median'methods can be non-monotonic: a cluster can be formed at a lower distance than one of its children (seeis_monotonic). In that case the merges are ordered, and height is compared, using the monotonized heights returned bymaxdists, the same heightsfclusteruses. A merge is then never processed before the merges that formed its children.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, 4], [2, 2], [0, 0], [1, 5], [3, 6], [4, 7]]) # random