8000 FIX check that parameters validation happen in fit for KernelPCA by MaggieChege · Pull Request #21567 · scikit-learn/scikit-learn · GitHub
[go: up one dir, main page]

Skip to content

FIX check that parameters validation happen in fit for KernelPCA #21567

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 4 commits into from
Nov 15, 2021
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
7 changes: 7 additions & 0 deletions doc/whats_new/v1.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,13 @@ Changelog
instead of `__init__`.
:pr:`21434` by :user:`Krum Arnaudov <krumeto>`.


:mod:`sklearn.decomposition.KernelPCA`
......................................
- |Fix| :class:`decomposition.KernelPCA` now validates input parameters in
`fit` instead of `__init__`.
:pr:`21567` by :user:`Maggie Chege <MaggieChege>`.

:mod:`sklearn.svm`
..................

Expand Down
4 changes: 2 additions & 2 deletions sklearn/decomposition/_kernel_pca.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,6 @@ def __init__(
copy_X=True,
n_jobs=None,
):
if fit_inverse_transform and kernel == "precomputed":
raise ValueError("Cannot fit_inverse_transform with a precomputed kernel.")
self.n_components = n_components
self.kernel = kernel
self.kernel_params = kernel_params
Expand Down Expand Up @@ -429,6 +427,8 @@ def fit(self, X, y=None):
self : object
Returns the instance itself.
"""
if self.fit_inverse_transform and self.kernel == "precomputed":
raise ValueError("Cannot fit_inverse_transform with a precomputed kernel.")
X = self._validate_data(X, accept_sparse="csr", copy=self.copy_X)
self._centerer = KernelCenterer()
K = self._get_kernel(X)
Expand Down
8 changes: 6 additions & 2 deletions sklearn/decomposition/tests/test_kernel_pca.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,12 @@ def test_kernel_pca_invalid_parameters():
Tests fitting inverse transform with a precomputed kernel raises a
ValueError.
"""
with pytest.raises(ValueError):
KernelPCA(10, fit_inverse_transform=True, kernel="precomputed")
estimator = KernelPCA(
n_components=10, fit_inverse_transform=True, kernel="precomputed"
)
err_ms = "Cannot fit_inverse_transform with a precomputed kernel"
with pytest.raises(ValueError, match=err_ms):
estimator.fit(np.random.randn(10, 10))


def test_kernel_pca_consistent_transform():
Expand Down
1 change: 0 additions & 1 deletion sklearn/tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,6 @@ def test_transformers_get_feature_names_out(transformer):
"FeatureUnion",
"GridSearchCV",
"HalvingGridSearchCV",
"KernelPCA",
"Pipeline",
"SGDOneClassSVM",
"TheilSenRegressor",
Expand Down
0