Skip to content

Fix Make centering inplace in KernelPCA #29100

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
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: 6 additions & 4 deletions sklearn/decomposition/_kernel_pca.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,10 +324,10 @@ def _get_kernel(self, X, Y=None):
X, Y, metric=self.kernel, filter_params=True, n_jobs=self.n_jobs, **params
)

def _fit_transform(self, K):
def _fit_transform_in_place(self, K):
"""Fit's using kernel K"""
# center kernel
K = self._centerer.fit_transform(K)
# center kernel in place
K = self._centerer.fit(K).transform(K, copy=False)

# adjust n_components according to user inputs
if self.n_components is None:
Expand Down Expand Up @@ -438,7 +438,9 @@ def fit(self, X, y=None):
self.gamma_ = 1 / X.shape[1] if self.gamma is None else self.gamma
self._centerer = KernelCenterer().set_output(transform="default")
K = self._get_kernel(X)
self._fit_transform(K)
# When kernel="precomputed", K is X but it's safe to perform in place operations
# on K because a copy was made before if requested by copy_X.
self._fit_transform_in_place(K)

if self.fit_inverse_transform:
# no need to use the kernel to transform X, use shortcut expression
Expand Down