pub struct RandomForestClassifier { /* private fields */ }Expand description
Random Forest classifier - an ensemble of decision trees.
Combines multiple decision trees trained on bootstrap samples with random feature selection to reduce overfitting and improve accuracy.
Implementations§
Source§impl RandomForestClassifier
impl RandomForestClassifier
Sourcepub fn with_max_depth(self, max_depth: usize) -> Self
pub fn with_max_depth(self, max_depth: usize) -> Self
Sets the maximum depth for each tree.
Sourcepub fn with_random_state(self, random_state: u64) -> Self
pub fn with_random_state(self, random_state: u64) -> Self
Sets the random state for reproducibility.
Sourcepub fn score(&self, x: &Matrix<f32>, y: &[usize]) -> f32
pub fn score(&self, x: &Matrix<f32>, y: &[usize]) -> f32
Calculates accuracy score on test data.
Sourcepub fn predict_proba(&self, x: &Matrix<f32>) -> Matrix<f32>
pub fn predict_proba(&self, x: &Matrix<f32>) -> Matrix<f32>
Predict class probabilities for input features.
Returns probability distribution over classes based on vote proportions across trees in the forest.
§Returns
Matrix<f32> with shape (n_samples, n_classes) where each row
sums to 1.0.
Sourcepub fn oob_prediction(&self) -> Option<Vec<usize>>
pub fn oob_prediction(&self) -> Option<Vec<usize>>
Returns Out-of-Bag (OOB) predictions for training samples.
For each training sample, predictions are made using only the trees where that sample was NOT in the bootstrap sample (out-of-bag).
§Returns
Some(Vec<usize>) if the model has been fitted, None otherwise.
The vector has the same length as the training data.
Sourcepub fn oob_score(&self) -> Option<f32>
pub fn oob_score(&self) -> Option<f32>
Returns Out-of-Bag (OOB) accuracy score.
Computes accuracy using OOB predictions. This provides an unbiased estimate of the model’s performance without needing a validation set.
§Returns
Some(f32) with accuracy in [0, 1] if model has been fitted, None otherwise.
Sourcepub fn feature_importances(&self) -> Option<Vec<f32>>
pub fn feature_importances(&self) -> Option<Vec<f32>>
Returns feature importances based on mean decrease in impurity.
Feature importance is calculated as the total decrease in node impurity (weighted by the number of samples) averaged over all trees in the forest.
§Returns
Some(Vec<f32>) with importance for each feature (normalized to sum to 1.0)
if model has been fitted, None otherwise.
§Example
let mut rf = RandomForestClassifier::new(50);
rf.fit(&x_train, &y_train).expect("fit should succeed");
if let Some(importances) = rf.feature_importances() {
for (i, &importance) in importances.iter().enumerate() {
println!("Feature {}: {:.4}", i, importance);
}
}Trait Implementations§
Source§impl Clone for RandomForestClassifier
impl Clone for RandomForestClassifier
Source§fn clone(&self) -> RandomForestClassifier
fn clone(&self) -> RandomForestClassifier
1.0.0 · 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<'de> Deserialize<'de> for RandomForestClassifier
impl<'de> Deserialize<'de> for RandomForestClassifier
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Auto Trait Implementations§
impl Freeze for RandomForestClassifier
impl RefUnwindSafe for RandomForestClassifier
impl Send for RandomForestClassifier
impl Sync for RandomForestClassifier
impl Unpin 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