RidgeRegression
Ridge (L2-regularized) linear regression solved in closed form via the regularized normal equations (X'X + alpha*I)w = X'y. The intercept term, when fitted, is not penalized. Larger alpha shrinks coefficients toward zero; alpha = 0.0 reduces to ordinary least squares.
Example
X := [[1.0], [2.0], [3.0]];
y := [[3.0], [5.0], [7.0]]; # y = 2x + 1
model := RidgeRegression->New(0.1);
model->Fit(X, y);
"slope~2: {$model->GetCoefficients()[1]}"->PrintLine();Operations
Fit #
Fits the model with an intercept term.
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 |
Fit #
Fits the model, optionally estimating an unpenalized intercept.
method : public : Fit(X:Float[,], y:Float[,], fit_intercept:Bool) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| X | Float | feature matrix (rows=samples, cols=features) |
| y | Float | target matrix (rows=samples, cols=1) |
| fit_intercept | Bool | whether to estimate a bias/intercept term |
Return
| Type | Description |
|---|---|
| Bool | true if fitting succeeded (false on a singular system) |
GetAdjustedRSquared #
Gets the adjusted R-squared over the training data.
method : public : GetAdjustedRSquared() ~ FloatReturn
| Type | Description |
|---|---|
| Float | adjusted R-squared value |
GetCoefficients #
Gets the fitted coefficients (index 0 is the intercept when fitted).
method : public : GetCoefficients() ~ Float[]Return
| Type | Description |
|---|---|
| Float | coefficient array, or Nil if not fitted |
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) ~ RidgeRegressionParameters
| Name | Type | Description |
|---|---|---|
| filename | String | file to load from |
Return
| Type | Description |
|---|---|
| RidgeRegression | fitted model, or Nil on failure |
New # constructor
Constructor
New(alpha:Float)Parameters
| Name | Type | Description |
|---|---|---|
| alpha | Float | L2 regularization strength (>= 0.0) |
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 |