8000 ENH adding sample weights for BayesianRidge (#10112) · maskani-moh/scikit-learn@8bc5378 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8bc5378

Browse files
pstjohnmaskani-moh
authored andcommitted
ENH adding sample weights for BayesianRidge (scikit-learn#10112)
1 parent 965c072 commit 8bc5378

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

doc/whats_new/v0.20.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ Classifiers and regressors
8181
``inverse_func`` are the inverse of each other.
8282
:issue:`9399` by :user:`Guillaume Lemaitre <glemaitre>`.
8383

84+
- Add `sample_weight` parameter to the fit method of
85+
:class:`linear_model.BayesianRidge` for weighted linear regression.
86+
:issue:`10111` by :user:`Peter St. John <pstjohn>`.
87+
8488
Model evaluation and meta-estimators
8589

8690
- A scorer based on :func:`metrics.brier_score_loss` is also available.

sklearn/linear_model/bayes.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from scipy import linalg
1212
from scipy.linalg import pinvh
1313

14-
from .base import LinearModel
14+
from .base import LinearModel, _rescale_data
1515
from ..base import RegressorMixin
1616
from ..utils.extmath import fast_logdet
1717
from ..utils import check_X_y
@@ -140,7 +140,7 @@ def __init__(self, n_iter=300, tol=1.e-3, alpha_1=1.e-6, alpha_2=1.e-6,
140140
self.copy_X = copy_X
141141
self.verbose = verbose
142142

143-
def fit(self, X, y):
143+
def fit(self, X, y, sample_weight=None):
144144
"""Fit the model
145145
146146
Parameters
@@ -150,13 +150,25 @@ def fit(self, X, y):
150150
y : numpy array of shape [n_samples]
151151
Target values. Will be cast to X's dtype if necessary
152152
153+
sample_weight : numpy array of shape [n_samples]
154+
Individual weights for each sample
155+
156+
.. versionadded:: 0.20
157+
parameter *sample_weight* support to BayesianRidge.
158+
153159
Returns
154160
-------
155161
self : returns an instance of self.
156162
"""
157163
X, y = check_X_y(X, y, dtype=np.float64, y_numeric=True)
158164
X, y, X_offset_, y_offset_, X_scale_ = self._preprocess_data(
159-
X, y, self.fit_intercept, self.normalize, self.copy_X)
165+
X, y, self.fit_intercept, self.normalize, self.copy_X,
166+
sample_weight=sample_weight)
167+
168+
if sample_weight is not None:
169+
# Sample weight can be implemented via a simple rescaling.
170+
X, y = _rescale_data(X, y, sample_weight)
171+
160172
self.X_offset_ = X_offset_
161173
self.X_scale_ = X_scale_
162174
n_samples, n_features = X.shape

sklearn/linear_model/tests/test_bayes.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,21 @@ def test_bayesian_ridge_parameter():
5050
assert_almost_equal(rr_model.intercept_, br_model.intercept_)
5151

5252

53+
def test_bayesian_sample_weights():
54+
# Test correctness of the sample_weights method
55+
X = np.array([[1, 1], [3, 4], [5, 7], [4, 1], [2, 6], [3, 10], [3, 2]])
56+
y = np.array([1, 2, 3, 2, 0, 4, 5]).T
57+
w = np.array([4, 3, 3, 1, 1, 2, 3]).T
58+
59+
# A Ridge regression model using an alpha value equal to the ratio of
60+
# lambda_ and alpha_ from the Bayesian Ridge model must be identical
61+
br_model = BayesianRidge(compute_score=True).fit(X, y, sample_weight=w)
62+
rr_model = Ridge(alpha=br_model.lambda_ / br_model.alpha_).fit(
63+
X, y, sample_weight=w)
64+
assert_array_almost_equal(rr_model.coef_, br_model.coef_)
65+
assert_almost_equal(rr_model.intercept_, br_model.intercept_)
66+
67+
5368
def test_toy_bayesian_ridge_object():
5469
# Test BayesianRidge on toy
5570
X = np.array([[1], [2], [6], [8], [10]])

0 commit comments

Comments
 (0)
0