pub struct DecisionTreeLearner { /* private fields */ }Expand description
ID3/C4.5-style decision tree learner.
use ipfrs_tensorlogic::{DecisionTreeLearner, DtlSample, DtlLearnerConfig, DtlCriterion};
let mut learner = DecisionTreeLearner::new(
DtlLearnerConfig { max_depth: 5, ..Default::default() },
vec!["petal_len".into(), "petal_wid".into()],
);
let samples = vec![
DtlSample::new(vec![1.0, 0.5], "setosa"),
DtlSample::new(vec![4.5, 1.5], "versicolor"),
DtlSample::new(vec![5.0, 2.0], "virginica"),
DtlSample::new(vec![1.2, 0.4], "setosa"),
DtlSample::new(vec![4.8, 1.8], "versicolor"),
];
learner.fit(&samples).expect("example: should succeed in docs");
let pred = learner.predict(&[1.1, 0.45]).expect("example: should succeed in docs");
assert_eq!(pred.label, "setosa");Implementations§
Source§impl DecisionTreeLearner
impl DecisionTreeLearner
Sourcepub fn new(config: DtlLearnerConfig, feature_names: Vec<String>) -> Self
pub fn new(config: DtlLearnerConfig, feature_names: Vec<String>) -> Self
Create a new (untrained) learner.
feature_names is optional context: if non-empty its length must match
the feature dimension of the first fit call.
Sourcepub fn with_defaults(feature_names: Vec<String>) -> Self
pub fn with_defaults(feature_names: Vec<String>) -> Self
Convenience constructor with default configuration.
Source§impl DecisionTreeLearner
impl DecisionTreeLearner
Sourcepub fn predict(&self, features: &[f64]) -> Result<DtlPrediction, DtlError>
pub fn predict(&self, features: &[f64]) -> Result<DtlPrediction, DtlError>
Predict the class label for a single feature vector.
Sourcepub fn predict_batch(
&self,
samples: &[Vec<f64>],
) -> Vec<Result<DtlPrediction, DtlError>>
pub fn predict_batch( &self, samples: &[Vec<f64>], ) -> Vec<Result<DtlPrediction, DtlError>>
Predict a batch of feature vectors.
Source§impl DecisionTreeLearner
impl DecisionTreeLearner
Sourcepub fn feature_importance(&self) -> Vec<(String, f64)>
pub fn feature_importance(&self) -> Vec<(String, f64)>
Compute normalised feature importances (weighted impurity reduction).
Returns a vector of (feature_name, importance) pairs sorted by
importance descending. Values are normalised to sum to 1.0.
Source§impl DecisionTreeLearner
impl DecisionTreeLearner
Sourcepub fn prune(&mut self, min_samples: usize)
pub fn prune(&mut self, min_samples: usize)
Post-hoc pruning: collapse split nodes where both children are leaves
and neither has at least min_samples samples in the majority class.
This is a conservative bottom-up pruning pass that replaces such split nodes with a single leaf carrying the combined class distribution.
Source§impl DecisionTreeLearner
impl DecisionTreeLearner
Sourcepub fn learner_stats(&self) -> DtlLearnerStats
pub fn learner_stats(&self) -> DtlLearnerStats
Return a snapshot of learner statistics.
Sourcepub fn history(&self) -> &VecDeque<DtlTrainingRecord>
pub fn history(&self) -> &VecDeque<DtlTrainingRecord>
Reference to the training history ring-buffer.
Sourcepub fn config(&self) -> &DtlLearnerConfig
pub fn config(&self) -> &DtlLearnerConfig
Reference to the current configuration.
Sourcepub fn feature_names(&self) -> &[String]
pub fn feature_names(&self) -> &[String]
Reference to the feature names.
Sourcepub fn class_labels(&self) -> &[String]
pub fn class_labels(&self) -> &[String]
Reference to the discovered class labels.
Auto Trait Implementations§
impl Freeze for DecisionTreeLearner
impl RefUnwindSafe for DecisionTreeLearner
impl Send for DecisionTreeLearner
impl Sync for DecisionTreeLearner
impl Unpin for DecisionTreeLearner
impl UnsafeUnpin for DecisionTreeLearner
impl UnwindSafe for DecisionTreeLearner
Blanket Implementations§
impl<T> Allocation for T
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
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