pub struct BaggingRegressor<F> {
pub n_estimators: usize,
pub max_samples: f64,
pub max_features: f64,
pub bootstrap: bool,
pub bootstrap_features: bool,
pub random_state: Option<u64>,
pub max_depth: Option<usize>,
/* private fields */
}Expand description
Bootstrap aggregation (bagging) regressor using decision trees.
Each estimator is trained on a bootstrap sample of the data, optionally with a random subset of features. Final predictions are the mean across all estimators.
§Type Parameters
F: The floating-point type (f32orf64).
§Examples
use ferrolearn_tree::BaggingRegressor;
use ferrolearn_core::{Fit, Predict};
use ndarray::{Array1, Array2};
let x = Array2::from_shape_vec((6, 1), vec![
1.0, 2.0, 3.0, 4.0, 5.0, 6.0,
]).unwrap();
let y = Array1::from(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0]);
let model = BaggingRegressor::<f64>::new()
.with_n_estimators(10)
.with_random_state(42);
let fitted = model.fit(&x, &y).unwrap();
let preds = fitted.predict(&x).unwrap();Fields§
§n_estimators: usizeNumber of estimators in the ensemble.
max_samples: f64Fraction of samples to draw for each estimator (default 1.0).
max_features: f64Fraction of features to draw for each estimator (default 1.0).
bootstrap: boolWhether to sample with replacement (default true).
bootstrap_features: boolWhether to sample features with replacement (default false).
random_state: Option<u64>Random seed for reproducibility.
max_depth: Option<usize>Maximum depth of each base decision tree. None means unlimited.
Implementations§
Source§impl<F: Float> BaggingRegressor<F>
impl<F: Float> BaggingRegressor<F>
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new BaggingRegressor with default settings.
Defaults: n_estimators = 10, max_samples = 1.0,
max_features = 1.0, bootstrap = true,
bootstrap_features = false, random_state = None,
max_depth = None.
Sourcepub fn with_n_estimators(self, n: usize) -> Self
pub fn with_n_estimators(self, n: usize) -> Self
Set the number of estimators.
Sourcepub fn with_max_samples(self, frac: f64) -> Self
pub fn with_max_samples(self, frac: f64) -> Self
Set the fraction of samples to draw per estimator.
Sourcepub fn with_max_features(self, frac: f64) -> Self
pub fn with_max_features(self, frac: f64) -> Self
Set the fraction of features to draw per estimator.
Sourcepub fn with_bootstrap(self, bootstrap: bool) -> Self
pub fn with_bootstrap(self, bootstrap: bool) -> Self
Set whether to sample with replacement.
Sourcepub fn with_bootstrap_features(self, bootstrap_features: bool) -> Self
pub fn with_bootstrap_features(self, bootstrap_features: bool) -> Self
Set whether to sample features with replacement.
Sourcepub fn with_random_state(self, seed: u64) -> Self
pub fn with_random_state(self, seed: u64) -> Self
Set the random seed for reproducibility.
Sourcepub fn with_max_depth(self, max_depth: Option<usize>) -> Self
pub fn with_max_depth(self, max_depth: Option<usize>) -> Self
Set the maximum depth of each base decision tree.
Trait Implementations§
Source§impl<F: Clone> Clone for BaggingRegressor<F>
impl<F: Clone> Clone for BaggingRegressor<F>
Source§fn clone(&self) -> BaggingRegressor<F>
fn clone(&self) -> BaggingRegressor<F>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<F: Debug> Debug for BaggingRegressor<F>
impl<F: Debug> Debug for BaggingRegressor<F>
Source§impl<F: Float> Default for BaggingRegressor<F>
impl<F: Float> Default for BaggingRegressor<F>
Source§impl<F: Float + Send + Sync + 'static> Fit<ArrayBase<OwnedRepr<F>, Dim<[usize; 2]>>, ArrayBase<OwnedRepr<F>, Dim<[usize; 1]>>> for BaggingRegressor<F>
impl<F: Float + Send + Sync + 'static> Fit<ArrayBase<OwnedRepr<F>, Dim<[usize; 2]>>, ArrayBase<OwnedRepr<F>, Dim<[usize; 1]>>> for BaggingRegressor<F>
Source§fn fit(
&self,
x: &Array2<F>,
y: &Array1<F>,
) -> Result<FittedBaggingRegressor<F>, FerroError>
fn fit( &self, x: &Array2<F>, y: &Array1<F>, ) -> Result<FittedBaggingRegressor<F>, FerroError>
Fit the bagging regressor by building n_estimators decision trees in parallel.
§Errors
Returns FerroError::ShapeMismatch if x and y have different
numbers of samples.
Returns FerroError::InsufficientSamples if there are no samples.
Returns FerroError::InvalidParameter for invalid hyperparameters.
Source§type Fitted = FittedBaggingRegressor<F>
type Fitted = FittedBaggingRegressor<F>
fit.Source§type Error = FerroError
type Error = FerroError
fit.Source§impl<F: Float + Send + Sync + 'static> PipelineEstimator<F> for BaggingRegressor<F>
impl<F: Float + Send + Sync + 'static> PipelineEstimator<F> for BaggingRegressor<F>
Source§fn fit_pipeline(
&self,
x: &Array2<F>,
y: &Array1<F>,
) -> Result<Box<dyn FittedPipelineEstimator<F>>, FerroError>
fn fit_pipeline( &self, x: &Array2<F>, y: &Array1<F>, ) -> Result<Box<dyn FittedPipelineEstimator<F>>, FerroError>
Auto Trait Implementations§
impl<F> Freeze for BaggingRegressor<F>
impl<F> RefUnwindSafe for BaggingRegressor<F>where
F: RefUnwindSafe,
impl<F> Send for BaggingRegressor<F>where
F: Send,
impl<F> Sync for BaggingRegressor<F>where
F: Sync,
impl<F> Unpin for BaggingRegressor<F>where
F: Unpin,
impl<F> UnsafeUnpin for BaggingRegressor<F>
impl<F> UnwindSafe for BaggingRegressor<F>where
F: 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> DistributionExt for Twhere
T: ?Sized,
impl<T> DistributionExt for Twhere
T: ?Sized,
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