pub struct RandomForestClassifier<XT: Number, YT: WholeNumber> { /* private fields */ }Implementations§
Source§impl<XT: Number, YT: WholeNumber> RandomForestClassifier<XT, YT>
This module contains the implementation of the RandomForestClassifier struct.
impl<XT: Number, YT: WholeNumber> RandomForestClassifier<XT, YT>
This module contains the implementation of the RandomForestClassifier struct.
The RandomForestClassifier is a machine learning algorithm that combines multiple decision trees to make predictions.
It is used for classification tasks where the input features are of type XT and the target labels are of type YT.
§Example
use rusty_ai::forests::classifier::RandomForestClassifier;
use rusty_ai::data::dataset::Dataset;
use nalgebra::{DMatrix, DVector};
// Create a mock dataset
let x = DMatrix::from_row_slice(
6,
2,
&[1.0, 2.0, 1.1, 2.1, 1.2, 2.2, 3.0, 4.0, 3.1, 4.1, 3.2, 4.2],
);
let y = DVector::from_vec(vec![0, 0, 0, 1, 1, 1]);
let dataset = Dataset::new(x, y);
// Create a random forest classifier with default parameters
let mut forest = RandomForestClassifier::<f64, u8>::default();
// Fit the classifier to the dataset
forest.fit(&dataset, Some(42)).unwrap();
// Make predictions on new features
let features = DMatrix::from_row_slice(
2,
2,
&[
1.0, 2.0, // Should be classified as class 0
3.0, 4.0, // Should be classified as class 1
],
);
let predictions = forest.predict(&features).unwrap();
println!("Predictions: {:?}", predictions);Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new instance of the Random Forest Classifier.
This function initializes the classifier with empty frequency maps and an empty vector to store the count of unique feature values.
§Returns
A new instance of the Random Forest Classifier.
Sourcepub fn with_params(
num_trees: Option<usize>,
min_samples_split: Option<u16>,
max_depth: Option<u16>,
criterion: Option<String>,
sample_size: Option<usize>,
) -> Result<Self, Box<dyn Error>>
pub fn with_params( num_trees: Option<usize>, min_samples_split: Option<u16>, max_depth: Option<u16>, criterion: Option<String>, sample_size: Option<usize>, ) -> Result<Self, Box<dyn Error>>
Creates a new instance of the Random Forest Classifier with specified parameters.
§Arguments
num_trees- The number of trees in the forest. If not specified, defaults to 3.min_samples_split- The minimum number of samples required to split an internal node. If not specified, defaults to 2.max_depth- The maximum depth of the decision trees. If not specified, defaults to None.criterion- The function to measure the quality of a split. If not specified, defaults to “gini”.sample_size- The size of the random subsets of the dataset to train each tree. If not specified, defaults to None.
§Returns
A Result containing the Random Forest Classifier instance or an error.
Sourcepub fn set_trees(&mut self, trees: Vec<DecisionTreeClassifier<XT, YT>>)
pub fn set_trees(&mut self, trees: Vec<DecisionTreeClassifier<XT, YT>>)
Sets the decision trees of the random forest.
§Arguments
trees- A vector of DecisionTreeClassifier instances.
Sourcepub fn set_sample_size(
&mut self,
sample_size: Option<usize>,
) -> Result<(), Box<dyn Error>>
pub fn set_sample_size( &mut self, sample_size: Option<usize>, ) -> Result<(), Box<dyn Error>>
Sourcepub fn set_min_samples_split(
&mut self,
min_samples_split: u16,
) -> Result<(), Box<dyn Error>>
pub fn set_min_samples_split( &mut self, min_samples_split: u16, ) -> Result<(), Box<dyn Error>>
Sourcepub fn trees(&self) -> &Vec<DecisionTreeClassifier<XT, YT>>
pub fn trees(&self) -> &Vec<DecisionTreeClassifier<XT, YT>>
Returns a reference to the decision trees in the random forest.
Sourcepub fn sample_size(&self) -> Option<usize>
pub fn sample_size(&self) -> Option<usize>
Returns the sample size for each tree in the random forest.
Sourcepub fn min_samples_split(&self) -> u16
pub fn min_samples_split(&self) -> u16
Returns the minimum number of samples required to split an internal node in each decision tree.
Sourcepub fn max_depth(&self) -> Option<u16>
pub fn max_depth(&self) -> Option<u16>
Returns the maximum depth of each decision tree in the random forest.
Sourcepub fn criterion(&self) -> &String
pub fn criterion(&self) -> &String
Returns a reference to the criterion function used to measure the quality of a split in each decision tree.
Sourcepub fn fit(
&mut self,
dataset: &Dataset<XT, YT>,
seed: Option<u64>,
) -> Result<String, Box<dyn Error>>
pub fn fit( &mut self, dataset: &Dataset<XT, YT>, seed: Option<u64>, ) -> Result<String, Box<dyn Error>>
Fits the random forest to the given dataset.
§Arguments
dataset- The dataset to fit the random forest to.seed- The seed for the random number generator used to generate random subsets of the dataset. If not specified, a random seed will be used.
§Returns
A Result indicating whether the fitting process was successful or an error occurred.
Trait Implementations§
Source§impl<XT: Number, YT: WholeNumber> ClassificationMetrics<YT> for RandomForestClassifier<XT, YT>
impl<XT: Number, YT: WholeNumber> ClassificationMetrics<YT> for RandomForestClassifier<XT, YT>
Source§fn confusion_matrix(
&self,
y_true: &DVector<T>,
y_pred: &DVector<T>,
) -> Result<DMatrix<usize>, Box<dyn Error>>
fn confusion_matrix( &self, y_true: &DVector<T>, y_pred: &DVector<T>, ) -> Result<DMatrix<usize>, Box<dyn Error>>
Source§fn accuracy(
&self,
y_true: &DVector<T>,
y_pred: &DVector<T>,
) -> Result<f64, Box<dyn Error>>
fn accuracy( &self, y_true: &DVector<T>, y_pred: &DVector<T>, ) -> Result<f64, Box<dyn Error>>
Source§fn precision(
&self,
y_true: &DVector<T>,
y_pred: &DVector<T>,
) -> Result<f64, Box<dyn Error>>
fn precision( &self, y_true: &DVector<T>, y_pred: &DVector<T>, ) -> Result<f64, Box<dyn Error>>
Source§impl<XT: Clone + Number, YT: Clone + WholeNumber> Clone for RandomForestClassifier<XT, YT>
impl<XT: Clone + Number, YT: Clone + WholeNumber> Clone for RandomForestClassifier<XT, YT>
Source§fn clone(&self) -> RandomForestClassifier<XT, YT>
fn clone(&self) -> RandomForestClassifier<XT, YT>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<XT: Debug + Number, YT: Debug + WholeNumber> Debug for RandomForestClassifier<XT, YT>
impl<XT: Debug + Number, YT: Debug + WholeNumber> Debug for RandomForestClassifier<XT, YT>
Source§impl<XT: Number, YT: WholeNumber> Default for RandomForestClassifier<XT, YT>
impl<XT: Number, YT: WholeNumber> Default for RandomForestClassifier<XT, YT>
Auto Trait Implementations§
impl<XT, YT> Freeze for RandomForestClassifier<XT, YT>
impl<XT, YT> RefUnwindSafe for RandomForestClassifier<XT, YT>where
XT: RefUnwindSafe,
YT: RefUnwindSafe,
impl<XT, YT> Send for RandomForestClassifier<XT, YT>
impl<XT, YT> Sync for RandomForestClassifier<XT, YT>
impl<XT, YT> Unpin for RandomForestClassifier<XT, YT>where
XT: Unpin,
impl<XT, YT> UnwindSafe for RandomForestClassifier<XT, YT>where
XT: UnwindSafe,
YT: UnwindSafe,
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 moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.