Class RidgeRegression<T>
- Namespace
- AiDotNet.Regression
- Assembly
- AiDotNet.dll
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.
public class RidgeRegression<T> : RegressionBase<T>, IRegression<T>, IFullModel<T, Matrix<T>, Vector<T>>, IModel<Matrix<T>, Vector<T>, ModelMetadata<T>>, IModelSerializer, ICheckpointableModel, IParameterizable<T, Matrix<T>, Vector<T>>, IFeatureAware, IFeatureImportance<T>, ICloneable<IFullModel<T, Matrix<T>, Vector<T>>>, IGradientComputable<T, Matrix<T>, Vector<T>>, IJitCompilable<T>
Type Parameters
TThe numeric type used for calculations (typically float or double).
- Inheritance
-
RidgeRegression<T>
- Implements
-
IRegression<T>
- Inherited Members
- Extension Methods
Remarks
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.
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 options = new RidgeRegressionOptions<double> { Alpha = 1.0 };
var ridge = new RidgeRegression<double>(options);
ridge.Train(features, targets);
var predictions = ridge.Predict(newFeatures);
Constructors
RidgeRegression(RidgeRegressionOptions<T>?, IRegularization<T, Matrix<T>, Vector<T>>?)
Initializes a new instance of the RidgeRegression<T> class.
public RidgeRegression(RidgeRegressionOptions<T>? options = null, IRegularization<T, Matrix<T>, Vector<T>>? regularization = null)
Parameters
optionsRidgeRegressionOptions<T>Configuration options for Ridge Regression. If null, default options are used.
regularizationIRegularization<T, Matrix<T>, Vector<T>>Optional additional regularization strategy.
Remarks
Creates a new Ridge Regression model with the specified options. The primary regularization is controlled by the Alpha parameter in the options. An additional regularization strategy can be provided for more complex regularization schemes.
For Beginners: This creates a new Ridge Regression model.
You can customize the model by providing options:
// Create with default options (alpha = 1.0)
var ridge = new RidgeRegression<double>();
// Create with custom alpha
var options = new RidgeRegressionOptions<double> { Alpha = 0.5 };
var ridge = new RidgeRegression<double>(options);
Methods
CreateNewInstance()
Creates a new instance of Ridge Regression with the same configuration.
protected override IFullModel<T, Matrix<T>, Vector<T>> CreateNewInstance()
Returns
- IFullModel<T, Matrix<T>, Vector<T>>
A new instance with the same options.
Deserialize(byte[])
Deserializes a Ridge Regression model from a byte array.
public override void Deserialize(byte[] modelData)
Parameters
modelDatabyte[]The byte array containing the serialized model.
GetModelMetadata()
Gets metadata about the Ridge Regression model.
public override ModelMetadata<T> GetModelMetadata()
Returns
- ModelMetadata<T>
A ModelMetadata object containing model information.
GetModelType()
Gets the model type identifier.
protected override ModelType GetModelType()
Returns
- ModelType
The ModelType enumeration value for Ridge Regression.
Serialize()
Serializes the Ridge Regression model to a byte array.
public override byte[] Serialize()
Returns
- byte[]
A byte array containing the serialized model.
Train(Matrix<T>, Vector<T>)
Trains the Ridge Regression model using the provided training data.
public override void Train(Matrix<T> x, Vector<T> y)
Parameters
xMatrix<T>The input features matrix where each row is a sample and each column is a feature.
yVector<T>The target values vector corresponding to each sample.
Remarks
Training computes the closed-form solution: w = (X^T X + alpha * I)^(-1) X^T y
This is numerically stable due to the regularization term, which ensures the matrix X^T X + alpha * I is always invertible (positive definite).
For Beginners: This method teaches the model to make predictions from your data.
The training process:
- Adds an intercept column (if UseIntercept = true)
- Computes the optimal coefficients using a direct mathematical formula
- Stores the coefficients for making predictions later
Unlike some other methods, Ridge Regression has a direct solution - no iterative optimization is needed, making training fast and deterministic.