Skip to main content

Lasso

#include <Skigen/LinearModel>

template <typename Scalar = double>
class Skigen::Lasso(alpha=1, fit_intercept=true, max_iter=1000, tol=1e-4)

Linear Model trained with L1 prior as regularizer (aka the Lasso).

The optimization objective for Lasso is:

12nsamplesyXw22+αw1\frac{1}{2n_{\mathrm{samples}}} \|y - Xw\|_2^2 + \alpha \|w\|_1

Technically the Lasso model is optimizing the same objective function as the ElasticNet with l1_ratio = 1.0 (no L2 penalty).

The L1 penalty induces sparsity, driving some coefficients exactly to zero.

Mirrors sklearn.linear_model.Lasso.

Read more in the User Guide.


Parameters:

  • alpha : Scalar, default=1 Constant that multiplies the L1 term (Scalar, default 1). Controls regularization strength. alpha = 0 is equivalent to ordinary least squares (use LinearRegression instead for numerical stability).

  • fit_intercept : bool, default=true Whether the intercept should be estimated (bool, default true). If false, the data is assumed to be already centered.

  • max_iter : int, default=1000 The maximum number of iterations (int, default 1000).

  • tol : Scalar, default=1e-4 The tolerance for the optimization (Scalar, default 1e-4): if the maximum coordinate update is smaller than tol, the solver stops.


Attributes:

  • coef : RowVectorType Parameter vector ww (1 × n_features).

  • intercept : Scalar Independent term in the decision function.


Methods

fit(X, y)

Fit the Lasso model via coordinate descent.

Centers the data when fit_intercept is true, then runs coordinate descent with soft-thresholding until convergence or max_iter iterations.

Parameters:

  • X : MatrixType Design matrix of shape (n_samples, n_features).

  • y : VectorType Target vector of shape (n_samples,). Will be cast to Scalar if necessary.

Returns:

  • result : Lasso Reference to the fitted estimator (*this).
note

sklearn parity gap: sample_weight and check_input parameters are not yet supported.


predict(X)

Predict using the linear model.

Computes y^=Xw+b\hat{y} = X w + b where ww and bb are the fitted coefficients and intercept.

Parameters:

  • X : MatrixType Sample matrix of shape (n_samples, n_features).

Returns:

  • result : VectorType Predicted values of shape (n_samples,).

Throws:

  • std::runtime_error — if the model has not been fitted.

score(X, y)

Return the R2R^2 coefficient of determination on test data.

R2=1(yiy^i)2(yiyˉ)2R^2 = 1 - \frac{\sum (y_i - \hat{y}_i)^2}{\sum (y_i - \bar{y})^2}. Best possible score is 1.0; it can be negative if the model is arbitrarily worse than predicting the mean.

Parameters:

  • X : MatrixType Test samples of shape (n_samples, n_features).

  • y : VectorType True values of shape (n_samples,).

Returns:

  • result : Scalar R2R^2 score.

Throws:

  • std::runtime_error — if the model has not been fitted.

Example

// Lasso with varying alpha — higher alpha = more sparsity
std::cout << "=== Lasso: feature selection via L1 ===\n";
for (double alpha : {0.001, 0.01, 0.1, 0.5}) {
Skigen::Lasso<double> model(alpha);
model.fit(X_tr, split.y_train);

int nonzero = (model.coef().array().abs() > 1e-10).count();
std::cout << " alpha=" << std::setw(5) << alpha
<< " non-zero=" << nonzero
<< " R²=" << model.score(X_te, split.y_test)
<< " coef=" << model.coef() << "\n";
}