cnextend : Functionality for the hiearchical tree¶
-
class
idpflex.cnextend.
ClusterNodeX
(*args, **kwargs)[source]¶ Bases:
scipy.cluster.hierarchy.ClusterNode
Extension of
ClusterNode
to accommodate a parent reference and a dictionary-like of properties.-
distance_submatrix
(dist_mat)[source]¶ Extract matrix of distances between leafs under the node.
- Parameters
dist_mat (numpy.ndarray) – Distance matrix (square or in condensed form) among all N leaves of the tree to which the node belongs to. The row indexes of dist_mat must correspond to the node IDs of the leaves.
- Returns
square distance matrix MxM between the M leafs under the node
- Return type
-
property
leaf_ids
¶ ID’s of the leafs under the tree, ordered by increasing ID.
- Returns
- Return type
-
property
leafs
¶ Find the leaf nodes under this cluster node.
- Returns
node leafs ordered by increasing ID
- Return type
-
representative
(dist_mat, similarity=<function mean>)[source]¶ Find leaf under node that is most similar to all other leaves under the node
Find the leaf that minimizes the similarity between itself and all the other leaves under the node. For instance, the average of all distances between one leaf and all the other leaves results in a similarity scalar for the leaf.
- Parameters
dist_mat (
ndarray
) – condensed or square distance matrix MxM or NxN among all N leaves in the tree or among all M leaves under the node. If dealing with the distance matrix among all leaves in the tree, self.distance_submatrix is first applied.similarity (function object) – reduction operation on a the list of distances between one leaf and the other (M-1) leaves.
- Returns
representative leaf node
- Return type
-
-
class
idpflex.cnextend.
Tree
(z=None, dm=None)[source]¶ Bases:
object
Hierarchical binary tree.
- Parameters
-
from_linkage_matrix
(z, node_class=<class 'idpflex.cnextend.ClusterNodeX'>)[source]¶ Refactored
to_tree()
converts a hierarchical clustering encoded in matrix z (by linkage) into a convenient tree object.Each node_class instance has a left, right, dist, id, and count attribute. The left and right attributes point to node_class instances that were combined to generate the cluster. If both are None then node_class is a leaf node, its count must be 1, and its distance is meaningless but set to 0.
- Parameters
node_class (
ClusterNodeX
) – the type of nodes composing the tree. Now supportsClusterNodeX
and parent classClusterNode
-
nodes_above_depth
(depth=0)[source]¶ Nodes at or above depth from the root node
- Parameters
depth (int) – Depth level starting from the root level (depth=0)
- Returns
List of nodes ordered by increasing ID. Last one is the root node
- Return type
-
idpflex.cnextend.
load_tree
(filename)[source]¶ Load a previously saved tree
- Parameters
filename (str) – File name containing the serialized tree
- Returns
Tree instance stored in file
- Return type