pub struct RandomForestClassifier { /* private fields */ }Expand description
Random-forest classifier using bootstrap aggregation of CART classifiers.
The default is 100 bootstrap trees with max_features = sqrt(n_features)
at each split, matching scikit-learn’s RandomForestClassifier defaults.
random_state defaults to zero so fits are reproducible; call
with_random_state to choose another seed.
use datarust::ensemble::RandomForestClassifier;
use datarust::traits::{PredictProba, Predictor};
use datarust::Matrix;
let x = Matrix::new(vec![
vec![0.0, 0.0], vec![0.0, 1.0],
vec![1.0, 0.0], vec![1.0, 1.0],
])?;
let y = vec![0.0, 1.0, 1.0, 0.0];
let mut forest = RandomForestClassifier::new()
.with_n_estimators(25)
.with_random_state(7);
forest.fit(&x, &y)?;
let predictions = forest.predict(&x)?;
let probabilities = forest.predict_proba(&x)?;
assert_eq!(predictions.len(), 4);
assert_eq!(probabilities.ncols(), 2);Implementations§
Source§impl RandomForestClassifier
impl RandomForestClassifier
Sourcepub fn with_n_estimators(self, n: usize) -> Self
pub fn with_n_estimators(self, n: usize) -> Self
Sets the number of trees.
Sourcepub fn with_criterion(self, criterion: Criterion) -> Self
pub fn with_criterion(self, criterion: Criterion) -> Self
Sets the tree split criterion (Gini or Entropy).
Criterion::SquaredError is regression-only and is rejected by
fit.
Sourcepub fn with_max_depth(self, depth: Option<usize>) -> Self
pub fn with_max_depth(self, depth: Option<usize>) -> Self
Sets the maximum depth of each tree (None means unlimited).
Sourcepub fn with_min_samples_split(self, n: usize) -> Self
pub fn with_min_samples_split(self, n: usize) -> Self
Sets the minimum samples required to split a node.
Sourcepub fn with_min_samples_leaf(self, n: usize) -> Self
pub fn with_min_samples_leaf(self, n: usize) -> Self
Sets the minimum samples required in each leaf.
Sourcepub fn with_min_impurity_decrease(self, value: f64) -> Self
pub fn with_min_impurity_decrease(self, value: f64) -> Self
Sets the minimum impurity decrease required for a split.
Sourcepub fn with_max_features(self, value: MaxFeatures) -> Self
pub fn with_max_features(self, value: MaxFeatures) -> Self
Sets the number of features considered at each split.
Sourcepub fn with_bootstrap(self, bootstrap: bool) -> Self
pub fn with_bootstrap(self, bootstrap: bool) -> Self
Enables or disables bootstrap sampling.
Sourcepub fn with_random_state(self, seed: u64) -> Self
pub fn with_random_state(self, seed: u64) -> Self
Sets the deterministic forest seed.
Sourcepub fn n_estimators(&self) -> usize
pub fn n_estimators(&self) -> usize
Number of fitted trees.
Sourcepub fn n_features_in(&self) -> usize
pub fn n_features_in(&self) -> usize
Number of features seen during fitting.
Sourcepub fn trees(&self) -> &[DecisionTreeClassifier]
pub fn trees(&self) -> &[DecisionTreeClassifier]
Borrow the fitted decision trees.
Sourcepub fn estimators_samples(&self) -> Vec<Vec<usize>>
pub fn estimators_samples(&self) -> Vec<Vec<usize>>
Regenerates the row indices used to fit each tree (scikit-learn’s
estimators_samples_ semantics). With bootstrap enabled, each inner
vector contains n_samples draws with replacement; with bootstrap
disabled it is the original row order. Indices are derived from the
stored per-tree seeds on demand, so the fitted model does not retain
n_estimators × n_samples indices.
Sourcepub fn feature_importances(&self) -> Result<Vec<f64>>
pub fn feature_importances(&self) -> Result<Vec<f64>>
Mean normalized impurity reduction across the non-trivial fitted
trees, matching sklearn’s feature_importances_ behavior.
Trait Implementations§
Source§impl Classifier for RandomForestClassifier
impl Classifier for RandomForestClassifier
Source§impl Clone for RandomForestClassifier
impl Clone for RandomForestClassifier
Source§fn clone(&self) -> RandomForestClassifier
fn clone(&self) -> RandomForestClassifier
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for RandomForestClassifier
impl Debug for RandomForestClassifier
Source§impl Default for RandomForestClassifier
impl Default for RandomForestClassifier
impl Estimator for RandomForestClassifier
Source§impl Params for RandomForestClassifier
impl Params for RandomForestClassifier
Source§fn get_params(&self) -> Vec<(&'static str, ParamValue)>
fn get_params(&self) -> Vec<(&'static str, ParamValue)>
(name, value) pairs.Source§fn set_params(&mut self, name: &str, value: ParamValue) -> Result<()>
fn set_params(&mut self, name: &str, value: ParamValue) -> Result<()>
name is unknown or
value is the wrong type for that parameter.Source§impl Predictor for RandomForestClassifier
impl Predictor for RandomForestClassifier
Source§fn fit(&mut self, x: &Matrix, y: &[f64]) -> Result<()>
fn fit(&mut self, x: &Matrix, y: &[f64]) -> Result<()>
Auto Trait Implementations§
impl Freeze for RandomForestClassifier
impl RefUnwindSafe for RandomForestClassifier
impl Send for RandomForestClassifier
impl Sync for RandomForestClassifier
impl Unpin for RandomForestClassifier
impl UnsafeUnpin for RandomForestClassifier
impl UnwindSafe for RandomForestClassifier
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