5
5
# Authors: V. Michel, F. Pedregosa, A. Gramfort
6
6
# License: BSD 3 clause
7
7
8
+ import warnings
8
9
from math import log
9
10
from numbers import Integral , Real
10
11
import numpy as np
15
16
from ..utils .extmath import fast_logdet
16
17
from scipy .linalg import pinvh
17
18
from ..utils .validation import _check_sample_weight
18
- from ..utils ._param_validation import Interval
19
+ from ..utils ._param_validation import Interval , Hidden , StrOptions
20
+
21
+
22
+ # TODO(1.5) Remove
23
+ def _deprecate_n_iter (n_iter , max_iter ):
24
+ """Deprecates n_iter in favour of max_iter. Checks if the n_iter has been
25
+ used instead of max_iter and generates a deprecation warning if True.
26
+
27
+ Parameters
28
+ ----------
29
+ n_iter : int,
30
+ Value of n_iter attribute passed by the estimator.
31
+
32
+ max_iter : int, default=None
33
+ Value of max_iter attribute passed by the estimator.
34
+ If `None`, it corresponds to `max_iter=300`.
35
+
36
+ Returns
37
+ -------
38
+ max_iter : int,
39
+ Value of max_iter which shall further be used by the estimator.
40
+
41
+ Notes
42
+ -----
43
+ This function should be completely removed in 1.5.
44
+ """
45
+ if n_iter != "deprecated" :
46
+ if max_iter is not None :
47
+ raise ValueError (
48
+ "Both `n_iter` and `max_iter` attributes were set. Attribute"
49
+ " `n_iter` was deprecated in version 1.3 and will be removed in"
50
+ " 1.5. To avoid this error, only set the `max_iter` attribute."
51
+ )
52
+ warnings .warn (
53
+ "'n_iter' was renamed to 'max_iter' in version 1.3 and "
54
+ "will be removed in 1.5" ,
55
+ FutureWarning ,
56
+ )
57
+ max_iter = n_iter
58
+ elif max_iter is None :
59
+ max_iter = 300
60
+ return max_iter
61
+
19
62
20
63
###############################################################################
21
64
# BayesianRidge regression
@@ -32,8 +75,12 @@ class BayesianRidge(RegressorMixin, LinearModel):
32
75
33
76
Parameters
34
77
----------
35
- n_iter : int, default=300
36
- Maximum number of iterations. Should be greater than or equal to 1.
78
+ max_iter : int, default=None
79
+ Maximum number of iterations over the complete dataset before
80
+ stopping independently of any early stopping criterion. If `None`, it
81
+ corresponds to `max_iter=300`.
82
+
83
+ .. versionchanged:: 1.3
37
84
38
85
tol : float, default=1e-3
39
86
Stop the algorithm if w has converged.
@@ -83,14 +130,21 @@ class BayesianRidge(RegressorMixin, LinearModel):
83
130
verbose : bool, default=False
84
131
Verbose mode when fitting the model.
85
132
133
+ n_iter : int
134
+ Maximum number of iterations. Should be greater than or equal to 1.
135
+
136
+ .. deprecated:: 1.3
137
+ `n_iter` is deprecated in 1.3 and will be removed in 1.5. Use
138
+ `max_iter` instead.
139
+
86
140
Attributes
87
141
----------
88
142
coef_ : array-like of shape (n_features,)
89
143
Coefficients of the regression model (mean of distribution)
90
144
91
145
intercept_ : float
92
146
Independent term in decision function. Set to 0.0 if
93
- `` fit_intercept = False` `.
147
+ `fit_intercept = False`.
94
148
95
149
alpha_ : float
96
150
Estimated precision of the noise.
@@ -162,7 +216,7 @@ class BayesianRidge(RegressorMixin, LinearModel):
162
216
"""
163
217
164
218
_parameter_constraints : dict = {
165
- "n_iter " : [Interval (Integral , 1 , None , closed = "left" )],
219
+ "max_iter " : [Interval (Integral , 1 , None , closed = "left" ), None ],
166
220
"tol" : [Interval (Real , 0 , None , closed = "neither" )],
167
221
"alpha_1" : [Interval (Real , 0 , None , closed = "left" )],
168
222
"alpha_2" : [Interval (Real , 0 , None , closed = "left" )],
@@ -174,12 +228,16 @@ class BayesianRidge(RegressorMixin, LinearModel):
174
228
"fit_intercept" : ["boolean" ],
175
229
"copy_X" : ["boolean" ],
176
230
"verbose" : ["verbose" ],
231
+ "n_iter" : [
232
+ Interval (Integral , 1 , None , closed = "left" ),
233
+ Hidden (StrOptions ({"deprecated" })),
234
+ ],
177
235
}
178
236
179
237
def __init__ (
180
238
self ,
181
239
* ,
182
- n_iter = 300 ,
240
+ max_iter = None , # TODO(1.5): Set to 300
183
241
tol = 1.0e-3 ,
184
242
alpha_1 = 1.0e-6 ,
185
243
alpha_2 = 1.0e-6 ,
@@ -191,8 +249,9 @@ def __init__(
191
249
fit_intercept = True ,
192
250
copy_X = True ,
193
251
verbose = False ,
252
+ n_iter = "deprecated" , # TODO(1.5): Remove
194
253
):
195
- self .n_iter = n_iter
254
+ self .max_iter = max_iter
196
255
self .tol = tol
197
256
self .alpha_1 = alpha_1
198
257
self .alpha_2 = alpha_2
@@ -204,6 +263,7 @@ def __init__(
204
263
self .fit_intercept = fit_intercept
205
264
self .copy_X = copy_X
206
265
self .verbose = verbose
266
+ self .n_iter = n_iter
207
267
208
268
def fit (self , X , y , sample_weight = None ):
209
269
"""Fit the model.
@@ -228,6 +288,8 @@ def fit(self, X, y, sample_weight=None):
228
288
"""
229
289
self ._validate_params ()
230
290
291
+ max_iter = _deprecate_n_iter (self .n_iter , self .max_iter )
292
+
231
293
X , y = self ._validate_data (X , y , dtype = [np .float64 , np .float32 ], y_numeric = True )
232
294
233
295
if sample_weight is not None :
@@ -274,7 +336,7 @@ def fit(self, X, y, sample_weight=None):
274
336
eigen_vals_ = S ** 2
275
337
276
338
# Convergence loop of the bayesian ridge regression
277
- for iter_ in range (self . n_iter ):
339
+ for iter_ in range (max_iter ):
278
340
279
341
# update posterior mean coef_ based on alpha_ and lambda_ and
280
342
# compute corresponding rmse
@@ -430,8 +492,10 @@ class ARDRegression(RegressorMixin, LinearModel):
430
492
431
493
Parameters
432
494
----------
433
- n_iter : int, default=300
434
- Maximum number of iterations.
495
+ max_iter : int, default=None
496
+ Maximum number of iterations. If `None`, it corresponds to `max_iter=300`.
497
+
498
+ .. versionchanged:: 1.3
435
499
436
500
tol : float, default=1e-3
437
501
Stop the algorithm if w has converged.
@@ -470,6 +534,13 @@ class ARDRegression(RegressorMixin, LinearModel):
470
534
verbose : bool, default=False
471
535
Verbose mode when fitting the model.
472
536
537
+ n_iter : int
538
+ Maximum number of iterations.
539
+
540
+ .. deprecated:: 1.3
541
+ `n_iter` is deprecated in 1.3 and will be removed in 1.5. Use
542
+ `max_iter` instead.
543
+
473
544
Attributes
474
545
----------
475
546
coef_ : array-like of shape (n_features,)
@@ -487,6 +558,11 @@ class ARDRegression(RegressorMixin, LinearModel):
487
558
scores_ : float
488
559
if computed, value of the objective function (to be maximized)
489
560
561
+ n_iter_ : int
562
+ The actual number of iterations to reach the stopping criterion.
563
+
564
+ .. versionadded:: 1.3
565
+
490
566
intercept_ : float
491
567
Independent term in decision function. Set to 0.0 if
492
568
``fit_intercept = False``.
@@ -542,7 +618,7 @@ class ARDRegression(RegressorMixin, LinearModel):
542
618
"""
543
619
544
620
_parameter_constraints : dict = {
545
- "n_iter " : [Interval (Integral , 1 , None , closed = "left" )],
621
+ "max_iter " : [Interval (Integral , 1 , None , closed = "left" ), None ],
546
622
"tol" : [Interval (Real , 0 , None , closed = "left" )],
547
623
"alpha_1" : [Interval (Real , 0 , None , closed = "left" )],
548
624
"alpha_2" : [Interval (Real , 0 , None , closed = "left" )],
@@ -553,12 +629,16 @@ class ARDRegression(RegressorMixin, LinearModel):
553
629
"fit_intercept" : ["boolean" ],
554
630
"copy_X" : ["boolean" ],
555
631
"verbose" : ["verbose" ],
632
+ "n_iter" : [
633
+ Interval (Integral , 1 , None , closed = "left" ),
634
+ Hidden (StrOptions ({"deprecated" })),
635
+ ],
556
636
}
557
637
558
638
def __init__ (
559
639
self ,
560
640
* ,
561
- n_iter = 300 ,
641
+ max_iter = None , # TODO(1.5): Set to 300
562
642
tol = 1.0e-3 ,
563
643
alpha_1 = 1.0e-6 ,
564
644
alpha_2 = 1.0e-6 ,
@@ -569,8 +649,9 @@ def __init__(
569
649
fit_intercept = True ,
570
650
copy_X = True ,
571
651
verbose = False ,
652
+ n_iter = "deprecated" , # TODO(1.5): Remove
572
653
):
573
- self .n_iter = n_iter
654
+ self .max_iter = max_iter
574
655
self .tol = tol
575
656
self .fit_intercept = fit_intercept
576
657
self .alpha_1 = alpha_1
@@ -581,6 +662,7 @@ def __init__(
581
662
self .threshold_lambda = threshold_lambda
582
663
self .copy_X = copy_X
583
664
self .verbose = verbose
665
+ self .n_iter = n_iter
584
666
585
667
def fit (self , X , y ):
586
668
"""Fit the model according to the given training data and parameters.
@@ -603,6 +685,8 @@ def fit(self, X, y):
603
685
604
686
self ._validate_params ()
605
687
688
+ max_iter = _deprecate_n_iter (self .n_iter , self .max_iter )
689
+
606
690
X , y = self ._validate_data (
607
691
X , y , dtype = [np .float64 , np .float32 ], y_numeric = True , ensure_min_samples = 2
608
692
)
@@ -648,7 +732,7 @@ def update_coeff(X, y, coef_, alpha_, keep_lambda, sigma_):
648
732
else self ._update_sigma_woodbury
649
733
)
650
734
# Iterative procedure of ARDRegression
651
- for iter_ in range (self . n_iter ):
735
+ for iter_ in range (max_iter ):
652
736
sigma_ = update_sigma (X , alpha_ , lambda_ , keep_lambda )
653
737
coef_ = update_coeff (X , y , coef_ , alpha_ , keep_lambda , sigma_ )
654
738
@@ -688,6 +772,8 @@ def update_coeff(X, y, coef_, alpha_, keep_lambda, sigma_):
688
772
if not keep_lambda .any ():
689
773
break
690
774
775
+ self .n_iter_ = iter_ + 1
776
+
691
777
if keep_lambda .any ():
692
778
# update sigma and mu using updated params from the last iteration
693
779
sigma_ = update_sigma (X , alpha_ , lambda_ , keep_lambda )
0 commit comments