Skip to content
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

[MRG+1] Deprecate ridge_alpha param on SparsePCA.transform() #8137

Merged
merged 5 commits into from
Jan 4, 2017
Merged
Show file tree
Hide file tree
Changes from 4 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
18 changes: 16 additions & 2 deletions sklearn/decomposition/sparse_pca.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# Author: Vlad Niculae, Gael Varoquaux, Alexandre Gramfort
# License: BSD 3 clause

import warnings

import numpy as np

from ..utils import check_random_state, check_array
Expand Down Expand Up @@ -130,7 +132,7 @@ def fit(self, X, y=None):
self.error_ = E
return self

def transform(self, X, ridge_alpha=None):
def transform(self, X, ridge_alpha='deprecated'):
"""Least Squares projection of the data onto the sparse components.

To avoid instability issues in case the system is under-determined,
Expand All @@ -150,6 +152,10 @@ def transform(self, X, ridge_alpha=None):
Amount of ridge shrinkage to apply in order to improve
conditioning.

.. deprecated:: 0.19
This parameter will be removed in 0.21.
Specify ``ridge_alpha`` in the ``SparsePCA`` constructor.

Returns
-------
X_new array, shape (n_samples, n_components)
Expand All @@ -158,7 +164,15 @@ def transform(self, X, ridge_alpha=None):
check_is_fitted(self, 'components_')

X = check_array(X)
ridge_alpha = self.ridge_alpha if ridge_alpha is None else ridge_alpha
if ridge_alpha != 'deprecated':
warnings.warn("The ridge_alpha parameter on transform() is "
"deprecated since 0.19 and will be removed in 0.21. "
"Specify ridge_alpha in the SparsePCA constructor.",
DeprecationWarning)
if ridge_alpha is None:
ridge_alpha = self.ridge_alpha
else:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is going to break if ridge_alpha=None is passed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please test this case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the catch. In this case I have it reverting to self.ridge_alpha.

ridge_alpha = self.ridge_alpha
U = ridge_regression(self.components_.T, X.T, ridge_alpha,
solver='cholesky')
s = np.sqrt((U ** 2).sum(axis=0))
Expand Down
5 changes: 5 additions & 0 deletions sklearn/decomposition/tests/test_sparse_pca.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from sklearn.utils.testing import SkipTest
from sklearn.utils.testing import assert_true
from sklearn.utils.testing import assert_false
from sklearn.utils.testing import assert_warns
from sklearn.utils.testing import if_safe_multiprocessing_with_blas

from sklearn.decomposition import SparsePCA, MiniBatchSparsePCA
Expand Down Expand Up @@ -70,6 +71,10 @@ def test_fit_transform():
spca_lasso.fit(Y)
assert_array_almost_equal(spca_lasso.components_, spca_lars.components_)

# Test that deprecated ridge_alpha parameter throws warning
assert_warns(DeprecationWarning, spca_lars.transform, Y, ridge_alpha=0.01)
assert_warns(DeprecationWarning, spca_lars.transform, Y, ridge_alpha=None)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a niptick, it will be better to use assert_warns_message with a part of the message (just to be sure it's the right message).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the tip



@if_safe_multiprocessing_with_blas
def test_fit_transform_parallel():
Expand Down