10000 [MRG + 1] Return correct ridge parameter alpha_ and lambda_ for Bayes… · massich/scikit-learn@f49739f · GitHub
[go: up one dir, main page]

Skip to content

Commit f49739f

Browse files
gedeckJoan Massich
authored andcommitted
[MRG + 1] Return correct ridge parameter alpha_ and lambda_ for Bayesian ridge regression (scikit-learn#8567)
* Return correct ridge parameter alpha_ and lambda_ for regression * Add test for coefficients and fix style * Move sklearn.utils.testing to a more reasonable position. * Make flake8 happy * Code cleanup and entry in whats_new.rst
1 parent d3975cf commit f49739f

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

doc/whats_new.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,11 @@ Bug fixes
244244
multiple inheritance context.
245245
:issue:`8316` by :user:`Holger Peters <HolgerPeters>`.
246246

247+
- Fix :func:`sklearn.linear_model.BayesianRidge.fit` to return
248+
ridge parameter `alpha_` and `lambda_` consistent with calculated
249+
coefficients `coef_` and `intercept_`.
250+
:issue:`8224` by :user:`Peter Gedeck <gedeck>`.
251+
247252
API changes summary
248253
-------------------
249254

sklearn/linear_model/bayes.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,11 @@ def fit(self, X, y):
201201
logdet_sigma_[:n_samples] += alpha_ * eigen_vals_
202202
logdet_sigma_ = - np.sum(np.log(logdet_sigma_))
203203

204+
# Preserve the alpha and lambda values that were used to
205+
# calculate the final coefficients
206+
self.alpha_ = alpha_
207+
self.lambda_ = lambda_
208+
204209
# Update alpha and lambda
205210
rmse_ = np.sum((y - np.dot(X, coef_)) ** 2)
206211
gamma_ = (np.sum((alpha_ * eigen_vals_) /
@@ -229,8 +234,6 @@ def fit(self, X, y):
229234
break
230235
coef_old_ = np.copy(coef_)
231236

232-
self.alpha_ = alpha_
233-
self.lambda_ = lambda_
234237
self.coef_ = coef_
235238
sigma_ = np.dot(Vh.T,
236239
Vh / (eigen_vals_ + lambda_ / alpha_)[:, np.newaxis])

sklearn/linear_model/tests/test_bayes.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
import numpy as np
77

88
from sklearn.utils.testing import assert_array_equal
9+
from sklearn.utils.testing import assert_array_almost_equal
10+
from sklearn.utils.testing import assert_almost_equal
911
from sklearn.utils.testing import SkipTest
1012
from sklearn.linear_model.bayes import BayesianRidge, ARDRegression
13+
from sklearn.linear_model import Ridge
1114
from sklearn import datasets
1215

13-
from sklearn.utils.testing import assert_array_almost_equal
14-
1516

1617
def test_bayesian_on_diabetes():
1718
# Test BayesianRidge on diabetes
@@ -34,6 +35,19 @@ def test_bayesian_on_diabetes():
3435
assert_array_equal(np.diff(clf.scores_) > 0, True)
3536

3637

38+
def test_bayesian_ridge_parameter():
39+
# Test correctness of lambda_ and alpha_ parameters (Github issue #8224)
40+
X = np.array([[1, 1], [3, 4], [5, 7], [4, 1], [2, 6], [3, 10], [3, 2]])
41+
y = np.array([1, 2, 3, 2, 0, 4, 5]).T
42+
43+
# A Ridge regression model using an alpha value equal to the ratio of
44+
# lambda_ and alpha_ from the Bayesian Ridge model must be identical
45+
br_model = BayesianRidge(compute_score=True).fit(X, y)
46+
rr_model = Ridge(alpha=br_model.lambda_ / br_model.alpha_).fit(X, y)
47+
assert_array_almost_equal(rr_model.coef_, br_model.coef_)
48+
assert_almost_equal(rr_model.intercept_, br_model.intercept_)
49+
50+
3751
def test_toy_bayesian_ridge_object():
3852
# Test BayesianRidge on toy
3953
X = np.array([[1], [2], [6], [8], [10]])
@@ -64,7 +78,7 @@ def f(X):
6478
return np.dot(X, w) + b
6579

6680
def f_noise(X, noise_mult):
67-
return f(X) + np.random.randn(X.shape[0])*noise_mult
81+
return f(X) + np.random.randn(X.shape[0]) * noise_mult
6882

6983
d = 5
7084
n_train = 50

0 commit comments

Comments
 (0)
0