polars_online.gram.lasso_path¶
- polars_online.gram.lasso_path(g: Gram, penalties: Sequence[float], *, l1_ratio: float = 1.0, penalty_weights: Sequence[float] | None = None, target: str | int = 0, features: Sequence[str | int] | None = None, max_iter: int = 1000, tol: float = 1e-10) Any[source]¶
Compute the elastic-net path from a Gram, one row of coefficients per penalty.
Use it to fit the lasso or the elastic net on a Gram over a grid of penalties you choose, as the
lassomodel does over its stream. For the start of the lasso path, knot by knot and exactly, uselars_path(). For a ridge, usesolve().Parameters
gThe Gram, as
gram()returns it.penaltiesThe penalties
l, from large to small, as a path is meant to be walked: each penalty’s descent starts from the fit at the one before it, which makes the walk faster and better conditioned. They are alassospec’spenalties, andbank.coef()reports each aspenalty. Each is refused unless finite and at least 0. The keyword’s older name,lambdas, is refused, and the error names this one.l1_ratioThe share of the penalty that is L1. Default 1, the lasso; below 1, an elastic net. Refused outside
[0, 1].penalty_weightsA scale on the penalty per feature, in
featuresorder (or incolumnsorder without the intercept): 0 leaves a column unpenalized. A column with no variance in the Gram is dropped whatever its weight. DefaultNone, every weight 1. Thelassomodel takes no such parameter: here the path is walked again on every call, so each call may weigh the columns its own way. Refused at the wrong length, or with a weight that is not finite and at least 0.target,featuresAs for
solve().max_iterThe most sweeps of the coordinate descent at each penalty. Default 1,000, where the model’s is 100, since this path starts its first penalty from zero and the model starts each solve from its last fit. Refused below 1.
tolThe stopping rule: a sweep ends the descent when it moves no standardized coefficient by
tol * sor more, or moves none at all, withsthe target’s spread (Notes). Default 1e-10, the model’s. Refused unless finite and above 0.
Returns
A float array of shape
(len(penalties), k)over the Gram’skcolumns, the intercept first when there is one, in the features’ original units with the intercept recovered from the means, so a row is directly comparable tobank.coef(). On a Gram pooled within parts (merge(within=True)) the intercept isnan: such a Gram has an intercept per part and none of its own.Raises
ValueErrorA number outside its range, by the rule a spec applies to the same parameter (each listed under Parameters);
penalty_weightsof the wrong length; the intercept or a column named twice amongfeatures, as forsolve().KeyError,IndexErrorAs for
solve().TypeErrorpenalties’ older name,lambdas, is given.ReadinessWarningA warning, not an error: issued for each penalty whose descent runs out of sweeps before it meets
tol. That row keeps the coefficients where the descent stopped. Increasemax_iter, or take the knots exactly fromlars_path().
Examples
This code reads
dffrom Example data:spec = po.spec.ewridge("ridge", targets=["y"], features=["x0", "x1"], half_life=100.0) bank = po.ModelBank([spec]) bank.fit_predict(df) g = bank.gram("ridge")[0] path = po.gram.lasso_path(g, [1.0, 0.1, 0.001], target="y") # one row per penalty print(path.round(3) + 0.0) # + 0.0 prints -0.0 as 0.0
[[ 0.966 0.784 0. ] [ 0.991 1.848 -0.885] [ 0.992 1.968 -1.002]]
See also
lars_path(), the lasso path’s knots, exactly.solve(), a ridge on the same Gram.Choosing columns on the lasso path in docs/GRAM.md, the theory, and Choose columns on the lasso path, a recipe that runs.
Notes
The descent is the model’s own: the
lassomodel’s, run on the standardized (correlation-form) matrix, each penalty starting from the fit at the one before it:b_i = soft(rho_i, l * l1_ratio * pw_i) / (C_ii + l * (1 - l1_ratio) * pw_i) soft(v, t) = sign(v) * max(|v| - t, 0)
where
b_iis columni’s standardized coefficient,rho_iits standardized cross-correlation with the target less the other columns’ contributions,C_iiits standardized variance andpw_iits penalty weight. With an intercept the system is centered at the target’s own means, as the model’s descent reads it. Through the origin nothing is centered and the raw moments are used, as the model does.The stopping rule has no units.
sis the target’s spread, read from the Gram’s target moments: its standard deviation with an intercept, its root mean square through the origin. The standardized coefficients are in the target’s units, so the target scaled by a power of two, with the penalties, gives the same path scaled, to the bit, stopped on the same sweep.sis never below the largest standardized cross-moment|d_i|, which cannot exceed the target’s spread, so a Gram without target moments has a scale too. On 15 features at seven penalties,tol=1e-10cost the same as 1e-7 (0.07 ms) and landed 3e-10 from the converged path, where 1e-7 landed 4e-7.A descent on columns that nearly repeat each other takes many sweeps. A cyclic descent shrinks the error along two columns of correlation
rhobyrho**2a sweep. Their correlation matrix’s condition number iskappa = (1 + rho) / (1 - rho), sorho**2 = ((kappa - 1) / (kappa + 1))**2, about1 - 4 / kappa, and each e-fold of the error takes aboutkappa / 4sweeps.The descent runs in Rust. On 40 penalties at 15 strongly correlated columns it took 1.8 ms, where the same descent as a Python loop took 129 ms. Each sweep moves the coordinates in the loop’s order, so a row matches the loop’s to rounding (
2e-15there), not to the bit: the sum over the other columns runs in a different order.