from _api_doc_utils import *ElasticNet
Coordinate-descent elastic net regression
1 Where it fits
Group: Regression
ElasticNet estimates a penalized linear model with a convex combination of L1 and L2 penalties:
\[ \min_{\alpha,\beta}\; \frac{1}{2n}\sum_i (y_i-\alpha-x_i'\beta)^2 + \lambda\left(\rho\|\beta\|_1 + \frac{1-\rho}{2}\|\beta\|_2^2\right). \]
It is useful when the goal is prediction or sparse regularized coefficients rather than classical inference.
2 Python API
Constructor: cm.ElasticNet
Use ElasticNet(penalty, l1_ratio, tolerance, max_iterations), then fit(x, y), predict(x), summary(), and optionally bootstrap(B, seed=None). The summary reports point estimates and approximate HC-style standard errors.
print(inspect.signature(cm.ElasticNet))(penalty=1.0, l1_ratio=0.5, tolerance=0.0001, max_iterations=1000)
cls = cm.ElasticNet
display(HTML(html_table(["Public method"], public_methods(cls))))| Public method |
|---|
bootstrap(self, /, n_bootstrap, seed=None) |
fit(self, /, x, y) |
predict(self, /, x) |
summary(self, /) |
3 Minimal example
rng = np.random.default_rng(4)
x = rng.normal(size=(180, 8))
y = 0.4 + x[:, :3] @ np.array([1.0, -0.8, 0.5]) + rng.normal(scale=0.5, size=180)
model = cm.ElasticNet(penalty=0.05, l1_ratio=0.7)
model.fit(x, y)
print(model.summary()["coef"])
print(model.predict(x[:3]))[ 0.90319681 -0.79760879 0.45662966 -0.01442286 -0.01857245 -0.
0. -0. ]
[ 0.6312702 -1.2665473 -2.43483936]
4 summary() contract
The table below is generated by fitting the live class in this repository and then inspecting summary(). Shapes are shown because most values are plain NumPy arrays or scalars.
rng=np.random.default_rng(104); x=rng.normal(size=(90,5)); y=.4+x[:,:2]@np.array([1,-.8])+rng.normal(size=90)*.3
model=cm.ElasticNet(penalty=.05,l1_ratio=.7); model.fit(x,y)
summary = model.summary()
display(HTML(html_table(["summary() key", "shape"], summary_shape_rows(summary))))| summary() key | shape |
|---|---|
intercept |
() |
coef |
(5,) |
intercept_se |
() |
coef_se |
(5,) |