models.ElasticNet
An elastic-net GLM: the lasso (alpha=1), ridge (alpha=0) and
Usage
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 canonical link by default.
alpha: float = 1.0-
Mixing between ridge (0) and the lasso (1).
lam: float = 0.0-
Penalty strength (
lambdain 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: floatpower: floatlink_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. |
alpha
Mixing parameter.
alpha: float
lam
Penalty strength.
lam: float
Methods
| Name | Description |
|---|---|
| cross_validate() |
Cross-validates the path, like glmnet’s cv.glmnet: on each
|
| fit() | Fits at lam. |
| lambda_max() | The smallest lam at which every penalized coefficient is zero. |
| lambda_path() | n penalty strengths, log-spaced from lambda_max down to |
| path() | Fits at each of lams 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
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: Designy: list of floatlams: 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
fit()
Fits at lam.
Usage
fit(design, y)Parameters
design: Designy: 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
lambda_max(design, y)Parameters
design: Designy: list of float
Returns
float
lambda_path()
n penalty strengths, log-spaced from lambda_max down to
Usage
lambda_path(design, y, n=100, min_ratio=0.0001)min_ratio times it.
Parameters
design: Designy: list of floatn: int = 100min_ratio: float = 1e-4
Returns
list of float
path()
Fits at each of lams in turn, each from the previous solution.
Usage
path(design, y, lams)Parameters
design: Designy: list of floatlams: list of float
Returns
list of ElasticNetFit
with_lam()
The same spec at another penalty strength.
Usage
with_lam(lam)Parameters
lam: float
Returns
ElasticNet