import numpy as np
from pprint import pprint
from crabbymetrics import ElasticNet
np.set_printoptions(precision=4, suppress=True)ElasticNet Example
Fits center both predictors and the response for an unpenalized intercept, then return the intercept in the original predictor coordinates. Predictions are therefore invariant to adding constants to predictor columns. This corrects the older outcome-only centering behavior. Pure L2 (l1_ratio=0) and zero-penalty fits use a closed-form QR solution; other fits retain duality-gap checks.
1 Fit A Regularized Linear Model
rng = np.random.default_rng(1)
n = 600
k = 6
beta = np.array([2.0, -1.5, 0.0, 0.0, 0.8, -0.3])
intercept = -0.4
x = rng.normal(size=(n, k))
y = intercept + x @ beta + rng.normal(scale=0.7, size=n)
model = ElasticNet(penalty=0.1, l1_ratio=0.5)
model.fit(x, y)
print("true intercept:", intercept)
print("true coef:", beta)
pprint(model.summary())true intercept: -0.4
true coef: [ 2. -1.5 0. 0. 0.8 -0.3]
{'coef': array([ 1.8487, -1.3858, 0. , 0. , 0.6956, -0.2889]),
'coef_se': None,
'converged': True,
'duality_gap': 9.242381770491193e-05,
'duality_gap_tolerance': 0.4427918256609508,
'inference_available': False,
'intercept': -0.3917545657840523,
'intercept_se': None,
'iterations': 4,
'l1_ratio': 0.5,
'objective': 0.6281914787348275,
'penalty': 0.1,
'termination_reason': 'Duality gap tolerance reached'}
2 Predictions and feature shifts
The intercept absorbs shifts in predictor origins. This check uses the same penalty and verifies predictions in the corresponding coordinates.
shift = np.array([10.0, -4.0, 7.0, 0.5, -2.0, 3.0])
shifted = ElasticNet(penalty=0.1, l1_ratio=0.5)
shifted.fit(x + shift, y)
np.testing.assert_allclose(model.predict(x), shifted.predict(x + shift), atol=1e-7)
print("Predictions:", model.predict(x[:5]))
l2 = ElasticNet(penalty=0.1, l1_ratio=0.0)
l2.fit(x, y)
print("Pure-L2 coefficients:", l2.summary()["coef"])Predictions: [-0.3906 -2.3279 -1.4154 -0.8715 -2.7003]
Pure-L2 coefficients: [ 1.8114 -1.3647 0.017 0.0319 0.7053 -0.3211]
Feature centering does not standardize feature scales. Rescaling a column changes the effective penalty; use a training-fitted scaling transformation when coefficients should be penalized on comparable scales.