ElasticNet
Module: strata.linear_model • Kind: struct • Traits: Copyable, Movable, Regressor, Serializable
Source: strata/linear_model/elastic_net.mojo
struct ElasticNet[compute_dtype: DType = DType.float64](Copyable, Movable, Regressor, Serializable)from strata.linear_model import ElasticNetLinear regression with combined L1 and L2 regularization (ElasticNet).
Minimizes the penalized least-squares objective function using coordinate descent:
where corresponds to l1_ratio.
Parameters (Compile-Time)
Section titled “Parameters (Compile-Time)”| Parameter | Description |
|---|---|
compute_dtype |
Computational precision data type. Default DType.float64. |
Constructors
Section titled “Constructors”def __init__( out self, alpha: Scalar[Self.compute_dtype] = 1.0, l1_ratio: Scalar[Self.compute_dtype] = 0.5, fit_intercept: Bool = True, max_iter: Int = 1000, tol: Scalar[Self.compute_dtype] = 1e-4, positive: Bool = False,)Initialize the ElasticNet regression estimator.
| Argument | Type | Description |
|---|---|---|
alpha |
Scalar[Self.compute_dtype] |
Regularization strength (must be non-negative). Default 1.0. |
l1_ratio |
Scalar[Self.compute_dtype] |
Mixing parameter in [0, 1]. Default 0.5. |
fit_intercept |
Bool |
Whether to fit an intercept term. Default True. |
max_iter |
Int |
Maximum iterations for coordinate descent. Default 1000. |
tol |
Scalar[Self.compute_dtype] |
Convergence tolerance threshold. Default 1e-4. |
positive |
Bool |
Force non-negative coefficients. Default False. |
Attributes
Section titled “Attributes”| Attribute | Description |
|---|---|
coef_ |
Weight vector coefficients of length . |
intercept_ |
Independent bias intercept term. |
n_iter_ |
Actual number of coordinate descent iterations run. |
is_fitted |
Boolean flag indicating if the model has been fitted. |
n_features_in_ |
Number of input features observed during fitting. |
Methods Overview
Section titled “Methods Overview”| Method | Description |
|---|---|
ElasticNet.fit() |
Fit the ElasticNet linear model via coordinate descent. |
ElasticNet.predict() |
Predict continuous target values using the fitted ElasticNet model. |
ElasticNet.serialize() |
Serializes ElasticNet parameters and fitted state into BufferWriter. |
ElasticNet.deserialize() |
Deserializes ElasticNet from BufferReader. |
Method Details
Section titled “Method Details”ElasticNet.fit()
Section titled “ElasticNet.fit()”def fit[feat_dtype: DType, in_target_dtype: DType]( mut self, X: Matrix[feat_dtype], y: List[Scalar[in_target_dtype]],)
def fit[feat_dtype: DType, target_dtype: DType]( mut self, dataset: Dataset[feat_dtype, target_dtype],)Overload Note: This method accepts either standard
(X, y)inputs or a unifieddataset: Datasetcontainer.
Fit the ElasticNet linear model via coordinate descent.
| Parameter | Type | Description |
|---|---|---|
X |
Matrix[feat_dtype] |
Training feature matrix of shape . |
y |
List[Scalar[in_target_dtype]] |
Target values vector of length . |
dataset |
Dataset[feat_dtype, target_dtype] |
Dataset container holding feature matrix and targets. (Can be provided alternatively in place of (X, y)) |
ElasticNet.predict()
Section titled “ElasticNet.predict()”def predict[feat_dtype: DType]( self, X: Matrix[feat_dtype],) -> List[Scalar[feat_dtype]]
def predict[feat_dtype: DType, target_dtype: DType]( self, dataset: Dataset[feat_dtype, target_dtype],) -> List[Scalar[feat_dtype]]Overload Note: This method accepts either standard
(X, y)inputs or a unifieddataset: Datasetcontainer.
Predict continuous target values using the fitted ElasticNet model.
| Parameter | Type | Description |
|---|---|---|
X |
Matrix[feat_dtype] |
Feature matrix of shape to predict on. |
dataset |
Dataset[feat_dtype, target_dtype] |
Dataset container holding feature matrix and targets. (Can be provided alternatively in place of (X, y)) |
Returns: List[Scalar[feat_dtype]] — List[Scalar[feat_dtype]]: Predicted target vector of length .
ElasticNet.serialize()
Section titled “ElasticNet.serialize()”def serialize(self, mut writer: BufferWriter)Serializes ElasticNet parameters and fitted state into BufferWriter.
ElasticNet.deserialize()
Section titled “ElasticNet.deserialize()”def deserialize(mut reader: BufferReader) -> SelfDeserializes ElasticNet from BufferReader.
Returns: Self
Section titled “Returns: Self”Example
Section titled “Example”from strata.linear_model import ElasticNetfrom strata.core import Matrix
var reg = ElasticNet[DType.float64](alpha=0.1, l1_ratio=0.7)reg.fit(X_train, y_train)var preds = reg.predict(X_test)