1
$\begingroup$

The results alpha path from enet_path and the lambda path from glmnet are the same. Yet the coefficients are different. Below are sample code and output to illustrate the difference.

Curiously, when I set l1_ratio=1 in enet_path and alpha=1 in glmnet (they are the same parameter with different names), then the coefficients are the same.

What are the causes here and how can I make the coefficients from enet_path the same as those produced by glmnet?

(I should mention that the enet_path example must be run before the glmnet example since glmnet modifies x in place.)

enet_path example

from sklearn.datasets import make_regression
x, y = make_regression(n_features=2, n_samples=49, random_state=0)

from sklearn.linear_model import enet_path
alphas, coefs, _ = enet_path(x, y, l1_ratio=0.95, eps=1e-4)
alphas

array([1.06621108e+02, 9.71491830e+01, 8.85187174e+01, 8.06549585e+01,
        7.34897943e+01, 6.69611635e+01, 6.10125183e+01, 5.55923343e+01,
        5.06536646e+01, 4.61537326e+01, 4.20535622e+01, 3.83176396e+01,
        3.49136061e+01, 3.18119776e+01, 2.89858892e+01, 2.64108627e+01,
        2.40645944e+01, 2.19267622e+01, 1.99788491e+01, 1.82039832e+01,
        1.65867915e+01, 1.51132666e+01, 1.37706457e+01, 1.25472995e+01,
        1.14326320e+01, 1.04169885e+01, 9.49157192e+00, 8.64836683e+00,
        7.88006975e+00, 7.18002608e+00, 6.54217235e+00, 5.96098379e+00,
        5.43142642e+00, 4.94891346e+00, 4.50926563e+00, 4.10867490e+00,
        3.74367155e+00, 3.41109408e+00, 3.10806189e+00, 2.83195024e+00,
        2.58036758e+00, 2.35113484e+00, 2.14226650e+00, 1.95195345e+00,
        1.77854728e+00, 1.62054604e+00, 1.47658120e+00, 1.34540580e+00,
        1.22588365e+00, 1.11697953e+00, 1.01775015e+00, 9.27336027e-01,
        8.44954051e-01, 7.69890661e-01, 7.01495697e-01, 6.39176753e-01,
        5.82394052e-01, 5.30655769e-01, 4.83513773e-01, 4.40559742e-01,
coefs

array([[ 0.        ,  0.        ],
       [ 1.51505283,  0.        ],
       [ 3.12237082,  0.        ],
       [ 4.82305039,  0.        ],
       [ 6.61746562,  0.        ],
       [ 8.50517882,  0.        ],
       [10.49138699,  0.81136012],
       [12.57360077,  2.25212953],
       [14.74449868,  3.76023131],
       [16.99990135,  5.33350497],
       [19.33454251,  6.96901003],
       [21.74207426,  8.66300824],
       [24.21510026, 10.4109642 ],
       [26.74523854, 12.20756667],
...

glmnet example

import glmnet_python
from glmnet import glmnet
glm_model = glmnet(x=x, y=y, alpha=0.95, standardize=False, intr=False)
glm_model['lambdau'].T

array([106.62110845,  97.14918304,  88.51871737,  80.65495849,
        73.48979427,  66.96116349,  61.01251828,  55.59233432,
        50.65366455,  46.1537326 ,  42.0535622 ,  38.31763964,
        34.91360614,  31.8119776 ,  28.98588919,  26.41086268,
        24.06459443,  21.92676219,  19.97884907,  18.20398318,
        16.58679149,  15.11326665,  13.77064569,  12.54729948,
        11.43263198,  10.41698846,   9.49157192,   8.64836683,
         7.88006975,   7.18002608,   6.54217235,   5.96098379])
glm_model['beta'].T

array([[ 0.        ,  0.        ],
       [ 8.00878345,  0.        ],
       [15.3567615 ,  0.        ],
       [22.09445523,  0.        ],
       [28.26918861,  0.        ],
       [33.92517603,  0.        ],
       [39.22817905,  4.15014015],
       [44.12477964,  9.37337228],
       [48.60501583, 14.15402598],
       [52.70280532, 18.52789372],
       [56.44955191, 22.52815944],
...

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.