Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2c2ee2d

Browse files
authored
API Deprecate n_feature_ fitted attribute in PCA (#24421)
1 parent 20bb279 commit 2c2ee2d

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

doc/whats_new/v1.2.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,11 @@ Changelog
192192
:pr:`11860` by :user:`Pierre Ablin <pierreablin>`,
193193
:pr:`22527` by :user:`Meekail Zain <micky774>` and `Thomas Fan`_.
194194

195+
- |API| The `n_features_` attribute of
196+
:class:`decomposition.PCA` is deprecated in favor of
197+
`n_features_in_` and will be removed in 1.4. :pr:`24421` by
198+
:user:`Kshitij Mathur <Kshitij68>`.
199+
195200
:mod:`sklearn.discriminant_analysis`
196201
....................................
197202

sklearn/decomposition/_pca.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from ._base import _BasePCA
2323
from ..utils import check_random_state
2424
from ..utils._arpack import _init_arpack_v0
25+
from ..utils.deprecation import deprecated
2526
from ..utils.extmath import fast_logdet, randomized_svd, svd_flip
2627
from ..utils.extmath import stable_cumsum
2728
from ..utils.validation import check_is_fitted
@@ -402,6 +403,16 @@ def __init__(
402403
self.power_iteration_normalizer = power_iteration_normalizer
403404
self.random_state = random_state
404405

406+
# TODO(1.4): remove in 1.4
407+
# mypy error: Decorated property not supported
408+
@deprecated( # type: ignore
409+
"Attribute `n_features_` was deprecated in version 1.2 and will be "
410+
"removed in 1.4. Use `n_features_in_` instead."
411+
)
412+
@property
413+
def n_features_(self):
414+
return self.n_features_in_
415+
405416
def fit(self, X, y=None):
406417
"""Fit the model with X.
407418
@@ -552,7 +563,7 @@ def _fit_full(self, X, n_components):
552563
else:
553564
self.noise_variance_ = 0.0
554565

555-
self.n_samples_, self.n_features_ = n_samples, n_features
566+
self.n_samples_ = n_samples
556567
self.components_ = components_[:n_components]
557568
self.n_components_ = n_components
558569
self.explained_variance_ = explained_variance_[:n_components]
@@ -614,7 +625,7 @@ def _fit_truncated(self, X, n_components, svd_solver):
614625
random_state=random_state,
615626
)
616627

617-
self.n_samples_, self.n_features_ = n_samples, n_features
628+
self.n_samples_ = n_samples
618629
self.components_ = Vt
619630
self.n_components_ = n_components
620631

sklearn/decomposition/tests/test_sparse_pca.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,13 @@ def test_spca_n_iter_deprecation():
289289
assert model.n_iter_ <= max_iter
290290

291291

292+
def test_pca_n_features_deprecation():
293+
X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]])
294+
pca = PCA(n_components=2).fit(X)
295+
with pytest.warns(FutureWarning, match="`n_features_` was deprecated"):
296+
pca.n_features_
297+
298+
292299
def test_spca_early_stopping(global_random_seed):
293300
"""Check that `tol` and `max_no_improvement` act as early stopping."""
294301
rng = np.random.RandomState(global_random_seed)

0 commit comments

Comments
 (0)