Struct linfa_elasticnet::ElasticNetParams
source · [−]pub struct ElasticNetParams<F>(_);
Expand description
A hyper-parameter set during construction
Configures and minimizes the following objective function:
1 / (2 * n_samples) * ||y - Xw||^2_2
+ penalty * l1_ratio * ||w||_1
+ 0.5 * penalty * (1 - l1_ratio) * ||w||^2_2
The parameter set can be verified into a
ElasticNetValidParams
by calling
ParamGuard::check. It is also possible to directly fit a model with
Fit::fit which implicitely verifies the parameter set prior to the
model estimation and forwards any error.
Parameters
Name | Default | Purpose | Range |
---|---|---|---|
penalty | 1.0 | Overall parameter penalty | [0, inf) |
l1_ratio | 0.5 | Distribution of penalty to L1 and L2 regularizations | [0.0, 1.0] |
with_intercept | true | Enable intercept | false , true |
tolerance | 1e-4 | Absolute change of any of the parameters | (0, inf) |
max_iterations | 1000 | Maximum number of iterations | [1, inf) |
Errors
The following errors can come from invalid hyper-parameters:
Returns InvalidPenalty
if the penalty is negative.
Returns InvalidL1Ratio
if the L1 ratio is not in unit.
range
Returns InvalidTolerance
if the tolerance is negative.
Example
use linfa_elasticnet::{ElasticNetParams, ElasticNetError};
use linfa::prelude::*;
use ndarray::array;
let ds = Dataset::new(array![[1.0, 0.0], [0.0, 1.0]], array![3.0, 2.0]);
// create a new parameter set with penalty equals `1e-5`
let unchecked_params = ElasticNetParams::new()
.penalty(1e-5);
// fit model with unchecked parameter set
let model = unchecked_params.fit(&ds)?;
// transform into a verified parameter set
let checked_params = unchecked_params.check()?;
// Regenerate model with the verified parameters, this only returns
// errors originating from the fitting process
let model = checked_params.fit(&ds)?;
Implementations
sourceimpl<F: Float> ElasticNetParams<F>
impl<F: Float> ElasticNetParams<F>
Configure and fit a Elastic Net model
sourcepub fn new() -> ElasticNetParams<F>
pub fn new() -> ElasticNetParams<F>
Create default elastic net hyper parameters
By default, an intercept will be fitted. To disable fitting an
intercept, call .with_intercept(false)
before calling .fit()
.
To additionally normalize the feature matrix before fitting, call
fit_intercept_and_normalize()
before calling fit()
. The feature
matrix will not be normalized by default.
sourcepub fn penalty(self, penalty: F) -> Self
pub fn penalty(self, penalty: F) -> Self
Set the overall parameter penalty parameter of the elastic net.
Use l1_ratio
to configure how the penalty distributed to L1 and L2
regularization.
sourcepub fn l1_ratio(self, l1_ratio: F) -> Self
pub fn l1_ratio(self, l1_ratio: F) -> Self
Set l1_ratio parameter of the elastic net. Controls how the parameter
penalty is distributed to L1 and L2 regularization.
Setting l1_ratio
to 1.0 is equivalent to a “Lasso” penalization,
setting it to 0.0 is equivalent to “Ridge” penalization.
Defaults to 0.5
if not set
l1_ratio
must be between 0.0
and 1.0
.
sourcepub fn with_intercept(self, with_intercept: bool) -> Self
pub fn with_intercept(self, with_intercept: bool) -> Self
Configure the elastic net model to fit an intercept.
Defaults to true
if not set.
sourcepub fn tolerance(self, tolerance: F) -> Self
pub fn tolerance(self, tolerance: F) -> Self
Set the tolerance which is the minimum absolute change in any of the model parameters needed for the parameter optimization to continue.
Defaults to 1e-4
if not set
sourcepub fn max_iterations(self, max_iterations: u32) -> Self
pub fn max_iterations(self, max_iterations: u32) -> Self
Set the maximum number of iterations for the optimization routine.
Defaults to 1000
if not set
Trait Implementations
sourceimpl<F: Clone> Clone for ElasticNetParams<F>
impl<F: Clone> Clone for ElasticNetParams<F>
sourcefn clone(&self) -> ElasticNetParams<F>
fn clone(&self) -> ElasticNetParams<F>
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
sourceimpl<F: Debug> Debug for ElasticNetParams<F>
impl<F: Debug> Debug for ElasticNetParams<F>
sourceimpl<F: Float> Default for ElasticNetParams<F>
impl<F: Float> Default for ElasticNetParams<F>
sourceimpl<F: Float> ParamGuard for ElasticNetParams<F>
impl<F: Float> ParamGuard for ElasticNetParams<F>
type Checked = ElasticNetValidParams<F>
type Checked = ElasticNetValidParams<F>
The checked hyperparameters
type Error = ElasticNetError
type Error = ElasticNetError
Error type resulting from failed hyperparameter checking
sourcefn check(self) -> Result<Self::Checked>
fn check(self) -> Result<Self::Checked>
Checks the hyperparameters and returns the checked hyperparameters if successful
sourcefn check_unwrap(self) -> Self::Checked
fn check_unwrap(self) -> Self::Checked
Calls check()
and unwraps the result
sourceimpl<F: PartialEq> PartialEq<ElasticNetParams<F>> for ElasticNetParams<F>
impl<F: PartialEq> PartialEq<ElasticNetParams<F>> for ElasticNetParams<F>
sourcefn eq(&self, other: &ElasticNetParams<F>) -> bool
fn eq(&self, other: &ElasticNetParams<F>) -> bool
This method tests for self
and other
values to be equal, and is used
by ==
. Read more
sourcefn ne(&self, other: &ElasticNetParams<F>) -> bool
fn ne(&self, other: &ElasticNetParams<F>) -> bool
This method tests for !=
.
impl<F> StructuralPartialEq for ElasticNetParams<F>
Auto Trait Implementations
impl<F> RefUnwindSafe for ElasticNetParams<F> where
F: RefUnwindSafe,
impl<F> Send for ElasticNetParams<F> where
F: Send,
impl<F> Sync for ElasticNetParams<F> where
F: Sync,
impl<F> Unpin for ElasticNetParams<F> where
F: Unpin,
impl<F> UnwindSafe for ElasticNetParams<F> where
F: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more