-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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
[FIX] pca: n_features_ attribute of decomposition.PCA is deprecated in favor of n_features_in_ #6249
Merged
markotoplak
merged 1 commit into
biolab:master
from
JakaKokosar:scikit-learn-api-change
Apr 21, 2023
Merged
[FIX] pca: n_features_ attribute of decomposition.PCA is deprecated in favor of n_features_in_ #6249
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this works because:
Most of the code in
ImprovedPCA
is copied directly fromsklearn.decomposition.PCA
. This was done because it wasn't possible to add the randomized algorithm that enables PCA on sparse data without modifying these functions.Until recently, sklearn's PCA used
self.n_features_
. However, they recently switched over to usingself.n_features_in
, which is more generally used in sklearn, and is part of theirBaseEstimator
class. Theself.n_features_in
attribute is set inBaseEstimator._check_n_features
, which is, in turn, called byBaseEstimator._validate_data
. In scikit-learn's PCA, this method is called inPCA._fit
, which ensures that theself.n_features_in
attribute is set on sklearn's implementation.They also deprecated
self.n_features
and replaced it with a readonly property which just returnsself.n_featuers_in
anyways. This is still the same functionality as before, but you have to trace it through these different methods. So, this is still backwards compatible.So, we can do the same thing in our
ImprovedPCA
class. We can get rid ofself.n_features_ = features
, since this will be set when we callself._validate_data
, which is inherited fromsklearn.decomposition.PCA <- sklearn.BaseEstimator
. So, the other change we need is to replace our previous call tocheck_array
toself._validate_data
. And I believe this should ensure the exact same functionality as before.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
transform
method should also probably be updated to reflect how sklearn is doing things now.So, this would mean changing
on lines 224-230 to
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pavlin-policar, thanks for the explanation.