Skip to content

FIX raise NotFittedError in KernelPCA.inverse_transform when unfitted - #35326

Closed
lau0708 wants to merge 2 commits into
scikit-learn:mainfrom
lau0708:fix/kernel-pca-inverse-transform-notfitted
Closed

lau0708 wants to merge 2 commits into
scikit-learn:mainfrom
lau0708:fix/kernel-pca-inverse-transform-notfitted

Conversation

@lau0708

@lau0708 lau0708 commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

Reference Issues/PRs

Fixes #35325

What does this implement/fix? Explain your changes.

KernelPCA.inverse_transform raised a bare AttributeError: 'KernelPCA' object has no attribute 'X_transformed_fit_' when called on an unfitted estimator with fit_inverse_transform=True. This violates the unfitted-estimator contract documented in sklearn/utils/estimator_checks.py::check_estimators_unfitted ("Unfitted estimators should raise a NotFittedError"), and the bare AttributeError is not catchable with except NotFittedError (the idiomatic exception for this situation), because NotFittedError subclasses AttributeError, not the other way around.

The method escapes both existing safety nets:

  • check_estimators_unfitted only covers decision_function/predict-family methods, not inverse_transform;
  • the existing test test_kernel_pca_raise_not_fitted_error only covers the fitted + fit_inverse_transform=False path, so the unfitted path was untested (test asymmetry with the sibling TruncatedSVD.inverse_transform, which does call check_is_fitted(self) first, sklearn/decomposition/_truncated_svd.py:289).

Minimal fix: add check_is_fitted(self) at the beginning of KernelPCA.inverse_transform (one production line), matching the sibling pattern, plus a regression test test_kernel_pca_inverse_transform_unfitted covering the unfitted path with a control assertion for the already-covered fitted path.

Deliberately not changed: the NotFittedError raised when the estimator is fitted but fit_inverse_transform=False (a parameter mistake reported as NotFittedError). Arguably it should be a ValueError, but that changes an exception type long-standing user code may rely on, so I left it out of scope and flagged it in #35325 for maintainer decision.

Introduce yourself

I'm lau0708, a long-time scikit-learn user; this is a small contract-compliance fix found by auditing inverse_transform implementations for test asymmetry.

AI usage disclosure

I used AI assistance for:

  • Code generation (e.g., when writing an implementation or fixing a bug)
  • Test/benchmark generation
  • Research and understanding

All of the above (fix, test, issue text) was drafted with AI assistance and then verified by me locally: the failing test was run against a clean main (71659d6) build to confirm it fails for the stated reason, and re-run after the fix; lint was run with the pinned ruff version.

Any other comments?

Bug fix verification (local build of main @ 71659d6, macOS, Python 3.13.12):

  • Red on clean base (sklearn/decomposition/tests/test_kernel_pca.py::test_kernel_pca_inverse_transform_unfitted):
    AttributeError: 'KernelPCA' object has no attribute 'X_transformed_fit_'
    
    at sklearn/decomposition/_kernel_pca.py:558 (the self.X_transformed_fit_ access).
  • Control assertion in the same test — fitted + fit_inverse_transform=False → NotFittedError — passed before and after the change, so the red is attributable to the unfitted path, not test scaffolding.
  • Green after the fix: full target test file pytest sklearn/decomposition/tests/test_kernel_pca.py → 39 passed, 1 skipped, 28 warnings in 1.06s (the skip is the optional-pandas test).
  • Lint, verbatim as CI: ruff check and ruff format --check (pinned ruff==0.12.2 from .pre-commit-config.yaml) → both pass on the two changed files.

Notes for reviewer:

  • None of the PR check boxes for affected areas fits a one-line fitted-check addition in KernelPCA; the surface area is exactly the two files in the diff plus the changelog fragment.
  • The changelog fragment is named with this PR's number and will be pushed as a follow-up commit right after the PR number is known.

KernelPCA.inverse_transform raised a bare AttributeError when called on
an unfitted estimator with fit_inverse_transform=True, because it
accessed X_transformed_fit_ without a fitted check. NotFittedError is
subclassed by AttributeError, so user code catching NotFittedError could
not catch it. Sibling estimators (e.g. TruncatedSVD.inverse_transform)
call check_is_fitted first; add the same check.
@github-actions github-actions Bot added the size/S label Nov 6, 2026
@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown

Thank you for your interest in contributing to scikit-learn.

The linked issue is still under discussion, and the maintainers have not yet reached consensus on how it should be resolved. Before opening a pull request, please review the "Issues tagged 'Needs Triage'" section of the Contributing Guide:
https://scikit-learn.org/stable/developers/contributing.html#issues-tagged-needs-triage

For now, we are closing this pull request.

  • If you believe your proposed change addresses the linked issue, please explain your proposal in that issue, and allow time for discussion with the maintainers so that consensus can be reached before implementation begins.

  • If you believe this is a mistake, please leave a comment on the linked issue as well.

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

Labels

linked issue not ready PR opened before its linked issue reached consensus module:decomposition size/S

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: KernelPCA.inverse_transform raises bare AttributeError instead of NotFittedError when unfitted

1 participant