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

NameDefaultPurposeRange
penalty1.0Overall parameter penalty[0, inf)
l1_ratio0.5Distribution of penalty to L1 and L2 regularizations[0.0, 1.0]
with_intercepttrueEnable interceptfalse, true
tolerance1e-4Absolute change of any of the parameters(0, inf)
max_iterations1000Maximum 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

Configure and fit a Elastic Net model

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.

Set the overall parameter penalty parameter of the elastic net. Use l1_ratio to configure how the penalty distributed to L1 and L2 regularization.

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.

Configure the elastic net model to fit an intercept. Defaults to true if not set.

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

Set the maximum number of iterations for the optimization routine.

Defaults to 1000 if not set

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

Validate the hyper parameters

The checked hyperparameters

Error type resulting from failed hyperparameter checking

Checks the hyperparameters and returns the checked hyperparameters if successful

Calls check() and unwraps the result

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.