v2026.6.4
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.

LinearRegression

Simple linear regression using least squares. Fits y = X * coefficients via the normal equation.

Example

X := [[1.0, 1.0], [1.0, 2.0], [1.0, 3.0]];
y := [[2.0], [4.0], [6.0]];
model := LinearRegression->New();
model->Fit(X, y);
preds := model->Predict([[1.0, 4.0]]);
"Predicted: {$preds[0,0]}"->PrintLine();
"R-squared: {$model->GetRSquared()}"->PrintLine();

Operations

Fit #

Fits the model to training data using least squares.

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

Example

X := [[1.0, 1.0], [1.0, 2.0], [1.0, 3.0]];
y := [[2.0], [4.0], [6.0]];
model := LinearRegression->New();
if(model->Fit(X, y)) {
  "Fitted. R²={$model->GetRSquared()}"->PrintLine();
};

Fit #

Fits the model to training data, optionally estimating an intercept term. When fit_intercept is true a leading column of 1.0s is prepended to the design matrix and coefficient 0 holds the intercept. Computes R-squared, adjusted R-squared, MSE and RMSE; returns false on a singular / ill- conditioned system (the underlying solver yields no solution).

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

Example

X := [[1.0], [2.0], [3.0]];
y := [[3.0], [5.0], [7.0]];   # y = 2x + 1
model := LinearRegression->New();
model->Fit(X, y, true);       # intercept ~ 1, slope ~ 2
"RMSE={$model->GetRMSE()}"->PrintLine();

GetAdjustedRSquared #

Gets the adjusted R-squared, which penalizes additional predictors.

method : public : GetAdjustedRSquared() ~ Float

Return

TypeDescription
Floatadjusted R-squared value

GetCoefficients #

Gets the computed coefficients.

method : public : GetCoefficients() ~ Float[]

Return

TypeDescription
Floatcoefficient array, or Nil if not fitted

Example

coeffs := model->GetCoefficients();
each(c in coeffs) {
  c->PrintLine();
};

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 goodness of fit.

method : public : GetRSquared() ~ Float

Return

TypeDescription
FloatR-squared value

Example

"Model R²={$model->GetRSquared()}"->PrintLine();

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

Parameters

NameTypeDescription
filenameStringfile to load from

Return

TypeDescription
LinearRegressionfitted model, or Nil on failure

New # constructor

Constructor

New()

Example

model := LinearRegression->New();

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

Example

preds := model->Predict([[1.0, 5.0], [1.0, 6.0]]);
each(i : preds->Size()[0]) {
  preds[i,0]->PrintLine();
};

Score #

Scores the model on the given data, returning the R-squared (coefficient of determination) of its predictions against the true targets.

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 (coefficients, intercept convention and training metrics) to a file.

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

Parameters

NameTypeDescription
filenameStringfile to store to

Return

TypeDescription
Booltrue if successful, false otherwise