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.

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[,]) ~ Bool

Parameters

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

Return

TypeDescription
Booltrue if fitting succeeded

Fit #

Fits the model, optionally estimating an unpenalized intercept.

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

Parameters

NameTypeDescription
XFloatfeature matrix (rows=samples, cols=features)
yFloattarget matrix (rows=samples, cols=1)
fit_interceptBoolwhether to estimate a bias/intercept term

Return

TypeDescription
Booltrue if fitting succeeded (false on a singular system)

GetAdjustedRSquared #

Gets the adjusted R-squared over the training data.

method : public : GetAdjustedRSquared() ~ Float

Return

TypeDescription
Floatadjusted R-squared value

GetCoefficients #

Gets the fitted coefficients (index 0 is the intercept when fitted).

method : public : GetCoefficients() ~ Float[]

Return

TypeDescription
Floatcoefficient array, or Nil if not fitted

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) ~ RidgeRegression

Parameters

NameTypeDescription
filenameStringfile to load from

Return

TypeDescription
RidgeRegressionfitted model, or Nil on failure

New # constructor

Constructor

New(alpha:Float)

Parameters

NameTypeDescription
alphaFloatL2 regularization strength (>= 0.0)

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