Repository navigation
Conversation
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.
|
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: For now, we are closing this pull request.
|
Reference Issues/PRs
Fixes #35325
What does this implement/fix? Explain your changes.
KernelPCA.inverse_transformraised a bareAttributeError: 'KernelPCA' object has no attribute 'X_transformed_fit_'when called on an unfitted estimator withfit_inverse_transform=True. This violates the unfitted-estimator contract documented insklearn/utils/estimator_checks.py::check_estimators_unfitted("Unfitted estimators should raise a NotFittedError"), and the bareAttributeErroris not catchable withexcept NotFittedError(the idiomatic exception for this situation), becauseNotFittedErrorsubclassesAttributeError, not the other way around.The method escapes both existing safety nets:
check_estimators_unfittedonly coversdecision_function/predict-family methods, notinverse_transform;test_kernel_pca_raise_not_fitted_erroronly covers the fitted +fit_inverse_transform=Falsepath, so the unfitted path was untested (test asymmetry with the siblingTruncatedSVD.inverse_transform, which does callcheck_is_fitted(self)first,sklearn/decomposition/_truncated_svd.py:289).Minimal fix: add
check_is_fitted(self)at the beginning ofKernelPCA.inverse_transform(one production line), matching the sibling pattern, plus a regression testtest_kernel_pca_inverse_transform_unfittedcovering the unfitted path with a control assertion for the already-covered fitted path.Deliberately not changed: the
NotFittedErrorraised when the estimator is fitted butfit_inverse_transform=False(a parameter mistake reported asNotFittedError). Arguably it should be aValueError, 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_transformimplementations for test asymmetry.AI usage disclosure
I used AI assistance for:
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):sklearn/decomposition/tests/test_kernel_pca.py::test_kernel_pca_inverse_transform_unfitted):sklearn/decomposition/_kernel_pca.py:558(theself.X_transformed_fit_access).fit_inverse_transform=False→NotFittedError— passed before and after the change, so the red is attributable to the unfitted path, not test scaffolding.pytest sklearn/decomposition/tests/test_kernel_pca.py→39 passed, 1 skipped, 28 warnings in 1.06s(the skip is the optional-pandas test).ruff checkandruff format --check(pinnedruff==0.12.2from.pre-commit-config.yaml) → both pass on the two changed files.Notes for reviewer:
KernelPCA; the surface area is exactly the two files in the diff plus the changelog fragment.