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
| Parameter | Type | Default | Description |
|---|---|---|---|
alpha | float | 1.0 | L1 penalty strength |
max_iter | int | 1000 | Maximum coordinate descent iterations |
tol | float | 1e-4 | Convergence tolerance |
fit_intercept | bool | True | Fit a bias term |
Attributes
| Attribute | Type | Description |
|---|---|---|
coef_ | list[float] | Fitted coefficients (sparse) |
intercept_ | float | Bias term |
alpha_ | float | Regularisation parameter |
n_iter_ | int | Actual 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:
Coordinate descent updates one coefficient at a time while holding the rest fixed. For each $j$:
where $S(\cdot, \lambda)$ is the soft-threshold operator:
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ètre | Type | Défaut | Description |
|---|---|---|---|
alpha | float | 1.0 | Force de pénalité L1 |
max_iter | int | 1000 | Nombre maximum d'itérations de descente de coordonnées |
tol | float | 1e-4 | Tolérance de convergence |
fit_intercept | bool | True | Ajuster un terme de biais |
Attributs
| Attribut | Type | Description |
|---|---|---|
coef_ | list[float] | Coefficients ajustés (creux) |
intercept_ | float | Terme de biais |
alpha_ | float | Paramètre de régularisation |
n_iter_ | int | Nombre 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 :
La descente de coordonnées met à jour un coefficient à la fois en fixant les autres. Pour chaque $j$ :
où $S(\cdot, \lambda)$ est l'opérateur de seuillage doux :
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.