pub struct KernelRidgeRegression { /* private fields */ }Expand description
Kernel Ridge Regression
§Example
use scirs2_transform::kernel::{KernelRidgeRegression, KernelType};
use scirs2_core::ndarray::{Array1, Array2};
let x = Array2::<f64>::zeros((50, 3));
let y = Array1::<f64>::zeros(50);
let mut krr = KernelRidgeRegression::new(1.0, KernelType::RBF { gamma: 0.1 });
krr.fit(&x, &y).expect("should succeed");
let predictions = krr.predict(&x).expect("should succeed");Implementations§
Source§impl KernelRidgeRegression
impl KernelRidgeRegression
Sourcepub fn new(alpha: f64, kernel: KernelType) -> Self
pub fn new(alpha: f64, kernel: KernelType) -> Self
Create a new KernelRidgeRegression
§Arguments
alpha- Regularization parameter (lambda). Larger values = more regularization.kernel- The kernel function to use
Sourcepub fn with_alpha(self, alpha: f64) -> Self
pub fn with_alpha(self, alpha: f64) -> Self
Set the regularization parameter
Sourcepub fn kernel(&self) -> &KernelType
pub fn kernel(&self) -> &KernelType
Get the kernel type
Sourcepub fn regularization(&self) -> f64
pub fn regularization(&self) -> f64
Get the regularization parameter
Sourcepub fn fit<S1, S2>(
&mut self,
x: &ArrayBase<S1, Ix2>,
y: &ArrayBase<S2, Ix1>,
) -> Result<()>
pub fn fit<S1, S2>( &mut self, x: &ArrayBase<S1, Ix2>, y: &ArrayBase<S2, Ix1>, ) -> Result<()>
Fit the model with a single output target
§Arguments
x- Training data, shape (n_samples, n_features)y- Target values, shape (n_samples,)
Sourcepub fn fit_multi<S1, S2>(
&mut self,
x: &ArrayBase<S1, Ix2>,
y: &ArrayBase<S2, Ix2>,
) -> Result<()>
pub fn fit_multi<S1, S2>( &mut self, x: &ArrayBase<S1, Ix2>, y: &ArrayBase<S2, Ix2>, ) -> Result<()>
Fit the model with multiple output targets
§Arguments
x- Training data, shape (n_samples, n_features)y- Target values, shape (n_samples, n_outputs)
Sourcepub fn loo_cv(&self) -> Result<(Array2<f64>, f64)>
pub fn loo_cv(&self) -> Result<(Array2<f64>, f64)>
Leave-one-out cross-validation in closed form
Computes the LOO-CV predictions and error without explicitly re-fitting the model n times. Uses the formula:
LOO_residual_i = alpha_i / (K + lambda*I)^{-1}_{ii}
which requires only one matrix inversion.
§Returns
(loo_predictions, loo_mse)- LOO predictions for each sample and mean squared error
Sourcepub fn auto_select_alpha<S1, S2>(
x: &ArrayBase<S1, Ix2>,
y: &ArrayBase<S2, Ix1>,
kernel: &KernelType,
alpha_values: &[f64],
) -> Result<(f64, f64)>
pub fn auto_select_alpha<S1, S2>( x: &ArrayBase<S1, Ix2>, y: &ArrayBase<S2, Ix1>, kernel: &KernelType, alpha_values: &[f64], ) -> Result<(f64, f64)>
Automatic selection of the regularization parameter via LOO-CV
Tries multiple alpha values and selects the one with lowest LOO-CV error.
§Arguments
x- Training datay- Target values (single output)alpha_values- Candidate regularization parameters
§Returns
(best_alpha, best_mse)- Best alpha and corresponding LOO-CV MSE
Trait Implementations§
Source§impl Clone for KernelRidgeRegression
impl Clone for KernelRidgeRegression
Source§fn clone(&self) -> KernelRidgeRegression
fn clone(&self) -> KernelRidgeRegression
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 moreAuto Trait Implementations§
impl Freeze for KernelRidgeRegression
impl RefUnwindSafe for KernelRidgeRegression
impl Send for KernelRidgeRegression
impl Sync for KernelRidgeRegression
impl Unpin for KernelRidgeRegression
impl UnsafeUnpin for KernelRidgeRegression
impl UnwindSafe for KernelRidgeRegression
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.