#[non_exhaustive]pub enum TreeNode {
Leaf {
prediction: f64,
n_samples: usize,
class_counts: Vec<usize>,
impurity: f64,
},
Split {
feature_idx: usize,
threshold: f64,
left: Box<TreeNode>,
right: Box<TreeNode>,
n_samples: usize,
impurity: f64,
class_counts: Vec<usize>,
prediction: f64,
},
}Expand description
A node in the decision tree (recursive representation).
Used during tree construction, then flattened into a FlatTree for
cache-optimal prediction. Exposed publicly for visualization.
Variants (Non-exhaustive)§
This enum is marked as non-exhaustive
Implementations§
Source§impl TreeNode
impl TreeNode
Sourcepub fn predict_proba(&self, sample: &[f64], n_classes: usize) -> Vec<f64>
pub fn predict_proba(&self, sample: &[f64], n_classes: usize) -> Vec<f64>
Get class probabilities for a single sample.
Sourcepub fn total_leaf_impurity(&self) -> f64
pub fn total_leaf_impurity(&self) -> f64
Sum of weighted leaf impurities: Σ(impurity_leaf × n_samples_leaf).
This is R(T_t) in the cost-complexity pruning literature.
Sourcepub fn prune_ccp(self, ccp_alpha: f64) -> TreeNode
pub fn prune_ccp(self, ccp_alpha: f64) -> TreeNode
Minimal cost-complexity pruning (MCCP).
Recursively prunes subtrees whose effective alpha is ≤ ccp_alpha.
Effective alpha = (R(t) - R(T_t)) / (|T_t| - 1), where R(t) is the
re-substitution error if this node were a leaf and R(T_t) is the
total leaf impurity of the subtree.
This matches sklearn’s ccp_alpha parameter behavior.
Sourcepub fn cost_complexity_pruning_path(&self) -> (Vec<f64>, Vec<f64>)
pub fn cost_complexity_pruning_path(&self) -> (Vec<f64>, Vec<f64>)
Compute the cost-complexity pruning path.
Returns (ccp_alphas, total_impurities) — a sequence of effective
alpha values and the corresponding total tree impurity at each
pruning step. Useful for elbow-method selection of ccp_alpha.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for TreeNode
impl RefUnwindSafe for TreeNode
impl Send for TreeNode
impl Sync for TreeNode
impl Unpin for TreeNode
impl UnsafeUnpin for TreeNode
impl UnwindSafe for TreeNode
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more