Skip to content

Commit

Permalink
FIX make sure that KernelPCA works with pandas output and arpack solv…
Browse files Browse the repository at this point in the history
…er (#27583)
  • Loading branch information
glemaitre committed Oct 23, 2023
1 parent d53756e commit 9b977e9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
2 changes: 1 addition & 1 deletion sklearn/decomposition/_kernel_pca.py
Expand Up @@ -432,7 +432,7 @@ def fit(self, X, y=None):
raise ValueError("Cannot fit_inverse_transform with a precomputed kernel.")
X = self._validate_data(X, accept_sparse="csr", copy=self.copy_X)
self.gamma_ = 1 / X.shape[1] if self.gamma is None else self.gamma
self._centerer = KernelCenterer()
self._centerer = KernelCenterer().set_output(transform="default")
K = self._get_kernel(X)
self._fit_transform(K)

Expand Down
15 changes: 14 additions & 1 deletion sklearn/decomposition/tests/test_kernel_pca.py
Expand Up @@ -4,7 +4,8 @@
import pytest
import scipy.sparse as sp

from sklearn.datasets import make_blobs, make_circles
import sklearn
from sklearn.datasets import load_iris, make_blobs, make_circles
from sklearn.decomposition import PCA, KernelPCA
from sklearn.exceptions import NotFittedError
from sklearn.linear_model import Perceptron
Expand Down Expand Up @@ -550,3 +551,15 @@ def test_kernel_pca_inverse_correct_gamma():
X2_recon = kpca2.inverse_transform(kpca1.transform(X))

assert_allclose(X1_recon, X2_recon)


def test_kernel_pca_pandas_output():
"""Check that KernelPCA works with pandas output when the solver is arpack.
Non-regression test for:
https://github.com/scikit-learn/scikit-learn/issues/27579
"""
pytest.importorskip("pandas")
X, _ = load_iris(as_frame=True, return_X_y=True)
with sklearn.config_context(transform_output="pandas"):
KernelPCA(n_components=2, eigen_solver="arpack").fit_transform(X)

0 comments on commit 9b977e9

Please sign in to comment.