Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
10 changes: 8 additions & 2 deletions doc/whats_new/v1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -358,11 +358,17 @@ Changelog
LinearRegression was deprecated in:
:pr:`17743` by :user:`Maria Telenczuk <maikia>` and
:user:`Alexandre Gramfort <agramfort>`.
Ridge, RidgeClassifier, RidgeCV or RidgeClassifierCV were deprecated in:
The ``normalize`` parameter in Ridge, RidgeClassifier, RidgeCV or
RidgeClassifierCV were deprecated and will be removed in 1.2.
:pr:`17772` by :user:`Maria Telenczuk <maikia>` and
:user:`Alexandre Gramfort <agramfort>`.
BayesianRidge, ARDRegression were deprecated in:
Same for BayesianRidge, ARDRegression in:
:pr:`17746` by :user:`Maria Telenczuk <maikia>`.
The ``normalize`` parameter of :class:`linear_model.OrthogonalMatchingPursuit`
:class:`linear_model.OrthogonalMatchingPursuitCV` will default to
False in 1.2 and will be removed in 1.4.
:pr:`17750` by :user:`Maria Telenczuk <maikia>` and
:user:`Alexandre Gramfort <agramfort>`.

- |Fix| `sample_weight` are now fully taken into account in linear models
when `normalize=True` for both feature centering and feature
Expand Down
5 changes: 3 additions & 2 deletions examples/linear_model/plot_omp.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
plt.stem(idx, w[idx], use_line_collection=True)

# plot the noise-free reconstruction
omp = OrthogonalMatchingPursuit(n_nonzero_coefs=n_nonzero_coefs)
omp = OrthogonalMatchingPursuit(n_nonzero_coefs=n_nonzero_coefs,
normalize=False)
omp.fit(X, y)
coef = omp.coef_
idx_r, = coef.nonzero()
Expand All @@ -60,7 +61,7 @@
plt.stem(idx_r, coef[idx_r], use_line_collection=True)

# plot the noisy reconstruction with number of non-zeros set by CV
omp_cv = OrthogonalMatchingPursuitCV()
omp_cv = OrthogonalMatchingPursuitCV(normalize=False)
omp_cv.fit(X, y_noisy)
coef = omp_cv.coef_
idx_r, = coef.nonzero()
Expand Down
37 changes: 29 additions & 8 deletions sklearn/linear_model/_omp.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from scipy.linalg.lapack import get_lapack_funcs
from joblib import Parallel

from ._base import LinearModel, _pre_fit
from ._base import LinearModel, _pre_fit, _deprecate_normalize
from ..base import RegressorMixin, MultiOutputMixin
from ..utils import as_float_array, check_array
from ..utils.fixes import delayed
Expand Down Expand Up @@ -616,6 +616,10 @@ class OrthogonalMatchingPursuit(MultiOutputMixin, 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. It will default
to False in 1.2 and be removed in 1.4.

precompute : 'auto' or bool, default='auto'
Whether to use a precomputed Gram and Xy matrix to speed up
calculations. Improves performance when :term:`n_targets` or
Expand Down Expand Up @@ -648,7 +652,7 @@ class OrthogonalMatchingPursuit(MultiOutputMixin, RegressorMixin, LinearModel):
>>> from sklearn.linear_model import OrthogonalMatchingPursuit
>>> from sklearn.datasets import make_regression
>>> X, y = make_regression(noise=4, random_state=0)
>>> reg = OrthogonalMatchingPursuit().fit(X, y)
>>> reg = OrthogonalMatchingPursuit(normalize=False).fit(X, y)
>>> reg.score(X, y)
0.9991...
>>> reg.predict(X[:1,])
Expand Down Expand Up @@ -683,7 +687,7 @@ def __init__(
n_nonzero_coefs=None,
tol=None,
fit_intercept=True,
normalize=True,
normalize="deprecated",
precompute="auto",
):
self.n_nonzero_coefs = n_nonzero_coefs
Expand All @@ -709,11 +713,15 @@ def fit(self, X, y):
self : object
returns an instance of self.
"""
_normalize = _deprecate_normalize(
self.normalize, default=True, estimator_name=self.__class__.__name__
)

X, y = self._validate_data(X, y, multi_output=True, y_numeric=True)
n_features = X.shape[1]

X, y, X_offset, y_offset, X_scale, Gram, Xy = _pre_fit(
X, y, None, self.precompute, self.normalize, self.fit_intercept, copy=True
X, y, None, self.precompute, _normalize, self.fit_intercept, copy=True
)

if y.ndim == 1:
Expand Down Expand Up @@ -797,6 +805,10 @@ def _omp_path_residues(
:class:`~sklearn.preprocessing.StandardScaler` before calling ``fit``
on an estimator with ``normalize=False``.

.. deprecated:: 1.0
``normalize`` was deprecated in version 1.0. It will default
to False in 1.2 and be removed in 1.4.

max_iter : int, default=100
Maximum numbers of iterations to perform, therefore maximum features
to include. 100 by default.
Expand Down Expand Up @@ -872,6 +884,10 @@ class OrthogonalMatchingPursuitCV(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. It will default
to False in 1.2 and be removed in 1.4.

max_iter : int, default=None
Maximum numbers of iterations to perform, therefore maximum features
to include. 10% of ``n_features`` but at least 5 if available.
Expand Down Expand Up @@ -929,7 +945,7 @@ class OrthogonalMatchingPursuitCV(RegressorMixin, LinearModel):
>>> from sklearn.datasets import make_regression
>>> X, y = make_regression(n_features=100, n_informative=10,
... noise=4, random_state=0)
>>> reg = OrthogonalMatchingPursuitCV(cv=5).fit(X, y)
>>> reg = OrthogonalMatchingPursuitCV(cv=5, normalize=False).fit(X, y)
>>> reg.score(X, y)
0.9991...
>>> reg.n_nonzero_coefs_
Expand All @@ -956,7 +972,7 @@ def __init__(
*,
copy=True,
fit_intercept=True,
normalize=True,
normalize="deprecated",
max_iter=None,
cv=None,
n_jobs=None,
Expand Down Expand Up @@ -986,6 +1002,11 @@ def fit(self, X, y):
self : object
returns an instance of self.
"""

_normalize = _deprecate_normalize(
self.normalize, default=True, estimator_name=self.__class__.__name__
)

X, y = self._validate_data(
X, y, y_numeric=True, ensure_min_features=2, estimator=self
)
Expand All @@ -1004,7 +1025,7 @@ def fit(self, X, y):
y[test],
self.copy,
self.fit_intercept,
self.normalize,
_normalize,
max_iter,
)
for train, test in cv.split(X)
Expand All @@ -1019,7 +1040,7 @@ def fit(self, X, y):
omp = OrthogonalMatchingPursuit(
n_nonzero_coefs=best_n_nonzero_coefs,
fit_intercept=self.fit_intercept,
normalize=self.normalize,
normalize=_normalize,
)
omp.fit(X, y)
self.coef_ = omp.coef_
Expand Down
30 changes: 30 additions & 0 deletions sklearn/linear_model/tests/test_omp.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,30 @@
# and y (n_samples, 3)


