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||_1Because 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
| Backend | Primitives Used | Support |
|---|---|---|
| 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
| Parameter | Type | Default | Description |
|---|---|---|---|
alpha | float | 1.0 | Constant multiplying the L1 penalty term. Must be non-negative; 0.0 corresponds to unregularized OLS. |
fit_intercept | bool | True | Whether to calculate the intercept for this model. |
max_iter | int | 1000 | Maximum number of coordinate descent iterations. |
tol | float | 1e-4 | Convergence tolerance for coordinate descent updates. |
positive | bool | False | When True, constrains all fitted coefficients to be non-negative (w >= 0). |
device | str | None | None | Target compute device ("auto", "cpu", "cuda:0", "wgpu", "tpu:0"). Defaults to ochreml.get_default_device(). |
Attributes
| Attribute | Type | Description |
|---|---|---|
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_ | int | Number of features seen during fit. |
device_ | str | Resolved hardware device string used by the estimator. |
Methods
| Method | Signature | Description |
|---|---|---|
fit | fit(X, y, sample_weight=None) -> self | Fit L1-regularized linear model on 1D or 2D targets. |
predict | predict(X) -> ndarray | Predict continuous values using the sparse linear model. |
score | score(X, y) -> float | Return coefficient of determination R^2. |
to | to(device: str) -> self | Move the estimator execution target to another hardware device. |
save / load | model.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]))