23
23
_raise_for_params ,
24
24
get_routing_for_object ,
25
25
)
26
- from ..utils ._param_validation import Interval , StrOptions , validate_params
26
+ from ..utils ._param_validation import Hidden , Interval , StrOptions , validate_params
27
27
from ..utils .extmath import safe_sparse_dot
28
28
from ..utils .metadata_routing import (
29
29
_routing_enabled ,
@@ -1493,8 +1493,17 @@ class LinearModelCV(MultiOutputMixin, LinearModel, ABC):
1493
1493
1494
1494
_parameter_constraints : dict = {
1495
1495
"eps" : [Interval (Real , 0 , None , closed = "neither" )],
1496
- "n_alphas" : [Interval (Integral , 1 , None , closed = "left" )],
1497
- "alphas" : ["array-like" , None ],
1496
+ "n_alphas" : [
1497
+ Interval (Integral , 1 , None , closed = "left" ),
1498
+ Hidden (StrOptions ({"deprecated" })),
1499
+ ],
1500
+ # TODO(1.9): remove "warn" and None options.
1501
+ "alphas" : [
1502
+ Interval (Integral , 1 , None , closed = "left" ),
1503
+ "array-like" ,
1504
+ None ,
1505
+ Hidden (StrOptions ({"warn" })),
1506
+ ],
1498
1507
"fit_intercept" : ["boolean" ],
1499
1508
"precompute" : [StrOptions ({"auto" }), "array-like" , "boolean" ],
1500
1509
"max_iter" : [Interval (Integral , 1 , None , closed = "left" )],
@@ -1512,8 +1521,8 @@ class LinearModelCV(MultiOutputMixin, LinearModel, ABC):
1512
1521
def __init__ (
1513
1522
self ,
1514
1523
eps = 1e-3 ,
1515
- n_alphas = 100 ,
1516
- alphas = None ,
1524
+ n_alphas = "deprecated" ,
1525
+ alphas = "warn" ,
1517
1526
fit_intercept = True ,
1518
1527
precompute = "auto" ,
1519
1528
max_iter = 1000 ,
@@ -1595,6 +1604,40 @@ def fit(self, X, y, sample_weight=None, **params):
1595
1604
"""
1596
1605
_raise_for_params (params , self , "fit" )
1597
1606
1607
+ # TODO(1.9): remove n_alphas and alphas={"warn", None}; set alphas=100 by
1608
+ # default. Remove these deprecations messages and use self.alphas directly
1609
+ # instead of self._alphas.
1610
+ if self .n_alphas == "deprecated" :
1611
+ self ._alphas = 100
1612
+ else :
1613
+ warnings .warn (
1614
+ "'n_alphas' was deprecated in 1.7 and will be removed in 1.9. "
1615
+ "'alphas' now accepts an integer value which removes the need to pass "
1616
+ "'n_alphas'. The default value of 'alphas' will change from None to "
1617
+ "100 in 1.9. Pass an explicit value to 'alphas' and leave 'n_alphas' "
1618
+ "to its default value to silence this warning." ,
1619
+ FutureWarning ,
1620
+ )
1621
+ self ._alphas = self .n_alphas
1622
+
1623
+ if isinstance (self .alphas , str ) and self .alphas == "warn" :
1624
+ # - If self.n_alphas == "deprecated", both are left to their default values
1625
+ # so we don't warn since the future default behavior will be the same as
1626
+ # the current default behavior.
1627
+ # - If self.n_alphas != "deprecated", then we already warned about it
1628
+ # and the warning message mentions the future self.alphas default, so
1629
+ # no need to warn a second time.
1630
+ pass
1631
+ elif self .alphas is None :
1632
+ warnings .warn (
1633
+ "'alphas=None' is deprecated and will be removed in 1.9, at which "
1634
+ "point the default value will be set to 100. Set 'alphas=100' "
1635
+ "to silence this warning." ,
1636
+ FutureWarning ,
1637
+ )
1638
+ else :
1639
+ self ._alphas = self .alphas
1640
+
1598
1641
# This makes sure that there is no duplication in memory.
1599
1642
# Dealing right with copy_X is important in the following:
1600
1643
# Multiple functions touch X and subsamples of X and can induce a
@@ -1692,7 +1735,6 @@ def fit(self, X, y, sample_weight=None, **params):
1692
1735
path_params .pop ("cv" , None )
1693
1736
path_params .pop ("n_jobs" , None )
1694
1737
1695
- alphas = self .alphas
1696
1738
n_l1_ratio = len (l1_ratios )
1697
1739
1698
1740
check_scalar_alpha = partial (
@@ -1702,26 +1744,26 @@ def fit(self, X, y, sample_weight=None, **params):
1702
1744
include_boundaries = "left" ,
1703
1745
)
1704
1746
1705
- if alphas is None :
1747
+ if isinstance ( self . _alphas , Integral ) :
1706
1748
alphas = [
1707
1749
_alpha_grid (
1708
1750
X ,
1709
1751
y ,
1710
1752
l1_ratio = l1_ratio ,
1711
1753
fit_intercept = self .fit_intercept ,
1712
1754
eps = self .eps ,
1713
- n_alphas = self .n_alphas ,
1755
+ n_alphas = self ._alphas ,
1714
1756
copy_X = self .copy_X ,
1715
1757
sample_weight = sample_weight ,
1716
1758
)
1717
1759
for l1_ratio in l1_ratios
1718
1760
]
1719
1761
else :
1720
1762
# Making sure alphas entries are scalars.
1721
- for index , alpha in enumerate (alphas ):
1763
+ for index , alpha in enumerate (self . _alphas ):
1722
1764
check_scalar_alpha (alpha , f"alphas[{ index } ]" )
1723
1765
# Making sure alphas is properly ordered.
1724
- alphas = np .tile (np .sort (alphas )[::- 1 ], (n_l1_ratio , 1 ))
1766
+ alphas = np .tile (np .sort (self . _alphas )[::- 1 ], (n_l1_ratio , 1 ))
1725
1767
1726
1768
# We want n_alphas to be the number of alphas used for each l1_ratio.
1727
1769
n_alphas = len (alphas [0 ])
@@ -1807,7 +1849,7 @@ def fit(self, X, y, sample_weight=None, **params):
1807
1849
1808
1850
self .l1_ratio_ = best_l1_ratio
1809
1851
self .alpha_ = best_alpha
1810
- if self .alphas is None :
1852
+ if isinstance ( self ._alphas , Integral ) :
1811
1853
self .alphas_ = np .asarray (alphas )
1812
1854
if n_l1_ratio == 1 :
1813
1855
self .alphas_ = self .alphas_ [0 ]
@@ -1897,9 +1939,22 @@ class LassoCV(RegressorMixin, LinearModelCV):
1897
1939
n_alphas : int, default=100
1898
1940
Number of alphas along the regularization path.
1899
1941
1900
- alphas : array-like, default=None
1901
- List of alphas where to compute the models.
1902
- If ``None`` alphas are set automatically.
1942
+ .. deprecated:: 1.7
1943
+ `n_alphas` was deprecated in 1.7 and will be removed in 1.9. Use `alphas`
1944
+ instead.
1945
+
1946
+ alphas : array-like or int, default=None
1947
+ Values of alphas to test along the regularization path.
1948
+ If int, `alphas` values are generated automatically.
1949
+ If array-like, list of alpha values to use.
1950
+
1951
+ .. versionchanged:: 1.7
1952
+ `alphas` accepts an integer value which removes the need to pass
1953
+ `n_alphas`.
1954
+
1955
+ .. deprecated:: 1.7
1956
+ `alphas=None` was deprecated in 1.7 and will be removed in 1.9, at which
1957
+ point the default value will be set to 100.
1903
1958
1904
1959
fit_intercept : bool, default=True
1905
1960
Whether to calculate the intercept for this model. If set
@@ -2049,8 +2104,8 @@ def __init__(
2049
2104
self ,
2050
2105
* ,
2051
2106
eps = 1e-3 ,
2052
- n_alphas = 100 ,
2053
- alphas = None ,
2107
+ n_alphas = "deprecated" ,
2108
+ alphas = "warn" ,
2054
2109
fit_intercept = True ,
2055
2110
precompute = "auto" ,
2056
2111
max_iter = 1000 ,
@@ -2155,9 +2210,22 @@ class ElasticNetCV(RegressorMixin, LinearModelCV):
2155
2210
n_alphas : int, default=100
2156
2211
Number of alphas along the regularization path, used for each l1_ratio.
2157
2212
2158
- alphas : array-like, default=None
2159
- List of alphas where to compute the models.
2160
- If None alphas are set automatically.
2213
+ .. deprecated:: 1.7
2214
+ `n_alphas` was deprecated in 1.7 and will be removed in 1.9. Use `alphas`
2215
+ instead.
2216
+
2217
+ alphas : array-like or int, default=None
2218
+ Values of alphas to test along the regularization path, used for each l1_ratio.
2219
+ If int, `alphas` values are generated automatically.
2220
+ If array-like, list of alpha values to use.
2221
+
2222
+ .. versionchanged:: 1.7
2223
+ `alphas` accepts an integer value which removes the need to pass
2224
+ `n_alphas`.
2225
+
2226
+ .. deprecated:: 1.7
2227
+ `alphas=None` was deprecated in 1.7 and will be removed in 1.9, at which
2228
+ point the default value will be set to 100.
2161
2229
2162
2230
fit_intercept : bool, default=True
2163
2231
Whether to calculate the intercept for this model. If set
@@ -2326,8 +2394,8 @@ def __init__(
2326
2394
* ,
2327
2395
l1_ratio = 0.5 ,
2328
2396
eps = 1e-3 ,
2329
- n_alphas = 100 ,
2330
- alphas = None ,
2397
+ n_alphas = "deprecated" ,
2398
+ alphas = "warn" ,
2331
2399
fit_intercept = True ,
2332
2400
precompute = "auto" ,
2333
2401
max_iter = 1000 ,
@@ -2845,9 +2913,22 @@ class MultiTaskElasticNetCV(RegressorMixin, LinearModelCV):
2845
2913
n_alphas : int, default=100
2846
2914
Number of alphas along the regularization path.
2847
2915
2848
- alphas : array-like, default=None
2849
- List of alphas where to compute the models.
2850
- If not provided, set automatically.
2916
+ .. deprecated:: 1.7
2917
+ `n_alphas` was deprecated in 1.7 and will be removed in 1.9. Use `alphas`
2918
+ instead.
2919
+
2920
+ alphas : array-like or int, default=None
2921
+ Values of alphas to test along the regularization path, used for each l1_ratio.
2922
+ If int, `alphas` values are generated automatically.
2923
+ If array-like, list of alpha values to use.
2924
+
2925
+ .. versionchanged:: 1.7
2926
+ `alphas` accepts an integer value which removes the need to pass
2927
+ `n_alphas`.
2928
+
2929
+ .. deprecated:: 1.7
2930
+ `alphas=None` was deprecated in 1.7 and will be removed in 1.9, at which
2931
+ point the default value will be set to 100.
2851
2932
2852
2933
fit_intercept : bool, default=True
2853
2934
Whether to calculate the intercept for this model. If set
@@ -2991,8 +3072,8 @@ def __init__(
2991
3072
* ,
2992
3073
l1_ratio = 0.5 ,
2993
3074
eps = 1e-3 ,
2994
- n_alphas = 100 ,
2995
- alphas = None ,
3075
+ n_alphas = "deprecated" ,
3076
+ alphas = "warn" ,
2996
3077
fit_intercept = True ,
2997
3078
max_iter = 1000 ,
2998
3079
tol = 1e-4 ,
@@ -3088,9 +3169,22 @@ class MultiTaskLassoCV(RegressorMixin, LinearModelCV):
3088
3169
n_alphas : int, default=100
3089
3170
Number of alphas along the regularization path.
3090
3171
3091
- alphas : array-like, default=None
3092
- List of alphas where to compute the models.
3093
- If not provided, set automatically.
3172
+ .. deprecated:: 1.7
3173
+ `n_alphas` was deprecated in 1.7 and will be removed in 1.9. Use `alphas`
3174
+ instead.
3175
+
3176
+ alphas : array-like or int, default=None
3177
+ Values of alphas to test along the regularization path.
3178
+ If int, `alphas` values are generated automatically.
3179
+ If array-like, list of alpha values to use.
3180
+
3181
+ .. versionchanged:: 1.7
3182
+ `alphas` accepts an integer value which removes the need to pass
3183
+ `n_alphas`.
3184
+
3185
+ .. deprecated:: 1.7
3186
+ `alphas=None` was deprecated in 1.7 and will be removed in 1.9, at which
3187
+ point the default value will be set to 100.
3094
3188
3095
3189
fit_intercept : bool, default=True
3096
3190
Whether to calculate the intercept for this model. If set
@@ -3230,8 +3324,8 @@ def __init__(
3230
3324
self ,
3231
3325
* ,
3232
3326
eps = 1e-3 ,
3233
- n_alphas = 100 ,
3234
- alphas = None ,
3327
+ n_alphas = "deprecated" ,
3328
+ alphas = "warn" ,
3235
3329
fit_intercept = True ,
3236
3330
max_iter = 1000 ,
3237
3331
tol = 1e-4 ,
0 commit comments