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
+
19
21
20
22
###############################################################################
21
23
# BayesianRidge regression
@@ -32,8 +34,11 @@ class BayesianRidge(RegressorMixin, LinearModel):
32
34
33
35
Parameters
34
36
----------
35
- n_iter : int, default=300
36
- Maximum number of iterations. Should be greater than or equal to 1.
37
+ max_iter : int, default=300
38
+ Maximum number of iterations over the complete dataset before
39
+ stopping independently of any early stopping criterion.
40
+
41
+ .. versionchanged:: 1.3
37
42
38
43
tol : float, default=1e-3
39
44
Stop the algorithm if w has converged.
@@ -83,14 +88,21 @@ class BayesianRidge(RegressorMixin, LinearModel):
83
88
verbose : bool, default=False
84
89
Verbose mode when fitting the model.
85
90
91
+ n_iter : int
92
+ Maximum number of iterations. Should be greater than or equal to 1.
93
+
94
+ .. deprecated:: 1.3
95
+ `n_iter` is deprecated in 1.3 and will be removed in 1.5. Use
96
+ `max_iter` instead.
97
+
86
98
Attributes
87
99
----------
88
100
coef_ : array-like of shape (n_features,)
89
101
Coefficients of the regression model (mean of distribution)
90
102
91
103
intercept_ : float
92
104
Independent term in decision function. Set to 0.0 if
93
- `` fit_intercept = False` `.
105
+ `fit_intercept = False`.
94
106
95
107
alpha_ : float
96
108
Estimated precision of the noise.
@@ -162,7 +174,7 @@ class BayesianRidge(RegressorMixin, LinearModel):
162
174
"""
163
175
164
176
_parameter_constraints : dict = {
165
- "n_iter " : [Interval (Integral , 1 , None , closed = "left" )],
177
+ "max_iter " : [Interval (Integral , 1 , None , closed = "left" ), None ],
166
178
"tol" : [Interval (Real , 0 , None , closed = "neither" )],
167
179
"alpha_1" : [Interval (Real , 0 , None , closed = "left" )],
168
180
"alpha_2" : [Interval (Real , 0 , None , closed = "left" )],
@@ -174,12 +186,16 @@ class BayesianRidge(RegressorMixin, LinearModel):
174
186
"fit_intercept" : ["boolean" ],
175
187
"copy_X" : ["boolean" ],
176
188
"verbose" : ["verbose" ],
189
+ "n_iter" : [
190
+ Interval (Integral , 1 , None , closed = "left" ),
191
+ Hidden (StrOptions ({"deprecated" })),
192
+ ],
177
193
}
178
194
179
195
def __init__ (
180
196
self ,
181
197
* ,
182
- n_iter = 300 ,
198
+ max_iter = None , # TODO(1.5): Set to 300
183
199
tol = 1.0e-3 ,
184
200
alpha_1 = 1.0e-6 ,
185
201
alpha_2 = 1.0e-6 ,
@@ -191,8 +207,9 @@ def __init__(
191
207
fit_intercept = True ,
192
208
copy_X = True ,
193
209
verbose = False ,
210
+ n_iter = "deprecated" , # TODO(1.5): Remove
194
211
):
195
- self .n_iter = n_iter
212
+ self .max_iter = max_iter
196
213
self .tol = tol
197
214
self .alpha_1 = alpha_1
198
215
self .alpha_2 = alpha_2
@@ -204,6 +221,7 @@ def __init__(
204
221
self .fit_intercept = fit_intercept
205
222
self .copy_X = copy_X
206
223
self .verbose = verbose
224
+ self .n_iter = n_iter
207
225
208
226
def fit (self , X , y , sample_weight = None ):
209
227
"""Fit the model.
@@ -226,8 +244,26 @@ def fit(self, X, y, sample_weight=None):
226
244
self : object
227
245
Returns the instance itself.
228
246
"""
247
+
229
248
self ._validate_params ()
230
249
250
+ max_iter = self .max_iter
251
+ # TODO(1.5) Remove
252
+ if self .n_iter != "deprecated" :
253
+ if max_iter is not None :
254
+ raise ValueError (
255
+ "Both `n_iter` and `max_iter` attributes were set. Attribute"
256
+ " `n_iter` was deprecated in version 1.3 and will be removed in"
257
+ " 1.5. To avoid this error, only set the `max_iter` attribute."
258
+ )
259
+ warnings .warn (
260
+ "'n_iter' was renamed to 'max_iter' in version 1.3 and "
261
+ "will be removed in 1.5" ,
262
+ FutureWarning ,
263
+ )
264
+ max_iter = self .n_iter
265
+ elif max_iter is None :
266
+ max_iter = 300
231
267
X , y = self ._validate_data (X , y , dtype = [np .float64 , np .float32 ], y_numeric = True )
232
268
233
269
if sample_weight is not None :
@@ -274,7 +310,7 @@ def fit(self, X, y, sample_weight=None):
274
310
eigen_vals_ = S ** 2
275
311
276
312
# Convergence loop of the bayesian ridge regression
277
- for iter_ in range (self . n_iter ):
313
+ for iter_ in range (max_iter ):
278
314
279
315
# update posterior mean coef_ based on alpha_ and lambda_ and
280
316
# compute corresponding rmse
0 commit comments