pub struct EnsembleLearner<M> {
pub models: Vec<M>,
pub model_features: Vec<Vec<usize>>,
}Expand description
A fitted ensemble of learners for classification.
§Structure
An Ensemble Learner is composed of a collection of fitted models of type M.
§Fitting Algorithm
Given a DatasetBase denoted as D,
- Create as many distinct bootstrapped subset of the original dataset
Das number of distinct model to fit. - Fit each distinct model on a distinct bootstrapped subset of
D.
Note that the subset size, as well as the subset of feature to use in each training subset can be specified in the parameters.
§Prediction Algorithm
The prediction result is the result of majority voting across the fitted learners.
§Example
This example shows how to train a bagging model using 100 decision trees, each trained on 70% of the training data (bootstrap sampling).
use linfa::prelude::{Fit, Predict};
use linfa_ensemble::EnsembleLearnerParams;
use linfa_trees::DecisionTree;
use ndarray_rand::rand::SeedableRng;
use rand::rngs::SmallRng;
// Load Iris dataset
let mut rng = SmallRng::seed_from_u64(42);
let (train, test) = linfa_datasets::iris()
.shuffle(&mut rng)
.split_with_ratio(0.8);
// Train the model on the iris dataset
let bagging_model = EnsembleLearnerParams::new(DecisionTree::params())
.ensemble_size(100) // Number of Decision Tree to fit
.bootstrap_proportion(0.7) // Select only 70% of the data via bootstrap
.fit(&train)
.unwrap();
// Make predictions on the test set
let predictions = bagging_model.predict(&test);§References
Fields§
§models: Vec<M>§model_features: Vec<Vec<usize>>Implementations§
Source§impl<M> EnsembleLearner<M>
impl<M> EnsembleLearner<M>
Trait Implementations§
Source§impl<F: Clone, T, M> PredictInplace<ArrayBase<OwnedRepr<F>, Dim<[usize; 2]>>, T> for EnsembleLearner<M>
impl<F: Clone, T, M> PredictInplace<ArrayBase<OwnedRepr<F>, Dim<[usize; 2]>>, T> for EnsembleLearner<M>
Source§fn predict_inplace(&self, x: &Array2<F>, y: &mut T)
fn predict_inplace(&self, x: &Array2<F>, y: &mut T)
Predict something in place
Source§fn default_target(&self, x: &Array2<F>) -> T
fn default_target(&self, x: &Array2<F>) -> T
Create targets that
predict_inplace works with.Auto Trait Implementations§
impl<M> Freeze for EnsembleLearner<M>
impl<M> RefUnwindSafe for EnsembleLearner<M>where
M: RefUnwindSafe,
impl<M> Send for EnsembleLearner<M>where
M: Send,
impl<M> Sync for EnsembleLearner<M>where
M: Sync,
impl<M> Unpin for EnsembleLearner<M>where
M: Unpin,
impl<M> UnsafeUnpin for EnsembleLearner<M>
impl<M> UnwindSafe for EnsembleLearner<M>where
M: 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
Mutably borrows from an owned value. Read more
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>
Converts
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>
Converts
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