RidgeRegression<T>

Models & Types · AiDotNet.Regression

Implements Ridge Regression (L2 regularized linear regression), which extends ordinary least squares by adding a penalty term proportional to the squared magnitude of the coefficients.

Inheritance: RegressionBase<T> → RidgeRegression<T>

For Beginners

Ridge Regression is a safer version of linear regression.

Regular linear regression can become unstable when:

  • You have many features relative to samples
  • Some features are highly correlated with each other
  • The data contains noise

Ridge Regression fixes these issues by adding a “penalty” for large coefficients:

  • It prevents any single feature from dominating the prediction
  • It makes the model more stable and reliable
  • It typically improves predictions on new, unseen data

Think of it like putting rubber bands on a flexible ruler - the bands (regularization) keep the ruler from bending too wildly (overfitting), while still allowing it to follow the general trend of the data.

Example usage:

var features = new Matrix<double>(new double[,] { { 1.0, 2.0 }, { 3.0, 4.0 }, { 5.0, 6.0 }, { 7.0, 8.0 } });
var targets = new Vector<double>(new double[] { 3.1, 7.2, 11.0, 15.1 });

var result = new AiModelBuilder<double, Matrix<double>, Vector<double>>()
.ConfigureModel(new RidgeRegression<double>(new RidgeRegressionOptions<double> { Alpha = 1.0 }))
.Build(features, targets);

var newFeatures = new Matrix<double>(new double[,] { { 9.0, 10.0 } });
var predictions = result.Predict(newFeatures);

How It Works

Ridge Regression solves the following optimization problem: minimize ||y - Xw||^2 + alpha * ||w||^2

This has a closed-form solution: w = (X^T X + alpha * I)^(-1) X^T y

The L2 penalty shrinks coefficients toward zero but never sets them exactly to zero, making Ridge Regression suitable for problems where all features are expected to contribute.

Example

using AiDotNet;
using AiDotNet.Data.Loaders;
using AiDotNet.Regression;
using AiDotNet.Tensors.LinearAlgebra;

double[][] features =
{
    new[] { 1.0, 2.0 }, new[] { 2.0, 3.0 }, new[] { 3.0, 4.0 },
    new[] { 4.0, 5.0 }, new[] { 5.0, 6.0 }, new[] { 6.0, 7.0 }
};
double[] targets = { 3.0, 5.0, 7.0, 9.0, 11.0, 13.0 };

var result = await new AiModelBuilder<double, Matrix<double>, Vector<double>>()
    .ConfigureModel(new RidgeRegression<double>())
    .ConfigureDataLoader(DataLoaders.FromArrays(features, targets))
    .BuildAsync();

Console.WriteLine("Trained RidgeRegression.");

Constructors

Constructor Summary
RidgeRegression(RidgeRegressionOptions<T> options, IRegularization<T, Matrix<T>, Vector<T>> regularization) Initializes a new instance of the RidgeRegression class.

Methods

Method Summary
ModelMetadata<T> GetModelMetadata() Gets metadata about the Ridge Regression model.
void Train(Matrix<T> x, Vector<T> y) Trains the Ridge Regression model using the provided training data.