trees Module
Data structures for representing trees.
@author: drusk
-
class pml.supervised.decision_trees.trees.Node(value)[source]
A node in a tree. Holds a value and may have branches connecting it to
other nodes.
-
__init__(value)[source]
Constructs a new node.
- Args:
- value:
- The data value to be associated with this node.
-
add_child(branch, child)[source]
Creates a branch from this node to another node which will be the
child.
- Args:
- branch:
- The identifier for the branch connecting this node to the
child node.
- child: Node
- Another node which will be a child of the current node.
- Returns:
- void
-
get_all_descendants()[source]
Retrieves all descendants of the current node, i.e. nodes which can
eventually be reached by following outgoing branches from the current
node.
- Returns:
- descendants: list(Node)
-
get_branches()[source]
Retrieves all the branches to children of the current node.
- Returns:
- branches: list
- A list of all the branches to child nodes. Note that this means
branches TO this node are not included.
-
get_child(branch)[source]
Retrieves the child node connected by the specified branch.
- Args:
- branch:
- The identifier that was used to associate a child node with
the current node.
- Returns:
- child: Node
- The child node found by following the specified branch.
- Raises:
- KeyError if the specified branch does not exist.
-
get_height()[source]
Determines the node’s height, i.e. the maximum number of edges
between it and a leaf node.
- Returns:
- height: int
-
get_value()[source]
Retrieves the data value associated with this node.
- Returns:
- value:
- The value associated with this node.
-
is_leaf()[source]
Checks if this node is a leaf (has no children).
- Returns:
- is_leaf: boolean
- True if this node has no children.
-
class pml.supervised.decision_trees.trees.Tree(root_node)[source]
A tree containing nodes which are connected to each other by directed
edges.
-
__init__(root_node)[source]
Constructs a new tree.
- Args:
- root_node: Node
- The node which will be at the root of the tree.
-
get_depth()[source]
Calculates the number of nodes on the longest path from root to leaf.
- Returns:
- depth: int
-
get_leaves()[source]
Retrieves all leaf nodes from the tree.
- Returns:
- leaves: list(Node)
-
get_num_leaves()[source]
Counts the number of leaves in the tree.
- Returns:
- num_leaves: int
-
get_root_node()[source]
Retrieves the root node of the tree. This node will have only
outgoing edges (children), no incoming edges (parents).
- Returns:
- root: Node
- The root node.