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[,]) ~ 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 |
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) ~ 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 |
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() ~ FloatReturn
| Type | Description |
|---|---|
| Float | adjusted R-squared value |
GetCoefficients #
Gets the computed coefficients.
method : public : GetCoefficients() ~ Float[]Return
| Type | Description |
|---|---|
| Float | coefficient 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() ~ 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 goodness of fit.
method : public : GetRSquared() ~ FloatReturn
| Type | Description |
|---|---|
| Float | R-squared value |
Example
"Model R²={$model->GetRSquared()}"->PrintLine();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) ~ LinearRegressionParameters
| Name | Type | Description |
|---|---|---|
| filename | String | file to load from |
Return
| Type | Description |
|---|---|
| LinearRegression | fitted model, or Nil on failure |
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 |
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[,]) ~ FloatParameters
| Name | Type | Description |
|---|---|---|
| X | Float | feature matrix |
| y | Float | true target matrix (rows x 1) |
Return
| Type | Description |
|---|---|
| Float | R-squared, or 0.0 if not fitted or inputs are invalid |