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[,]) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| X | Float | feature matrix (rows=samples, cols=features) |
| y | Float | target matrix (rows=samples, cols=1) |
Return
| Type | Description |
|---|---|
| Bool | true if fitting succeeded |
GetCoefficients #
Gets the fitted feature coefficients (the intercept is separate).
method : public : GetCoefficients() ~ Float[]Return
| Type | Description |
|---|---|
| Float | coefficient array, or Nil if not fitted |
GetIntercept #
Gets the fitted intercept term.
method : public : GetIntercept() ~ FloatReturn
| Type | Description |
|---|---|
| Float | intercept |
GetMSE #
Gets the mean squared error over the training data.
method : public : GetMSE() ~ FloatReturn
| Type | Description |
|---|---|
| Float | MSE |
GetRMSE #
Gets the root mean squared error over the training data.
method : public : GetRMSE() ~ FloatReturn
| Type | Description |
|---|---|
| Float | RMSE |
GetRSquared #
Gets the R-squared over the training data.
method : public : GetRSquared() ~ FloatReturn
| Type | Description |
|---|---|
| Float | R-squared value |
IsFitted #
Whether the model has been fitted.
method : public : IsFitted() ~ BoolReturn
| Type | Description |
|---|---|
| Bool | true if fitted |
Load # function
Loads a fitted model from a file.
function : Load(filename:String) ~ ElasticNetParameters
| Name | Type | Description |
|---|---|---|
| filename | String | file to load from |
Return
| Type | Description |
|---|---|
| ElasticNet | fitted model, or Nil on failure |
New # constructor
Constructor with default optimization settings (1000 iterations, tolerance 0.0001).
New(alpha:Float, l1_ratio:Float)Parameters
| Name | Type | Description |
|---|---|---|
| alpha | Float | overall regularization strength (>= 0.0) |
| l1_ratio | Float | mix 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
| Name | Type | Description |
|---|---|---|
| alpha | Float | overall regularization strength (>= 0.0) |
| l1_ratio | Float | mix between L1 (1.0) and L2 (0.0) penalties |
| iterations | Int | maximum coordinate-descent sweeps |
| tolerance | Float | stop 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
| Name | Type | Description |
|---|---|---|
| X | Float | feature matrix |
Return
| Type | Description |
|---|---|
| Float | prediction matrix, or Nil if not fitted |