Skip to content

Fixed issue with KernelPCA.inverse_transform mean #16655

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 5 commits into from
Mar 10, 2020
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
4 changes: 4 additions & 0 deletions doc/whats_new/v0.23.rst
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ Changelog
:func:`decomposition.non_negative_factorization` now preserves float32 dtype.
:pr:`16280` by :user:`Jeremie du Boisberranger <jeremiedbb>`.

- |Fix| :class:`decomposition.KernelPCA` method ``inverse_transform`` now
Copy link
Member

Choose a reason for hiding this comment

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

Maybe could be "fixed .... in the case that data was not centred" would be more helpful to users

Copy link
Contributor Author

@lrjball lrjball Mar 8, 2020

Choose a reason for hiding this comment

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

So actually after doing some digging, it was still returning the wrong thing even for centered data. I just only noticed the bug in non-centered data because the mean of the inverse-transformed data was zero when the original data set was not centered.

For example, in 0.22.0 the following still does not work:

import numpy as np
from sklearn.datasets import make_blobs
from sklearn.decomposition import KernelPCA

X, _ = make_blobs(n_samples=100, centers=[[1, 1, 1, 1]], random_state=0)
X = X - X.mean(axis=0)
kp = KernelPCA(n_components=2, fit_inverse_transform=True)
X_trans = kp.fit_transform(X)
X_inv = kp.inverse_transform(X_trans)

assert np.isclose(X, X_inv).all()

So this PR fixes the inverse_transform function for all X.

However, I can still update the message if there is a better way of phrasing this change.

applies the correct inverse transform to the transformed data. :pr:`16655`
by :user:`Lewis Ball <lrjball>`.

:mod:`sklearn.ensemble`
.......................

Expand Down
3 changes: 2 additions & 1 deletion sklearn/decomposition/_kernel_pca.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,5 +358,6 @@ def inverse_transform(self, X):
"the inverse transform is not available.")

K = self._get_kernel(X, self.X_transformed_fit_)

n_samples = self.X_transformed_fit_.shape[0]
K.flat[::n_samples + 1] += self.alpha
return np.dot(K, self.dual_coef_)
13 changes: 13 additions & 0 deletions sklearn/decomposition/tests/test_kernel_pca.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from sklearn.decomposition import PCA, KernelPCA
from sklearn.datasets import make_circles
from sklearn.datasets import make_blobs
from sklearn.linear_model import Perceptron
from sklearn.pipeline import Pipeline
from sklearn.model_selection import GridSearchCV
Expand Down Expand Up @@ -282,3 +283,15 @@ def test_kernel_conditioning():
# check that the small non-zero eigenvalue was correctly set to zero
assert kpca.lambdas_.min() == 0
assert np.all(kpca.lambdas_ == _check_psd_eigenvalues(kpca.lambdas_))


@pytest.mark.parametrize("kernel",
["linear", "poly", "rbf", "sigmoid", "cosine"])
def test_kernel_pca_inverse_transform(kernel):
X, *_ = make_blobs(n_samples=100, n_features=4, centers=[[1, 1, 1, 1]],
random_state=0)

kp = KernelPCA(n_components=2, kernel=kernel, fit_inverse_transform=True)
X_trans = kp.fit_transform(X)
X_inv = kp.inverse_transform(X_trans)
assert_allclose(X, X_inv)