Fix get_feature_names_out raising AttributeError on fitted estimators - #246
Open
LukeTheoJohnson wants to merge 2 commits into
Open
Fix get_feature_names_out raising AttributeError on fitted estimators#246LukeTheoJohnson wants to merge 2 commits into
LukeTheoJohnson wants to merge 2 commits into
Conversation
PCA.get_feature_names_out and MCA.get_feature_names_out return np.arange(self.n_components_), but n_components_ was only declared as a type annotation (added to make ty pass) and never assigned during fit. Calling the method on any fitted PCA, MCA, or FAMD estimator raised AttributeError: object has no attribute 'n_components_', breaking the scikit-learn transformer contract that Pipeline.get_feature_names_out and ColumnTransformer rely on. fit now sets n_components_ to the effective number of fitted components (len(svd_.s)). This equals the width of the transform output in every path, including the rank-capped CA/MCA SVD where fewer components are kept than requested. Adds a regression test for PCA, MCA, and FAMD.
LukeTheoJohnson
marked this pull request as ready for review
July 11, 2026 00:23
Owner
|
Cheers! I'll take a look in a couple of weeks after my holidays. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Calling
get_feature_names_out()on a fittedPCA,MCA, orFAMDestimator raises:This also breaks
Pipeline.get_feature_names_out()andColumnTransformer, which delegates to this method, so a pipeline containing a prince estimator fails the same way.Cause
get_feature_names_outreturnsnp.arange(self.n_components_), but there's nothing infitthat assignsn_components_. It just exists as a class-level annotation, so the type checker stays green while the attribute is missing at runtime.Fix
fitnow setsself.n_components_ = len(self.svd_.s)inPCA.fitandMCA.fit(FAMDinherits). Works in the paths I traced (including the rank capped CA/MCA SVD where fewer components are kept than requested) and matches the width of thetransformoutput. Happy to adjust if there's a case I've missed.Tests
One regression test per estimator, asserting
len(get_feature_names_out()) == transform(X).shape[1]. All three raise theAttributeErroron master and pass with this change.ruff check,ruff format --check, andty check princeare clean.