scipy.cluster.hierarchy.to_tree¶
-
scipy.cluster.hierarchy.
to_tree
(Z, rd=False)[source]¶ Convert a linkage matrix into an easy-to-use tree object.
The reference to the root
ClusterNode
object is returned (by default).Each
ClusterNode
object has aleft
,right
,dist
,id
, andcount
attribute. The left and right attributes point to ClusterNode objects that were combined to generate the cluster. If both are None then theClusterNode
object is a leaf node, its count must be 1, and its distance is meaningless but set to 0.Note: This function is provided for the convenience of the library user. ClusterNodes are not used as input to any of the functions in this library.
- Parameters
- Zndarray
The linkage matrix in proper form (see the
linkage
function documentation).- rdbool, optional
When False (default), a reference to the root
ClusterNode
object is returned. Otherwise, a tuple(r, d)
is returned.r
is a reference to the root node whiled
is a list ofClusterNode
objects - one per original entry in the linkage matrix plus entries for all clustering steps. If a cluster id is less than the number of samplesn
in the data that the linkage matrix describes, then it corresponds to a singleton cluster (leaf node). Seelinkage
for more information on the assignment of cluster ids to clusters.
- Returns
- treeClusterNode or tuple (ClusterNode, list of ClusterNode)
If
rd
is False, aClusterNode
. Ifrd
is True, a list of length2*n - 1
, withn
the number of samples. See the description of rd above for more details.
See also
Examples
>>> from scipy.cluster import hierarchy >>> x = np.random.rand(10).reshape(5, 2) >>> Z = hierarchy.linkage(x) >>> hierarchy.to_tree(Z) <scipy.cluster.hierarchy.ClusterNode object at ... >>> rootnode, nodelist = hierarchy.to_tree(Z, rd=True) >>> rootnode <scipy.cluster.hierarchy.ClusterNode object at ... >>> len(nodelist) 9