Skip to main content

models.ElasticNet

An elastic-net GLM: the lasso (alpha=1), ridge (alpha=0) and

Usage

Source

models.ElasticNet()

everything between, minimizing glmnet’s objective sum(w * d) / (2 * sum(w)) + lam * sum(pf * ((1 - alpha) / 2 * b**2 + alpha * |b|)) over coefficients b of standardized columns. The design’s first all-ones column is the unpenalized intercept; coefficients are reported on the design’s scale.

Parameters

family: str

As Glm.

link: str

As Glm: the log link for the gamma, inverse Gaussian and Tweedie, the canonical link otherwise, by default.

alpha: float = 1.0

Mixing between ridge (0) and the lasso (1).

lam: float = 0.0

Penalty strength (lambda in glmnet).

standardize: bool = True

Penalize the coefficients of columns scaled to unit standard deviation.

penalty_factor: list of float

One factor per design column (the intercept’s is ignored); 0 leaves a column unpenalized.

theta: float
power: float
link_power: float

Examples

>>> from prospicio.models import Design, ElasticNet
>>> x = [float(i) for i in range(6)]
>>> d = Design([[1.0] * 6, x, [1.0, 0.0] * 3], ["(Intercept)", "x1", "x2"])
>>> y = [1.0, 3.1, 4.9, 7.2, 9.0, 10.8]
>>> net = ElasticNet("gaussian", alpha=1.0)
>>> top = net.lambda_max(d, y)
>>> net.with_lam(1.01 * top).fit(d, y).coefficients[1:]

[0.0, 0.0]

Attributes

Name Description
alpha Mixing parameter.
lam Penalty strength.
lower_limits Lower bounds on the coefficients, one per design column, or
upper_limits Upper bounds on the coefficients, one per design column, or

alpha

Mixing parameter.

alpha: float


lam

Penalty strength.

lam: float


lower_limits

Lower bounds on the coefficients, one per design column, or

lower_limits: list[float] | None

None.


upper_limits

Upper bounds on the coefficients, one per design column, or

upper_limits: list[float] | None

None.

Methods

Name Description
cross_validate() Cross-validates the path, like glmnet’s cv.glmnet: on each
cross_validate_relaxed() Cross-validates relaxed fits, like glmnet’s cv.glmnet(relax =</td> </tr> <tr> <td><a href="#fit" class="doc-function doc-label doc-label-method">fit()</a></td> <td>Fits atlam.</td> </tr> <tr> <td><a href="#lambda_max" class="doc-function doc-label doc-label-method">lambda_max()</a></td> <td>The smallestlamat which every penalized coefficient is zero.</td> </tr> <tr> <td><a href="#lambda_path" class="doc-function doc-label doc-label-method">lambda_path()</a></td> <td>npenalty strengths, log-spaced fromlambda_maxdown to</td> </tr> <tr> <td><a href="#path" class="doc-function doc-label doc-label-method">path()</a></td> <td>Fits at each oflams`` in turn, each from the previous solution.
with_lam() The same spec at another penalty strength.

cross_validate()

Cross-validates the path, like glmnet’s cv.glmnet: on each

Usage

Source

cross_validate(design, y, lams, splits)

split, fits lams (warm starts) to the training rows and scores the mean deviance on the test rows. Folds run in parallel.

Parameters
design: Design
y: list of float
lams: list of float

Penalty strengths, largest first (from lambda_path).

splits: list of (list of int, list of int)
(train, test) rows, as k_fold returns.
Returns
CvPath

cross_validate_relaxed()

Cross-validates relaxed fits, like glmnet’s ``cv.glmnet(relax =

Usage

Source

cross_validate_relaxed(design, y, lams, gammas, splits)

TRUE): on each split, every fit along the path is relaxed at each ofgammas(ElasticNetFit.relax) and scored on the test rows. The lowest mean deviance over every(lam, gamma)islam_min,gamma_min; the largestlam, thengamma, within one standard error of it islam_1se,gamma_1se``.

Parameters
design: Design
y: list of float
lams: list of float

Penalty strengths, largest first (from lambda_path).

gammas: list of float

Relaxations in [0, 1]; 1 is the penalized fit.

splits: list of (list of int, list of int)
(train, test) rows, as k_fold returns.
Returns
RelaxedCv

fit()

Fits at lam.

Usage

Source

fit(design, y)
Parameters
design: Design
y: list of float
Returns
ElasticNetFit
Raises
ValueError
If a parameter is out of range, a response is outside the family’s range, or the fit does not converge.

lambda_max()

The smallest lam at which every penalized coefficient is zero.

Usage

Source

lambda_max(design, y)
Parameters
design: Design
y: list of float
Returns
float

lambda_path()

n penalty strengths, log-spaced from lambda_max down to

Usage

Source

lambda_path(design, y, n=100, min_ratio=0.0001)

min_ratio times it.

Parameters
design: Design
y: list of float
n: int = 100
min_ratio: float = 1e-4
Returns
list of float

path()

Fits at each of lams in turn, each from the previous solution.

Usage

Source

path(design, y, lams)
Parameters
design: Design
y: list of float
lams: list of float
Returns
list of ElasticNetFit

with_lam()

The same spec at another penalty strength.

Usage

Source

with_lam(lam)
Parameters
lam: float
Returns
ElasticNet