Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 535ef55

Browse filesBrowse files
authored
BUG Fix issue with KernelPCA.inverse_transform (#16655)
1 parent ae159ec commit 535ef55
Copy full SHA for 535ef55

File tree

Expand file treeCollapse file tree

3 files changed

+19
-1
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+19
-1
lines changed

‎doc/whats_new/v0.23.rst

Copy file name to clipboardExpand all lines: doc/whats_new/v0.23.rst
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ Changelog
142142
:func:`decomposition.non_negative_factorization` now preserves float32 dtype.
143143
:pr:`16280` by :user:`Jeremie du Boisberranger <jeremiedbb>`.
144144

145+
- |Fix| :class:`decomposition.KernelPCA` method ``inverse_transform`` now
146+
applies the correct inverse transform to the transformed data. :pr:`16655`
147+
by :user:`Lewis Ball <lrjball>`.
148+
145149
:mod:`sklearn.ensemble`
146150
.......................
147151

‎sklearn/decomposition/_kernel_pca.py

Copy file name to clipboardExpand all lines: sklearn/decomposition/_kernel_pca.py
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,5 +358,6 @@ def inverse_transform(self, X):
358358
"the inverse transform is not available.")
359359

360360
K = self._get_kernel(X, self.X_transformed_fit_)
361-
361+
n_samples = self.X_transformed_fit_.shape[0]
362+
K.flat[::n_samples + 1] += self.alpha
362363
return np.dot(K, self.dual_coef_)

‎sklearn/decomposition/tests/test_kernel_pca.py

Copy file name to clipboardExpand all lines: sklearn/decomposition/tests/test_kernel_pca.py
+13Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from sklearn.decomposition import PCA, KernelPCA
99
from sklearn.datasets import make_circles
10+
from sklearn.datasets import make_blobs
1011
from sklearn.linear_model import Perceptron
1112
from sklearn.pipeline import Pipeline
1213
from sklearn.model_selection import GridSearchCV
@@ -282,3 +283,15 @@ def test_kernel_conditioning():
282283
# check that the small non-zero eigenvalue was correctly set to zero
283284
assert kpca.lambdas_.min() == 0
284285
assert np.all(kpca.lambdas_ == _check_psd_eigenvalues(kpca.lambdas_))
286+
287+
288+
@pytest.mark.parametrize("kernel",
289+
["linear", "poly", "rbf", "sigmoid", "cosine"])
290+
def test_kernel_pca_inverse_transform(kernel):
291+
X, *_ = make_blobs(n_samples=100, n_features=4, centers=[[1, 1, 1, 1]],
292+
random_state=0)
293+
294+
kp = KernelPCA(n_components=2, kernel=kernel, fit_inverse_transform=True)
295+
X_trans = kp.fit_transform(X)
296+
X_inv = kp.inverse_transform(X_trans)
297+
assert_allclose(X, X_inv)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.