Skip to content

RidgeCV doesn't allow alpha=0 #23074

@Micky774

Description

@Micky774

Describe the bug

RidgeCV doesn't allow any alphas to be 0, despite the underlying Ridge linear model allowing such behavior.

Steps/Code to Reproduce

from sklearn.datasets import load_diabetes
from sklearn.linear_model import RidgeCV
X, y = load_diabetes(return_X_y=True)
clf = RidgeCV(alphas=[0, 1e-2, 1e-1, 1]).fit(X, y)

Expected Results

No error thrown

Actual Results

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_18064/1693855650.py in <module>
      2 from sklearn.linear_model import RidgeCV
      3 X, y = load_diabetes(return_X_y=True)
----> 4 clf = RidgeCV(alphas=[0, 1e-2, 1e-1, 1]).fit(X, y)
      5 clf.score(X, y)

r:\work\scikit-learn\sklearn\linear_model\_ridge.py in fit(self, X, y, sample_weight)
   2147             if n_alphas != 1:
   2148                 for index, alpha in enumerate(self.alphas):
-> 2149                     alpha = check_scalar_alpha(alpha, f"alphas[{index}]")
   2150             else:
   2151                 self.alphas[0] = check_scalar_alpha(self.alphas[0], "alphas")

r:\work\scikit-learn\sklearn\utils\validation.py in check_scalar(x, name, target_type, min_val, max_val, include_boundaries)
   1466     )
   1467     if min_val is not None and comparison_operator(x, min_val):
-> 1468         raise ValueError(
   1469             f"{name} == {x}, must be"
   1470             f" {'>=' if include_boundaries in ('left', 'both') else '>'} {min_val}."

ValueError: alphas[0] == 0, must be > 0.0.

Versions

System:
    python: 3.9.7 (default, Sep 16 2021, 16:59:28) [MSC v.1916 64 bit (AMD64)]
executable: R:\ProgramFiles\anaconda3\envs\scikit-dev\python.exe
   machine: Windows-10-10.0.19043-SP0

Python dependencies:
      sklearn: 1.1.dev0
          pip: 21.2.4
   setuptools: 58.0.4
        numpy: 1.21.5
        scipy: 1.7.3
       Cython: 0.29.26
       pandas: 1.3.5
   matplotlib: 3.5.1
       joblib: 1.1.0
threadpoolctl: 3.0.0

Built with OpenMP: True

threadpoolctl info:
       user_api: blas
   internal_api: openblas
         prefix: libopenblas
       filepath: R:\ProgramFiles\anaconda3\envs\scikit-dev\Lib\site-packages\numpy\.libs\libopenblas.XWYDX2IKJW2NMTWSFYNGFUWKQU3LYTCZ.gfortran-win_amd64.dll
        version: 0.3.17
threading_layer: pthreads
   architecture: Zen
    num_threads: 12

       user_api: openmp
   internal_api: openmp
         prefix: vcomp
       filepath: C:\Windows\System32\vcomp140.dll
        version: None
    num_threads: 12

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions