Skip to content

inverse_transform in KernelPCA does not account for the mean #16654

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

Closed
lrjball opened this issue Mar 7, 2020 · 1 comment · Fixed by #16655
Closed

inverse_transform in KernelPCA does not account for the mean #16654

lrjball opened this issue Mar 7, 2020 · 1 comment · Fixed by #16655

Comments

@lrjball
Copy link
Contributor

lrjball commented Mar 7, 2020

Describe the bug

KernelPCA.inverse_transform gives back a zero-meaned dataset, even if the data used to fit the PCA did not have zero mean.

Steps/Code to Reproduce

Example:

from sklearn.datasets import make_blobs
from sklearn.decomposition import KernelPCA

X, _ = make_blobs(n_samples=100, centers=[[1, 1]], random_state=0)

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

print(X.mean(axis=0))
print(X_inv.mean(axis=0))

Expected Results

[0.99904232 1.14277867]
[0.99904232 1.14277867]

The mean of the data after transforming and then inverse transforming should be the same, or at least very similar (maybe there are rounding errors etc.) to the mean of the original dataset.

Actual Results

[0.99904232 1.14277867]
[-5.07371922e-16  2.21767049e-16]

(The inverse transformed data still has zero mean)

Versions

System:
python: 3.6.8 (default, Oct 7 2019, 12:59:55) [GCC 8.3.0]
executable: /home//.virtualenvs/kernelpca_issue/bin/python3
machine: Linux-4.15.0-88-generic-x86_64-with-Ubuntu-18.04-bionic

Python dependencies:
pip: 20.0.2
setuptools: 45.2.0
sklearn: 0.22.2.post1
numpy: 1.18.1
scipy: 1.4.1
Cython: None
pandas: None
matplotlib: None
joblib: 0.14.1

Built with OpenMP: True

@lrjball
Copy link
Contributor Author

lrjball commented Mar 8, 2020

After some digging, this bug is not exclusive to non-centered data, see:

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

X, _ = make_blobs(n_samples=100, 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()

which will still raise an AssertionError

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants