8000 DEP Deprecates 'normalize' in _bayes.py by maikia · Pull Request #17746 · scikit-learn/scikit-learn · GitHub
[go: up one dir, main page]

Skip to content

DEP Deprecates 'normalize' in _bayes.py #17746

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Apr 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/whats_new/v1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,8 @@ Changelog
Ridge, RidgeClassifier, RidgeCV or RidgeClassifierCV were deprecated in:
:pr:`17772` by :user:`Maria Telenczuk <maikia>` and
:user:`Alexandre Gramfort <agramfort>`.
BayesianRidge, ARDRegression were deprecated in:
:pr:`17746` by :user:`Maria Telenczuk <maikia>`.

- |Fix|: `sample_weight` are now fully taken into account in linear models
when `normalize=True` for both feature centering and feature
Expand Down
32 changes: 25 additions & 7 deletions sklearn/linear_model/_bayes.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from ._base import LinearModel, _rescale_data
from ..base import RegressorMixin
from ._base import _deprecate_normalize
from ..utils.extmath import fast_logdet
from scipy.linalg import pinvh
from ..utils.validation import _check_sample_weight
Expand Down Expand Up @@ -84,6 +85,10 @@ class BayesianRidge(RegressorMixin, LinearModel):
:class:`~sklearn.preprocessing.StandardScaler` before calling ``fit``
on an estimator with ``normalize=False``.

.. deprecated:: 1.0
``normalize`` was deprecated in version 1.0 and will be removed in
1.2.

copy_X : bool, default=True
If True, X will be copied; else, it may be overwritten.

Expand Down Expand Up @@ -158,7 +163,7 @@ class BayesianRidge(RegressorMixin, LinearModel):
def __init__(self, *, n_iter=300, tol=1.e-3, alpha_1=1.e-6, alpha_2=1.e-6,
lambda_1=1.e-6, lambda_2=1.e-6, alpha_init=None,
lambda_init=None, compute_score=False, fit_intercept=True,
normalize=False, copy_X=True, verbose=False):
normalize='deprecated', copy_X=True, verbose=False):
self.n_iter = n_iter
self.tol = tol
self.alpha_1 = alpha_1
Expand Down Expand Up @@ -193,6 +198,10 @@ def fit(self, X, y, sample_weight=None):
-------
self : returns an instance of self.
"""
self._normalize = _deprecate_normalize(
self.normalize, default=False,
estimator_name=self.__class__.__name__
)

if self.n_iter < 1:
raise ValueError('n_iter should be greater than or equal to 1.'
Expand All @@ -205,7 +214,7 @@ def fit(self, X, y, sample_weight=None):
dtype=X.dtype)

X, y, X_offset_, y_offset_, X_scale_ = self._preprocess_data(
X, y, self.fit_intercept, self.normalize, self.copy_X,
X, y, self.fit_intercept, self._normalize, self.copy_X,
sample_weight=sample_weight)

if sample_weight is not None:
Expand Down Expand Up @@ -325,7 +334,7 @@ def predict(self, X, return_std=False):
if return_std is False:
return y_mean
else:
if self.normalize:
if self._normalize:
X = (X - self.X_offset_) / self.X_scale_
sigmas_squared_data = (np.dot(X, self.sigma_) * X).sum(axis=1)
y_std = np.sqrt(sigmas_squared_data + (1. / self.alpha_))
Expand Down Expand Up @@ -445,6 +454,10 @@ class ARDRegression(RegressorMixin, LinearModel):
:class:`~sklearn.preprocessing.StandardScaler` before calling ``fit``
on an estimator with ``normalize=False``.

.. deprecated:: 1.0
``normalize`` was deprecated in version 1.0 and will be removed in
1.2.

copy_X : bool, default=True
If True, X will be copied; else, it may be overwritten.

Expand Down Expand Up @@ -510,8 +523,8 @@ class ARDRegression(RegressorMixin, LinearModel):
@_deprecate_positional_args
def __init__(self, *, n_iter=300, tol=1.e-3, alpha_1=1.e-6, alpha_2=1.e-6,
lambda_1=1.e-6, lambda_2=1.e-6, compute_score=False,
threshold_lambda=1.e+4, fit_intercept=True, normalize=False,
copy_X=True, verbose=False):
threshold_lambda=1.e+4, fit_intercept=True,
normalize='deprecated', copy_X=True, verbose=False):
self.n_iter = n_iter
self.tol = tol
self.fit_intercept = fit_intercept
Expand Down Expand Up @@ -543,14 +556,19 @@ def fit(self, X, y):
-------
self : returns an instance of self.
"""
self._normalize = _deprecate_normalize(
self.normalize, default=False,
estimator_name=self.__class__.__name__
)

X, y = self._validate_data(X, y, dtype=np.float64, y_numeric=True,
ensure_min_samples=2)

n_samples, n_features = X.shape
coef_ = np.zeros(n_features)

X, y, X_offset_, y_offset_, X_scale_ = self._preprocess_data(
X, y, self.fit_intercept, self.normalize, self.copy_X)
X, y, self.fit_intercept, self._normalize, self.copy_X)

self.X_offset_ = X_offset_
self.X_scale_ = X_scale_
Expand Down Expand Up @@ -686,7 +704,7 @@ def predict(self, X, return_std=False):
if return_std is False:
return y_mean
else:
if self.normalize:
if self._normalize:
X = (X - self.X_offset_) / self.X_scale_
X = X[:, self.lambda_ < self.threshold_lambda]
sigmas_squared_data = (np.dot(X, self.sigma_) * X).sum(axis=1)
Expand Down
2 changes: 2 additions & 0 deletions sklearn/linear_model/tests/test_bayes.py
< 8000 /div>
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,8 @@ def test_update_sigma(seed):
np.testing.assert_allclose(sigma, sigma_woodbury)


# FIXME: 'normalize' to be removed in 1.2 in LinearRegression
@pytest.mark.filterwarnings("ignore:'normalize' was deprecated")
def test_ard_regression_predict_normalize_true():
"""Check that we can predict with `normalize=True` and `return_std=True`.
Non-regression test for:
Expand Down
5 changes: 4 additions & 1 deletion sklearn/linear_model/tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
from sklearn.linear_model import RidgeCV
from sklearn.linear_model import RidgeClassifier
from sklearn.linear_model import RidgeClassifierCV
from sklearn.linear_model import BayesianRidge
from sklearn.linear_model import ARDRegression

from sklearn.utils import check_random_state

Expand All @@ -24,7 +26,8 @@
)
@pytest.mark.parametrize(
"estimator",
[LinearRegression, Ridge, RidgeCV, RidgeClassifier, RidgeClassifierCV]
[LinearRegression, Ridge, RidgeCV, RidgeClassifier, RidgeClassifierCV,
BayesianRidge, ARDRegression]
)
# FIXME remove test in 1.2
def test_linear_model_normalize_deprecation_message(
Expand Down
0