pub struct Ridge { /* private fields */ }Expand description
Ridge regression with L2 regularization.
Fits a linear model with L2 penalty on coefficient magnitudes. The optimization objective is:
minimize ||y - Xβ||² + α||β||²where α (alpha) controls the regularization strength.
§Solver
Uses regularized normal equations: β = (X^T X + αI)^-1 X^T y
§When to use Ridge
- When you have many correlated features (multicollinearity)
- To prevent overfitting with limited samples
- When all features are expected to contribute
§Examples
use aprender::prelude::*;
use aprender::linear_model::Ridge;
// Data with some noise
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 = Ridge::new(1.0); // alpha = 1.0
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§
Source§impl Ridge
impl Ridge
Sourcepub fn new(alpha: f32) -> Ridge
pub fn new(alpha: f32) -> Ridge
Creates a new Ridge regression with the given regularization strength.
§Arguments
alpha- Regularization strength. Larger values = more regularization. Must be non-negative. Use 0.0 for no regularization (equivalent to OLS).
Sourcepub fn with_intercept(self, fit_intercept: bool) -> Ridge
pub fn with_intercept(self, fit_intercept: bool) -> Ridge
Sets whether to fit an intercept term.
Sourcepub fn coefficients(&self) -> &Vector<f32>
pub fn coefficients(&self) -> &Vector<f32>
Sourcepub fn save<P>(&self, path: P) -> Result<(), String>
pub fn save<P>(&self, path: P) -> Result<(), String>
Saves the model to a binary file using bincode.
§Errors
Returns an error if serialization or file writing fails.
Sourcepub fn load<P>(path: P) -> Result<Ridge, String>
pub fn load<P>(path: P) -> Result<Ridge, String>
Loads a model from a binary file.
§Errors
Returns an error if file reading or deserialization fails.
Sourcepub fn save_safetensors<P>(&self, path: P) -> Result<(), String>
pub fn save_safetensors<P>(&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 Ridge
impl<'de> Deserialize<'de> for Ridge
Source§fn deserialize<__D>(
__deserializer: __D,
) -> Result<Ridge, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(
__deserializer: __D,
) -> Result<Ridge, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
Source§impl Estimator for Ridge
impl Estimator for Ridge
Source§fn fit(&mut self, x: &Matrix<f32>, y: &Vector<f32>) -> Result<(), AprenderError>
fn fit(&mut self, x: &Matrix<f32>, y: &Vector<f32>) -> Result<(), AprenderError>
Fits the Ridge regression model using regularized normal equations.
Solves: β = (X^T X + αI)^-1 X^T y
§Errors
Returns an error if input dimensions don’t match or matrix is singular.
Source§impl Serialize for Ridge
impl Serialize for Ridge
Source§fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
Auto Trait Implementations§
impl Freeze for Ridge
impl RefUnwindSafe for Ridge
impl Send for Ridge
impl Sync for Ridge
impl Unpin for Ridge
impl UnsafeUnpin for Ridge
impl UnwindSafe for Ridge
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