v2026.6.1
All Bundles
Bundle Core machine learning types: the seedable Random generator, Matrix2D linear algebra and activations, matrix reference holders, the NeuralNetwork and CSV-backed MatrixReader. Compile with -lib ml.

ElasticNet

Elastic-net regularized linear regression fitted by cyclic coordinate descent on mean-centered data with an unpenalized intercept. Minimizes (1/2n)||y - Xw - b||^2 + alpha*(l1_ratio*|w|_1 + (1-l1_ratio)/2*|w|_2^2). l1_ratio = 1.0 is the lasso; l1_ratio = 0.0 is ridge (use RidgeRegression for the closed-form solution). The L1 term drives uninformative feature coefficients exactly to zero.

Example

model := ElasticNet->New(0.1, 0.5);
model->Fit(X, y);
preds := model->Predict(X);

Operations

Fit #

Fits the model using cyclic coordinate descent.

method : public : Fit(X:Float[,], y:Float[,]) ~ Bool

Parameters

NameTypeDescription
XFloatfeature matrix (rows=samples, cols=features)
yFloattarget matrix (rows=samples, cols=1)

Return

TypeDescription
Booltrue if fitting succeeded

GetCoefficients #

Gets the fitted feature coefficients (the intercept is separate).

method : public : GetCoefficients() ~ Float[]

Return

TypeDescription
Floatcoefficient array, or Nil if not fitted

GetIntercept #

Gets the fitted intercept term.

method : public : GetIntercept() ~ Float

Return

TypeDescription
Floatintercept

GetMSE #

Gets the mean squared error over the training data.

method : public : GetMSE() ~ Float

Return

TypeDescription
FloatMSE

GetRMSE #

Gets the root mean squared error over the training data.

method : public : GetRMSE() ~ Float

Return

TypeDescription
FloatRMSE

GetRSquared #

Gets the R-squared over the training data.

method : public : GetRSquared() ~ Float

Return

TypeDescription
FloatR-squared value

IsFitted #

Whether the model has been fitted.

method : public : IsFitted() ~ Bool

Return

TypeDescription
Booltrue if fitted

Load # function

Loads a fitted model from a file.

function : Load(filename:String) ~ ElasticNet

Parameters

NameTypeDescription
filenameStringfile to load from

Return

TypeDescription
ElasticNetfitted model, or Nil on failure

New # constructor

Constructor with default optimization settings (1000 iterations, tolerance 0.0001).

New(alpha:Float, l1_ratio:Float)

Parameters

NameTypeDescription
alphaFloatoverall regularization strength (>= 0.0)
l1_ratioFloatmix between L1 (1.0) and L2 (0.0) penalties

New # constructor

Constructor with explicit optimization settings.

New(alpha:Float, l1_ratio:Float, iterations:Int, tolerance:Float)

Parameters

NameTypeDescription
alphaFloatoverall regularization strength (>= 0.0)
l1_ratioFloatmix between L1 (1.0) and L2 (0.0) penalties
iterationsIntmaximum coordinate-descent sweeps
toleranceFloatstop when no coefficient moves more than this in a sweep

Predict #

Predicts target values for new input data.

method : public : Predict(X:Float[,]) ~ Float[,]

Parameters

NameTypeDescription
XFloatfeature matrix

Return

TypeDescription
Floatprediction matrix, or Nil if not fitted

Score #

Scores the model on the given data (R-squared of predictions).

method : public : Score(X:Float[,], y:Float[,]) ~ Float

Parameters

NameTypeDescription
XFloatfeature matrix
yFloattrue target matrix (rows x 1)

Return

TypeDescription
FloatR-squared, or 0.0 if not fitted or inputs are invalid

Store #

Saves the fitted model to a file.

method : public : Store(filename:String) ~ Bool

Parameters

NameTypeDescription
filenameStringfile to store to

Return

TypeDescription
Booltrue if successful, false otherwise