pub struct GradientBooster { /* private fields */ }Expand description
Main gradient boosting implementation.
GradientBooster is the core ensemble model that trains a sequence
of decision trees to minimize a given loss function. It supports
various training strategies and provides comprehensive monitoring capabilities.
§Key Features
- Loss Functions: MSE, MAE, Huber (robust regression), LogLoss (classification)
- Regularization: Learning rate shrinkage, L2 regularization (
lambda) - Stochastic Boosting: Row subsampling for variance reduction
- Early Stopping: Configurable patience and improvement tolerance
- Feature Importance: Gain-based importance scores
§Serialization
This struct derives Serialize and Deserialize, allowing trained models
to be saved and loaded using serde-compatible formats.
Implementations§
Source§impl GradientBooster
impl GradientBooster
Sourcepub fn new(config: GBRTConfig) -> BoostingResult<Self>
pub fn new(config: GBRTConfig) -> BoostingResult<Self>
Creates a new gradient booster with the given configuration.
Validates the configuration before returning. The booster is untrained
and ready for [fit()] to be called.
§Parameters
config:GBRTConfigwith hyperparameters and training options.
§Errors
Returns BoostingError::ConfigError if configuration validation fails.
Sourcepub fn fit(
&mut self,
train_data: &Dataset,
validation_data: Option<&Dataset>,
) -> BoostingResult<()>
pub fn fit( &mut self, train_data: &Dataset, validation_data: Option<&Dataset>, ) -> BoostingResult<()>
Trains the model on a dataset with optional validation data.
This is the main training entry point. It performs the complete gradient boosting algorithm including:
- Data validation and initialization
- Iterative tree building with gradient optimization
- Optional stochastic subsampling
- Validation loss monitoring and early stopping
- Feature importance computation
§Parameters
train_data: Training dataset with features and targets.validation_data: Optional validation dataset for early stopping and monitoring.
§Errors
Returns errors for:
- Invalid training data (empty, mismatched dimensions)
- Configuration issues detected during training
- Tree building failures
- Loss computation problems (e.g., NaN values)
§Early Stopping
If config.early_stopping_rounds is set and validation_data is provided,
training stops when validation loss doesn’t improve significantly
(by more than config.early_stopping_tolerance) for the specified number of rounds.
Sourcepub fn predict(&self, features: &FeatureMatrix) -> BoostingResult<Vec<f64>>
pub fn predict(&self, features: &FeatureMatrix) -> BoostingResult<Vec<f64>>
Makes predictions for a batch of samples.
Calculates predictions by summing contributions from all trees with shrinkage applied. Applies loss-specific transformation (e.g., sigmoid for LogLoss).
§Parameters
features: Feature matrix with shape(n_samples, n_features).
§Returns
Vector of predictions of length n_samples.
§Errors
PredictionError::ModelNotTrainedif called before [fit()]PredictionError::FeatureMismatchiffeatures.n_features()doesn’t match training data
Sourcepub fn predict_single(&self, features: &[f64]) -> BoostingResult<f64>
pub fn predict_single(&self, features: &[f64]) -> BoostingResult<f64>
Makes a prediction for a single sample.
More efficient than predict() for single-sample inference.
§Parameters
features: Slice of feature values of lengthn_features.
§Returns
Single prediction value.
§Errors
PredictionError::ModelNotTrainedif called before [fit()]PredictionError::FeatureMismatchiffeatures.len()doesn’t match training data
Sourcepub fn feature_importance(&self) -> &[f64]
pub fn feature_importance(&self) -> &[f64]
Returns feature importance scores from the trained model.
Importance is computed as the normalized total gain contributed by splits on each feature across all trees. Scores sum to 1.0.
§Returns
Slice of importance scores of length n_features.
Returns zeros if config.compute_feature_importance is false.
§Panics
Will panic if called before any model has been trained (no features are known).
Sourcepub fn training_state(&self) -> Option<&TrainingState>
pub fn training_state(&self) -> Option<&TrainingState>
Returns the training history and validation state.
Contains per-iteration metrics and the best iteration if early stopping was used. This is useful for plotting learning curves or analyzing training dynamics.
§Returns
Some(TrainingState) after training, None before training.
Sourcepub fn n_trees(&self) -> usize
pub fn n_trees(&self) -> usize
Returns the number of trees in the ensemble.
Note: This may be fewer than config.n_estimators if early stopping triggered.
§Returns
Number of trees after training.
Sourcepub fn config(&self) -> &GBRTConfig
pub fn config(&self) -> &GBRTConfig
Returns the configuration used to create this booster.
Sourcepub fn is_trained(&self) -> bool
pub fn is_trained(&self) -> bool
Checks whether the model has been trained.
Trait Implementations§
Source§impl Clone for GradientBooster
impl Clone for GradientBooster
Source§fn clone(&self) -> GradientBooster
fn clone(&self) -> GradientBooster
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for GradientBooster
impl Debug for GradientBooster
Source§impl<'de> Deserialize<'de> for GradientBooster
impl<'de> Deserialize<'de> for GradientBooster
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>,
Source§impl Display for GradientBooster
impl Display for GradientBooster
Auto Trait Implementations§
impl Freeze for GradientBooster
impl RefUnwindSafe for GradientBooster
impl Send for GradientBooster
impl Sync for GradientBooster
impl Unpin for GradientBooster
impl UnwindSafe for GradientBooster
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