Optional model state and #[derive(...)] for common traits
In addition of using ndarray instead of Vec<f64>, we also slightly modify the model struct.
To make our RidgeEstimator struct more ergonomic, we derive a few useful traits: Debug, Clone, and Default:
- The
Debugtrait allows us to print the struct for inspection usingprintln!("{:?}", ...), which is helpful during development. Clonelets us duplicate the struct, which is often needed in data processing pipelines.Defaultenables us to create a default value usingRidgeEstimator::default(), which internally calls thenew()method we define.
#![allow(unused)] fn main() { #[derive(Debug, Clone, Default)] pub struct RidgeEstimator { pub beta: Option<f64>, } }
The line
#![allow(unused)] fn main() { beta: Option<f64> }
means beta can be either:
Some(value): if the model is trainedNone: if the model has not been fitted yet
This way, we can explicitly model the fact that the estimator may not be fitted yet (i.e., no coefficients computed). When we initialize the model, we set beta to None as follows:
#![allow(unused)] fn main() { impl RidgeEstimator { /// Creates a new, unfitted Ridge estimator. /// /// # Returns /// A `RidgeEstimator` with `beta` set to `None`. pub fn new() -> Self { Self { beta: None } } } }