Skip to main content

Tree

Struct Tree 

Source
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

Source

pub fn new() -> Self

Source

pub fn with_capacity(capacity: usize) -> Self

Source

pub fn is_empty(&self) -> bool

Source

pub fn len(&self) -> usize

Source

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.

Source

pub fn add_root(&mut self, root: TreeNode) -> usize

Source

pub fn add_left_node(&mut self, parent: usize, node: TreeNode) -> usize

Source

pub fn add_right_node(&mut self, parent: usize, node: TreeNode) -> usize

Source

pub fn create_child(&mut self, parent: usize, left: bool) -> usize

Source

pub fn get_root_index(&self) -> usize

Source

pub fn get_node(&self, index: usize) -> Option<&TreeNode>

Source

pub fn get_node_mut(&mut self, index: usize) -> Option<&mut TreeNode>

Source

pub fn get_left_child(&self, node: &TreeNode) -> Option<&TreeNode>

Source

pub fn get_left_child_mut(&mut self, node: &TreeNode) -> Option<&mut TreeNode>

Source

pub fn get_right_child(&self, node: &TreeNode) -> Option<&TreeNode>

Source

pub fn get_right_child_mut(&mut self, node: &TreeNode) -> Option<&mut TreeNode>

Source

pub fn empty_tree(depth: usize) -> Tree

A complete tree skeleton of the given depth, with empty nodes.

Source

pub fn root_details(&self) -> NodeInfos

Source

pub fn node_details(&self, index: usize) -> NodeInfos

Source

pub fn root_error(&self) -> usize

Source

pub fn node_error(&self, index: usize) -> usize

Source

pub fn node_split(&self, index: usize) -> Option<f64>

Source

pub fn root_label(&self) -> Option<usize>

Source

pub fn node_label(&self, index: usize) -> Option<usize>

Source

pub fn root_feature(&self) -> Option<usize>

Source

pub fn root_split(&self) -> Option<f64>

Source

pub fn node_feature(&self, index: usize) -> Option<usize>

Source

pub fn update_node(&mut self, index: usize) -> Option<NodeUpdater<'_>>

A builder that edits the node at index.

Source

pub fn update_root(&mut self) -> Option<NodeUpdater<'_>>

Source

pub fn node_children(&self, index: usize) -> (usize, usize)

(left, right) child indices of the node at index; 0 means none.

Source

pub fn update_leaf_node( &mut self, index: usize, error: (usize, usize), ) -> &mut Self

Sets the (error, label) of the node at index.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn decision_path(&self, x: &[f64]) -> Result<Vec<usize>, TreeError>

The indices of the nodes an instance visits, root first, leaf last.

Source

pub fn predict( &self, rows: &[f64], n_features: usize, ) -> Result<Vec<usize>, TreeError>

Classifies a batch. rows is row-major, n_features values per row.

Trait Implementations§

Source§

impl Clone for Tree

Source§

fn clone(&self) -> Tree

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Tree

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Tree

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for Tree

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for Tree

One node per line, indented by depth, children after their parent.

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Serialize for Tree

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl Freeze for Tree

§

impl RefUnwindSafe for Tree

§

impl Send for Tree

§

impl Sync for Tree

§

impl Unpin for Tree

§

impl UnsafeUnpin for Tree

§

impl UnwindSafe for Tree

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V