Skip to content

_alpha_grid divide by zero when l1_ratio=0 (ElasticNetCV) #7551

@erikcs

Description

@erikcs

Description

According to the docs ElasticNetCV can fit a Ridge penalty by setting l1_ratio=0

This does not work because the grid automatically generated by _alpha_grid sets alpha_max to np.inf in case l1_ratio=0

Steps/Code to Reproduce

from sklearn.linear_model import ElasticNetCV

X = np.array([[1, 2, 4, 5, 8], [3, 5, 7, 7, 8]]).T
y = np.array([12, 10, 11, 21, 5])

est = ElasticNetCV(l1_ratio=0).fit(X, y)

Gives the runtime error
UnboundLocalError: local variable 'best_l1_ratio' referenced before assignment

because

from sklearn.linear_model.coordinate_descent import _alpha_grid 
_alpha_grid(X, y, l1_ratio=0)

returns

array([ inf,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,
        nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,
        nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,
        nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,
        nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,
        nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,
        nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,
        nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,
        nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,
        nan])

glmnet takes care of this by

For alpha=0 (ridge) lambda.max would be ∞∞; hence for this case we pick a value corresponding to a small value for alpha close to zero.)

I could submit a PR where I implement the corresponding logic?

Versions

Darwin-16.0.0-x86_64-i386-64bit
('Python', '2.7.12 |Anaconda custom (x86_64)| (default, Jul 2 2016, 17:43:17) \n[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)]')
('NumPy', '1.11.1')
('SciPy', '0.18.0')
('Scikit-Learn', '0.18')

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions