Skip to Content
OchreML
Linear, GLM & Linear SVM

Lasso

Sparse L1-regularized linear regression trained via coordinate descent for automatic feature selection.

Overview

Lasso fits a linear model with an L1 penalty on the weights:

min (1 / (2 * n_samples)) * ||y - (X @ w + b)||_2^2 + alpha * ||w||_1

Because the L1 norm promotes exact zeros in coef_, Lasso performs simultaneous regression and feature selection. It supports both single-output and multi-output targets, optional sample_weight, and non-negativity constraints via positive=True.

When to Use

  • High-dimensional datasets where only a subset of features truly drive the target.
  • When sparse, interpretable models with exact zero weights are preferred over dense L2 shrinkage.
  • When correlated features should be selected together rather than arbitrarily picking one, prefer ElasticNet.

Hardware Backend Support

BackendPrimitives UsedSupport
CPU (Rayon)Dense E1-E5, N2 (f64)Full (default fallback)
CUDA (cuda:0, cuda:all)cuBLAS FP64 Gram & projections (E1-E5, N2)Full
wgpu (wgpu)WGSL compute shaders (E1-E5, N2)Full
TPU (tpu:0)OpenXLA PJRT StableHLO dot_general (E1-E5, N2)Full

Import & Constructor

from ochreml.linear_model import Lasso
# or top-level lazy import:
from ochreml import Lasso

model = Lasso(
    alpha: float = 1.0,
    fit_intercept: bool = True,
    max_iter: int = 1000,
    tol: float = 1e-4,
    positive: bool = False,
    device: str | None = None,
)

Parameters

ParameterTypeDefaultDescription
alphafloat1.0Constant multiplying the L1 penalty term. Must be non-negative; 0.0 corresponds to unregularized OLS.
fit_interceptboolTrueWhether to calculate the intercept for this model.
max_iterint1000Maximum number of coordinate descent iterations.
tolfloat1e-4Convergence tolerance for coordinate descent updates.
positiveboolFalseWhen True, constrains all fitted coefficients to be non-negative (w >= 0).
devicestr | NoneNoneTarget compute device ("auto", "cpu", "cuda:0", "wgpu", "tpu:0"). Defaults to ochreml.get_default_device().

Attributes

AttributeTypeDescription
coef_ndarray of shape (n_features,) or (n_targets, n_features)Sparse weight vector or matrix.
intercept_float or ndarray of shape (n_targets,)Independent bias term(s).
n_iter_int or list[int]Number of coordinate descent iterations run (per target for multi-output).
n_features_in_intNumber of features seen during fit.
device_strResolved hardware device string used by the estimator.

Methods

MethodSignatureDescription
fitfit(X, y, sample_weight=None) -> selfFit L1-regularized linear model on 1D or 2D targets.
predictpredict(X) -> ndarrayPredict continuous values using the sparse linear model.
scorescore(X, y) -> floatReturn coefficient of determination R^2.
toto(device: str) -> selfMove the estimator execution target to another hardware device.
save / loadmodel.save(path) / Lasso.load(path, device=None)Persist or restore the model in OchreML's binary .dit format.

Usage Example

import numpy as np
import ochreml
from ochreml.linear_model import Lasso

rng = np.random.default_rng(0)
X = rng.standard_normal((300, 10))
true_w = np.array([3.0, 0.0, -2.0, 0.0, 0.0, 1.5, 0.0, 0.0, 0.0, 0.0])
y = X @ true_w + 0.7 + rng.normal(0.0, 0.2, size=300)

model = Lasso(alpha=0.1, max_iter=1000, tol=1e-4, device="auto")
model.fit(X, y)

preds = model.predict(X[:5])
print(f"R^2: {model.score(X, y):.4f}, non-zero coefs: {np.count_nonzero(model.coef_)}")

# Persist to .dit and reload
model.save("lasso_model.dit")
loaded = ochreml.load("lasso_model.dit", device="cpu")
assert np.allclose(preds, loaded.predict(X[:5]))

On this page