Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Lasso

API Reference

Signature

model = sp.Lasso(alpha=1.0, max_iter=1000, tol=1e-4, fit_intercept=True)

model.fit(X, y)
model.predict(X)   -> list[float]
model.score(X, y)  -> float
model.get_params() -> dict
model.set_params(alpha=..., max_iter=..., tol=..., fit_intercept=...)

Constructor parameters

ParameterTypeDefaultDescription
alphafloat1.0L1 penalty strength
max_iterint1000Maximum coordinate descent iterations
tolfloat1e-4Convergence tolerance
fit_interceptboolTrueFit a bias term

Attributes

AttributeTypeDescription
coef_list[float]Fitted coefficients (sparse)
intercept_floatBias term
alpha_floatRegularisation parameter
n_iter_intActual iterations performed
Example
import seraplot as sp
import numpy as np

X = np.random.randn(300, 20)
true_coef = np.zeros(20)
true_coef[:3] = [3.0, -2.0, 1.5]
y = X @ true_coef + np.random.randn(300) * 0.2

model = sp.Lasso(alpha=0.1)
model.fit(X, y)
non_zero = sum(1 for c in model.coef_ if abs(c) > 1e-6)
print(f"R2: {model.score(X, y):.4f}")
print(f"Non-zero coefficients: {non_zero} / 20")

Algorithmic Functioning

Lasso minimises the L1-penalised objective:

$$\hat{\beta} = \underset{\beta}{\arg\min}\ \frac{1}{2n}\|y - X\beta\|_2^2 + \alpha\|\beta\|_1$$

Coordinate descent updates one coefficient at a time while holding the rest fixed. For each $j$:

$$r_j = y - X_{-j}\hat{\beta}_{-j}$$
$$\hat{\beta}_j \leftarrow \frac{S\!\left(X_j^T r_j / n,\ \alpha\right)}{\|X_j\|_2^2 / n}$$

where $S(\cdot, \lambda)$ is the soft-threshold operator:

$$S(z, \lambda) = \text{sign}(z)\max(|z| - \lambda, 0)$$

This sets small coefficients exactly to zero, producing sparse solutions.

Convergence — iterations stop when $\max_j |\beta_j^{(t)} - \beta_j^{(t-1)}| < \texttt{tol}$ or after max_iter passes. The checkpoint_id argument enables training resumed across multiple Python calls.

Référence API

Signature

model = sp.Lasso(alpha=1.0, max_iter=1000, tol=1e-4, fit_intercept=True)

model.fit(X, y)
model.predict(X)   -> list[float]
model.score(X, y)  -> float
model.get_params() -> dict
model.set_params(alpha=..., max_iter=..., tol=..., fit_intercept=...)

Paramètres du constructeur

ParamètreTypeDéfautDescription
alphafloat1.0Force de pénalité L1
max_iterint1000Nombre maximum d'itérations de descente de coordonnées
tolfloat1e-4Tolérance de convergence
fit_interceptboolTrueAjuster un terme de biais

Attributs

AttributTypeDescription
coef_list[float]Coefficients ajustés (creux)
intercept_floatTerme de biais
alpha_floatParamètre de régularisation
n_iter_intNombre d'itérations réalisées
Exemple
import seraplot as sp
import numpy as np

X = np.random.randn(300, 20)
true_coef = np.zeros(20)
true_coef[:3] = [3.0, -2.0, 1.5]
y = X @ true_coef + np.random.randn(300) * 0.2

model = sp.Lasso(alpha=0.1)
model.fit(X, y)
non_zero = sum(1 for c in model.coef_ if abs(c) > 1e-6)
print(f"R2 : {model.score(X, y):.4f}")
print(f"Coefficients non nuls : {non_zero} / 20")

Fonctionnement algorithmique

Lasso minimise l'objectif avec pénalité L1 :

$$\hat{\beta} = \underset{\beta}{\arg\min}\ \frac{1}{2n}\|y - X\beta\|_2^2 + \alpha\|\beta\|_1$$

La descente de coordonnées met à jour un coefficient à la fois en fixant les autres. Pour chaque $j$ :

$$r_j = y - X_{-j}\hat{\beta}_{-j}$$
$$\hat{\beta}_j \leftarrow \frac{S\!\left(X_j^T r_j / n,\ \alpha\right)}{\|X_j\|_2^2 / n}$$

où $S(\cdot, \lambda)$ est l'opérateur de seuillage doux :

$$S(z, \lambda) = \text{sign}(z)\max(|z| - \lambda, 0)$$

Cela annule exactement les petits coefficients, produisant des solutions creuses.

Convergence — Les itérations s'arrêtent quand $\max_j |\beta_j^{(t)} - \beta_j^{(t-1)}| < \texttt{tol}$ ou après max_iter passes. L'argument checkpoint_id permet un entraînement repris entre plusieurs appels Python.