pub struct ElasticNet { /* private fields */ }Expand description
Elastic Net regression with combined L1 and L2 regularization.
Fits a linear model with both L1 and L2 penalties:
minimize ||y - Xβ||² + α * l1_ratio * ||β||₁ + α * (1 - l1_ratio) * ||β||²§Parameters
alpha- Overall regularization strengthl1_ratio- Mix between L1 and L2 (0.0 = Ridge, 1.0 = Lasso)
§Solver
Uses coordinate descent with combined soft-thresholding and shrinkage.
§When to use Elastic Net
- When you want both sparsity (L1) and grouping effect (L2)
- With correlated features where Lasso may be unstable
- When you don’t know which regularization type to use
§Examples
use aprender::prelude::*;
use aprender::linear_model::ElasticNet;
let x = Matrix::from_vec(5, 2, vec![
1.0, 2.0,
2.0, 3.0,
3.0, 4.0,
4.0, 5.0,
5.0, 6.0,
]).expect("Valid matrix dimensions");
let y = Vector::from_slice(&[5.0, 8.0, 11.0, 14.0, 17.0]);
// 50% L1, 50% L2
let mut model = ElasticNet::new(0.1, 0.5);
model.fit(&x, &y).expect("Fit should succeed with valid data");
let r2 = model.score(&x, &y);
assert!(r2 > 0.9);Implementations§
Source§impl ElasticNet
impl ElasticNet
Sourcepub fn new(alpha: f32, l1_ratio: f32) -> Self
pub fn new(alpha: f32, l1_ratio: f32) -> Self
Creates a new ElasticNet with the given parameters.
§Arguments
alpha- Overall regularization strengthl1_ratio- Mix between L1 and L2 (0.0 = Ridge, 1.0 = Lasso)
Sourcepub fn with_intercept(self, fit_intercept: bool) -> Self
pub fn with_intercept(self, fit_intercept: bool) -> Self
Sets whether to fit an intercept term.
Sourcepub fn with_max_iter(self, max_iter: usize) -> Self
pub fn with_max_iter(self, max_iter: usize) -> Self
Sets the maximum number of iterations.
Sourcepub fn coefficients(&self) -> &Vector<f32>
pub fn coefficients(&self) -> &Vector<f32>
Sourcepub fn load<P: AsRef<Path>>(path: P) -> Result<Self, String>
pub fn load<P: AsRef<Path>>(path: P) -> Result<Self, String>
Loads a model from a binary file.
§Errors
Returns an error if file reading or deserialization fails.
Sourcepub fn save_safetensors<P: AsRef<Path>>(&self, path: P) -> Result<(), String>
pub fn save_safetensors<P: AsRef<Path>>(&self, path: P) -> Result<(), String>
Saves the model to SafeTensors format.
SafeTensors format is compatible with:
- HuggingFace ecosystem
- Ollama (can convert to GGUF)
- PyTorch, TensorFlow
- realizar inference engine
§Errors
Returns an error if:
- Model is not fitted
- Serialization fails
- File writing fails
Trait Implementations§
Source§impl Clone for ElasticNet
impl Clone for ElasticNet
Source§fn clone(&self) -> ElasticNet
fn clone(&self) -> ElasticNet
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for ElasticNet
impl Debug for ElasticNet
Source§impl<'de> Deserialize<'de> for ElasticNet
impl<'de> Deserialize<'de> for ElasticNet
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>,
Deserialize this value from the given Serde deserializer. Read more
Auto Trait Implementations§
impl Freeze for ElasticNet
impl RefUnwindSafe for ElasticNet
impl Send for ElasticNet
impl Sync for ElasticNet
impl Unpin for ElasticNet
impl UnwindSafe for ElasticNet
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> 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>
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 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>
The inverse inclusion map: attempts to construct
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
Checks if
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
Use with care! Same as
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self to the equivalent element of its superset.