Class LassoRegression<T>
- Namespace
- AiDotNet.Regression
- Assembly
- AiDotNet.dll
Implements Lasso Regression (L1 regularized linear regression), which extends ordinary least squares by adding a penalty term proportional to the absolute magnitude of the coefficients.
public class LassoRegression<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
-
LassoRegression<T>
- Implements
-
IRegression<T>
- Inherited Members
- Extension Methods
Remarks
Lasso Regression solves the following optimization problem: minimize (1/2n) * ||y - Xw||^2 + alpha * ||w||_1
Unlike Ridge Regression, Lasso uses coordinate descent optimization because the L1 penalty is not differentiable at zero.
The L1 penalty can shrink coefficients exactly to zero, making Lasso useful for automatic feature selection.
For Beginners: Lasso Regression automatically selects important features.
While Ridge Regression keeps all features but shrinks their coefficients, Lasso can completely eliminate unimportant features by setting their coefficients to zero. This is useful when:
- You have many features and want to identify the most important ones
- You want a simpler, more interpretable model
- You suspect only a few features actually matter
Example usage:
var options = new LassoRegressionOptions<double> { Alpha = 1.0 };
var lasso = new LassoRegression<double>(options);
lasso.Train(features, targets);
var predictions = lasso.Predict(newFeatures);
// Check which features were selected (non-zero coefficients)
var selectedFeatures = lasso.GetActiveFeatureIndices();
Constructors
LassoRegression(LassoRegressionOptions<T>?, IRegularization<T, Matrix<T>, Vector<T>>?)
Initializes a new instance of the LassoRegression<T> class.
public LassoRegression(LassoRegressionOptions<T>? options = null, IRegularization<T, Matrix<T>, Vector<T>>? regularization = null)
Parameters
optionsLassoRegressionOptions<T>Configuration options for Lasso Regression. If null, default options are used.
regularizationIRegularization<T, Matrix<T>, Vector<T>>Optional additional regularization strategy.
Remarks
Creates a new Lasso Regression model with the specified options. The primary regularization is controlled by the Alpha parameter in the options.
For Beginners: This creates a new Lasso Regression model.
You can customize the model by providing options:
// Create with default options (alpha = 1.0)
var lasso = new LassoRegression<double>();
// Create with custom alpha (more aggressive feature selection)
var options = new LassoRegressionOptions<double> { Alpha = 2.0 };
var lasso = new LassoRegression<double>(options);
Properties
IterationsUsed
Gets the number of iterations used in the last training.
public int IterationsUsed { get; }
Property Value
NumberOfSelectedFeatures
Gets the number of non-zero coefficients (selected features).
public int NumberOfSelectedFeatures { get; }
Property Value
Methods
CreateNewInstance()
Creates a new instance of Lasso 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 Lasso 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 Lasso 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 Lasso Regression.
Serialize()
Serializes the Lasso 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 Lasso Regression model using coordinate descent optimization.
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 uses coordinate descent optimization, which updates one coefficient at a time while holding others fixed. For each coefficient, the update uses the soft-thresholding operator to apply the L1 penalty.
For Beginners: This method teaches the model to make predictions from your data.
The training process uses an iterative algorithm called coordinate descent:
- Start with all coefficients at zero (or warm start from previous solution)
- For each feature, compute the optimal coefficient value
- Apply "soft thresholding" which can set small values to exactly zero
- Repeat until coefficients stop changing significantly
This approach is slower than Ridge Regression's direct solution, but it's the only way to get exact zero coefficients for feature selection.