pub struct Lasso { /* private fields */ }Expand description
Lasso regression with L1 regularization.
Fits a linear model with L1 penalty on coefficient magnitudes. The optimization objective is:
minimize ||y - Xβ||² + α||β||₁where α (alpha) controls the regularization strength.
§Solver
Uses coordinate descent with soft-thresholding.
§When to use Lasso
- For automatic feature selection (produces sparse models)
- When you expect only a few features to be relevant
- When interpretability through sparsity is desired
§Examples
use aprender::prelude::*;
use aprender::linear_model::Lasso;
// Data with some features
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]);
let mut model = Lasso::new(0.1); // alpha = 0.1
model.fit(&x, &y).expect("Fit should succeed with valid data");
let predictions = model.predict(&x);
let r2 = model.score(&x, &y);
assert!(r2 > 0.9);Implementations§
§impl Lasso
impl Lasso
pub fn new(alpha: f32) -> Self
pub fn new(alpha: f32) -> Self
Creates a new Lasso regression with the given regularization strength.
§Arguments
alpha- Regularization strength. Larger values = more sparsity. Must be non-negative.
pub fn with_intercept(self, fit_intercept: bool) -> Self
pub fn with_intercept(self, fit_intercept: bool) -> Self
Sets whether to fit an intercept term.
pub 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.
pub fn coefficients(&self) -> &Vector<f32>
pub fn coefficients(&self) -> &Vector<f32>
pub fn save<P: AsRef<Path>>(&self, path: P) -> Result<(), String>
pub fn save<P: AsRef<Path>>(&self, path: P) -> Result<(), String>
Saves the model to a binary file using bincode.
§Errors
Returns an error if serialization or file writing fails.
pub 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.
pub 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:
HuggingFaceecosystem- 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<'de> Deserialize<'de> for Lasso
impl<'de> Deserialize<'de> for Lasso
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 Lasso
impl RefUnwindSafe for Lasso
impl Send for Lasso
impl Sync for Lasso
impl Unpin for Lasso
impl UnsafeUnpin for Lasso
impl UnwindSafe for Lasso
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 more