pub struct Tree { /* private fields */ }Expand description
A binary decision tree stored as an arena of nodes, the root at index 0.
Implementations§
Source§impl Tree
impl Tree
pub fn new() -> Self
pub fn with_capacity(capacity: usize) -> Self
pub fn is_empty(&self) -> bool
pub fn len(&self) -> usize
Sourcepub fn add_node(
&mut self,
parent: usize,
is_left: bool,
node: TreeNode,
) -> usize
pub fn add_node( &mut self, parent: usize, is_left: bool, node: TreeNode, ) -> usize
Appends node as the left or right child of parent and returns its
index. The first node added becomes the root and parent is ignored.
pub fn add_root(&mut self, root: TreeNode) -> usize
pub fn add_left_node(&mut self, parent: usize, node: TreeNode) -> usize
pub fn add_right_node(&mut self, parent: usize, node: TreeNode) -> usize
pub fn create_child(&mut self, parent: usize, left: bool) -> usize
pub fn get_root_index(&self) -> usize
pub fn get_node(&self, index: usize) -> Option<&TreeNode>
pub fn get_node_mut(&mut self, index: usize) -> Option<&mut TreeNode>
pub fn get_left_child(&self, node: &TreeNode) -> Option<&TreeNode>
pub fn get_left_child_mut(&mut self, node: &TreeNode) -> Option<&mut TreeNode>
pub fn get_right_child(&self, node: &TreeNode) -> Option<&TreeNode>
pub fn get_right_child_mut(&mut self, node: &TreeNode) -> Option<&mut TreeNode>
Sourcepub fn empty_tree(depth: usize) -> Tree
pub fn empty_tree(depth: usize) -> Tree
A complete tree skeleton of the given depth, with empty nodes.
pub fn root_details(&self) -> NodeInfos
pub fn node_details(&self, index: usize) -> NodeInfos
pub fn root_error(&self) -> usize
pub fn node_error(&self, index: usize) -> usize
pub fn node_split(&self, index: usize) -> Option<f64>
pub fn root_label(&self) -> Option<usize>
pub fn node_label(&self, index: usize) -> Option<usize>
pub fn root_feature(&self) -> Option<usize>
pub fn root_split(&self) -> Option<f64>
pub fn node_feature(&self, index: usize) -> Option<usize>
Sourcepub fn update_node(&mut self, index: usize) -> Option<NodeUpdater<'_>>
pub fn update_node(&mut self, index: usize) -> Option<NodeUpdater<'_>>
A builder that edits the node at index.
pub fn update_root(&mut self) -> Option<NodeUpdater<'_>>
Sourcepub fn node_children(&self, index: usize) -> (usize, usize)
pub fn node_children(&self, index: usize) -> (usize, usize)
(left, right) child indices of the node at index; 0 means none.
Sourcepub fn update_leaf_node(
&mut self,
index: usize,
error: (usize, usize),
) -> &mut Self
pub fn update_leaf_node( &mut self, index: usize, error: (usize, usize), ) -> &mut Self
Sets the (error, label) of the node at index.
Sourcepub fn update_subtree(
&mut self,
index: usize,
origin: &Tree,
origin_index: usize,
)
pub fn update_subtree( &mut self, index: usize, origin: &Tree, origin_index: usize, )
Copies the subtree of origin rooted at origin_index onto the node at
index, creating or detaching children as needed.
Sourcepub fn clean_orphaned_nodes(&mut self)
pub fn clean_orphaned_nodes(&mut self)
Collapses internal nodes that do not need their test: nodes marked as leaves that still have children, and nodes whose two leaves predict the same class.
Sourcepub fn nodes(&self) -> &[TreeNode]
pub fn nodes(&self) -> &[TreeNode]
The nodes of the arena, in insertion order. Index 0 is the root.
Index 0 doubles as “no child”: a child index of 0 means the node has no child on that side.
Sourcepub fn normalize_leaves(&mut self)
pub fn normalize_leaves(&mut self)
Rewrites the tree into its canonical form: a node is a leaf exactly
when its feature is None, and a leaf carries no split and no
children.
The search marks leaves with feature: Some(usize::MAX) and the depth-2
solver leaves the children of its skeleton attached, so every tree goes
through this before it reaches a caller.
Sourcepub fn validate(&self) -> Result<(), TreeError>
pub fn validate(&self) -> Result<(), TreeError>
Checks the invariant normalize_leaves establishes.
Every node is either a leaf (no feature, no split, no children, and a label to predict) or an internal node with a feature, a finite split threshold and two children.
Sourcepub fn predict_one(&self, x: &[f64]) -> Result<usize, TreeError>
pub fn predict_one(&self, x: &[f64]) -> Result<usize, TreeError>
Classifies one instance.
An internal node sends an instance left when x[feature] <= threshold
and right otherwise, as in scikit-learn. This is also how the search
partitions the data.