# FIXME: 'normalize' to set to False in 1.2 and removed in 1.4
@pytest.mark.parametrize(
"OmpModel", [OrthogonalMatchingPursuit, OrthogonalMatchingPursuitCV]
)
@pytest.mark.parametrize(
"normalize, n_warnings", [(True, 0), (False, 0), ("deprecated", 1)]
)
def test_assure_warning_when_normalize(OmpModel, normalize, n_warnings):
# check that we issue a FutureWarning when normalize was set
rng = check_random_state(0)
n_samples = 200
n_features = 2
X = rng.randn(n_samples, n_features)
X[X < 0.1] = 0.0
y = rng.rand(n_samples)

model = OmpModel(normalize=normalize)
with pytest.warns(None) as record:
model.fit(X, y)

record = [r for r in record if r.category == FutureWarning]
assert len(record) == n_warnings


def test_correct_shapes():
assert orthogonal_mp(X, y[:, 0], n_nonzero_coefs=5).shape == (n_features,)
assert orthogonal_mp(X, y, n_nonzero_coefs=5).shape == (n_features, 3)
Expand Down Expand Up @@ -125,6 +149,8 @@ def test_orthogonal_mp_gram_readonly():
assert_array_almost_equal(gamma[:, 0], gamma_gram, decimal=2)


# FIXME: 'normalize' to be removed in 1.4
@pytest.mark.filterwarnings("ignore:The default of 'normalize'")
def test_estimator():
omp = OrthogonalMatchingPursuit(n_nonzero_coefs=n_nonzero_coefs)
omp.fit(X, y[:, 0])
Expand Down Expand Up @@ -211,6 +237,8 @@ def test_omp_return_path_prop_with_gram():
assert_array_almost_equal(path[:, :, -1], last)


# FIXME: 'normalize' to be removed in 1.4
@pytest.mark.filterwarnings("ignore:The default of 'normalize'")
def test_omp_cv():
y_ = y[:, 0]
gamma_ = gamma[:, 0]
Expand All @@ -227,6 +255,8 @@ def test_omp_cv():
assert_array_almost_equal(ompcv.coef_, omp.coef_)


# FIXME: 'normalize' to be removed in 1.4
@pytest.mark.filterwarnings("ignore:The default of 'normalize'")
def test_omp_reaches_least_squares():
# Use small simple data; it's a sanity check but OMP can stop early
rng = check_random_state(0)
Expand Down
7 changes: 7 additions & 0 deletions sklearn/tests/test_docstring_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,13 @@ def test_fit_docstring_attributes(name, Estimator):
# default="auto" raises an error with the shape of `X`
est.set_params(n_components=2)

# FIXME: TO BE REMOVED in 1.4 (avoid FutureWarning)
if Estimator.__name__ in (
"OrthogonalMatchingPursuit",
"OrthogonalMatchingPursuitCV",
):
est.set_params(normalize=False)

# FIXME: TO BE REMOVED for 1.1 (avoid FutureWarning)
if Estimator.__name__ == "NMF":
est.set_params(init="nndsvda")
Expand Down