10000 Make Changes · scikit-learn/scikit-learn@136d786 · GitHub
[go: up one dir, main page]

Skip to content

Commit 136d786

Browse files
committed
Make Changes
1 parent d92fba7 commit 136d786

File tree

2 files changed

+73
-9
lines changed

2 files changed

+73
-9
lines changed

sklearn/linear_model/_bayes.py

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# Authors: V. Michel, F. Pedregosa, A. Gramfort
66
# License: BSD 3 clause
77

8+
import warnings
89
from math import log
910
from numbers import Integral, Real
1011
import numpy as np
@@ -15,7 +16,8 @@
1516
from ..utils.extmath import fast_logdet
1617
from scipy.linalg import pinvh
1718
from ..utils.validation import _check_sample_weight
18-
from ..utils._param_validation import Interval
19+
from ..utils._param_validation import Interval, Hidden, StrOptions
20+
1921

2022
###############################################################################
2123
# BayesianRidge regression
@@ -32,8 +34,11 @@ class BayesianRidge(RegressorMixin, LinearModel):
3234
3335
Parameters
3436
----------
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
3742
3843
tol : float, default=1e-3
3944
Stop the algorithm if w has converged.
@@ -83,14 +88,21 @@ class BayesianRidge(RegressorMixin, LinearModel):
8388
verbose : bool, default=False
8489
Verbose mode when fitting the model.
8590
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+
8698
Attributes
8799
----------
88100
coef_ : array-like of shape (n_features,)
89101
Coefficients of the regression model (mean of distribution)
90102
91103
intercept_ : float
92104
Independent term in decision function. Set to 0.0 if
93-
``fit_intercept = False``.
105+
`fit_intercept = False`.
94106
95107
alpha_ : float
96108
Estimated precision of the noise.
@@ -162,7 +174,7 @@ class BayesianRidge(RegressorMixin, LinearModel):
162174
"""
163175

164176
_parameter_constraints: dict = {
165-
"n_iter": [Interval(Integral, 1, None, closed="left")],
177+
"max_iter": [Interval(Integral, 1, None, closed="left"), None],
166178
"tol": [Interval(Real, 0, None, closed="neither")],
167179
"alpha_1": [Interval(Real, 0, None, closed="left")],
168180
"alpha_2": [Interval(Real, 0, None, closed="left")],
@@ -174,12 +186,16 @@ class BayesianRidge(RegressorMixin, LinearModel):
174186
"fit_intercept": ["boolean"],
175187
"copy_X": ["boolean"],
176188
"verbose": ["verbose"],
189+
"n_iter": [
190+
Interval(Integral, 1, None, closed="left"),
191+
Hidden(StrOptions({"deprecated"})),
192+
],
177193
}
178194

179195
def __init__(
180196
self,
181197
*,
182-
n_iter=300,
198+
max_iter=None, # TODO(1.5): Set to 300
183199
tol=1.0e-3,
184200
alpha_1=1.0e-6,
185201
alpha_2=1.0e-6,
@@ -191,8 +207,9 @@ def __init__(
191207
fit_intercept=True,
192208
copy_X=True,
193209
verbose=False,
210+
n_iter="deprecated", # TODO(1.5): Remove
194211
):
195-
self.n_iter = n_iter
212+
self.max_iter = max_iter
196213
self.tol = tol
197214
self.alpha_1 = alpha_1
198215
self.alpha_2 = alpha_2
@@ -204,6 +221,7 @@ def __init__(
204221
self.fit_intercept = fit_intercept
205222
self.copy_X = copy_X
206223
self.verbose = verbose
224+
self.n_iter = n_iter
207225

208226
def fit(self, X, y, sample_weight=None):
209227
"""Fit the model.
@@ -226,8 +244,26 @@ def fit(self, X, y, sample_weight=None):
226244
self : object
227245
Returns the instance itself.
228246
"""
247+
229248
self._validate_params()
230249

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
231267
X, y = self._validate_data(X, y, dtype=[np.float64, np.float32], y_numeric=True)
232268

233269
if sample_weight is not None:
@@ -274,7 +310,7 @@ def fit(self, X, y, sample_weight=None):
274310
eigen_vals_ = S**2
275311

276312
# Convergence loop of the bayesian ridge regression
277-
for iter_ in range(self.n_iter):
313+
for iter_ in range(max_iter):
278314

279315
# update posterior mean coef_ based on alpha_ and lambda_ and
280316
# compute corresponding rmse

sklearn/linear_model/tests/test_bayes.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def test_bayesian_ridge_score_values():
7373
alpha_2=alpha_2,
7474
lambda_1=lambda_1,
7575
lambda_2=lambda_2,
76-
n_iter=1,
76+
max_iter=1,
7777
fit_intercept=False,
7878
compute_score=True,
7979
)
@@ -292,3 +292,31 @@ def test_dtype_correctness(Estimator):
292292
coef_32 = model.fit(X.astype(np.float32), y).coef_
293293
coef_64 = model.fit(X.astype(np.float64), y).coef_
294294
np.testing.assert_allclose(coef_32, coef_64, rtol=1e-4)
295+
296+
297+
def test_bayesian_ridge_n_iter_deprecated():
298+
# check the deprecation warning of n_iter
299+
# TODO(1.5) remove
300+
depr_msg = (
301+
"'n_iter' was renamed to 'max_iter' in version 1.3 and will be removed in 1.5"
302+
)
303+
X, y = diabetes.data, diabetes.target
304+
est = BayesianRidge(n_iter=5)
3 975B 05+
306+
with pytest.warns(FutureWarning, match=depr_msg):
307+
est.fit(X, y)
308+
309+
310+
def test_bayesian_ridge_max_iter_and_n_iter_both_set():
311+
# TODO(1.5) remove
312+
"""Check that a ValueError is raised when both `max_iter` and `n_iter` are set."""
313+
err_msg = (
314+
"Both `n_iter` and `max_iter` attributes were set. Attribute"
315+
" `n_iter` was deprecated in version 1.3 and will be removed in"
316+
" 1.5. To avoid this error, only set the `max_iter` attribute."
317+
)
318+
X, y = diabetes.data, diabetes.target
319+
est = BayesianRidge(n_iter=5, max_iter=5)
320+
321+
with pytest.raises(ValueError, match=err_msg):
322+
est.fit(X, y)

0 commit comments

Comments
 (0)
0