8000 ENH Added n_components_ to SparsePCA and MiniBatchSparsePCA (#16981) · scikit-learn/scikit-learn@41b18fe · GitHub
[go: up one dir, main page]

Skip to content

Commit 41b18fe

Browse files
ReksbrilMateusz Górskithomasjpfan
authored
ENH Added n_components_ to SparsePCA and MiniBatchSparsePCA (#16981)
* Add n_components_ attribute to SparcePCA and MiniBatchSparsePCA * Add n_components_ attribute to SparcePCA and MiniBatchSparsePCA * Add whatsnew entry * Fix documentation * Apply suggestions from code review Co-Authored-By: Thomas J Fan <thomasjpfan@gmail.com> * Add suggested changes * Update test_sparse_pca.py Co-authored-by: Mateusz Górski <mateuszg122@gmail.com> Co-authored-by: Thomas J Fan <thomasjpfan@gmail.com>
1 parent 5b2c931 commit 41b18fe

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

doc/whats_new/v0.23.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,9 @@ Changelog
215215
raise `invalid value encountered in multiply` during `fit`.
216216
:pr:`16718` by :user:`Gui Miotto <gui-miotto>`.
217217

218+
- |Feature| Added `n_components_` attribute to :class:'decomposition.SparsePCA'
219+
and :class:'MiniBatchSparsePCA'. :pr:'16981' by :user:'Mateusz Górski <Reksbril>'
220+
218221
:mod:`sklearn.ensemble`
219222
.......................
220223

sklearn/decomposition/_sparse_pca.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ class SparsePCA(TransformerMixin, BaseEstimator):
103103
error_ : array
104104
Vector of errors at each iteration.
105105
106+
n_components_ : int
107+
Estimated number of components.
108+
109+
.. versionadded:: 0.23
110+
106111
n_iter_ : int
107112
Number of iterations run.
108113
@@ -197,6 +202,7 @@ def fit(self, X, y=None):
197202
self.components_, axis=1)[:, np.newaxis]
198203
components_norm[components_norm == 0] = 1
199204
self.components_ /= components_norm
205+
self.n_components_ = len(self.components_)
200206

201207
self.error_ = E
202208
return self
@@ -312,6 +318,11 @@ class MiniBatchSparsePCA(SparsePCA):
312318
components_ : array, [n_components, n_features]
313319
Sparse components extracted from the data.
314320
321+
n_components_ : int
322+
Estimated number of components.
323+
324+
.. versionadded:: 0.23
325+
315326
n_iter_ : int
316327
Number of iterations run.
317328
@@ -403,5 +414,6 @@ def fit(self, X, y=None):
403414
self.components_, axis=1)[:, np.newaxis]
404415
components_norm[components_norm == 0] = 1
405416
self.components_ /= components_norm
417+
self.n_components_ = len(self.components_)
406418

407419
return self

sklearn/decomposition/tests/test_sparse_pca.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,3 +207,18 @@ def test_spca_error_unormalized_components(spca):
207207
err_msg = "normalize_components=False is not supported starting "
208208
with pytest.raises(NotImplementedError, match=err_msg):
209209
spca(normalize_components=False).fit(Y)
210+
211+
212+
@pytest.mark.parametrize("SPCA", [SparsePCA, MiniBatchSparsePCA])
213+
@pytest.mark.parametrize("n_components", [None, 3])
214+
def test_spca_n_components_(SPCA, n_components):
215+
rng = np.random.RandomState(0)
216+
n_samples, n_features = 12, 10
217+
X = rng.randn(n_samples, n_features)
218+
219+
model = SPCA(n_components=n_components).fit(X)
220+
221+
if n_components is not None:
222+
assert model.n_components_ == n_components
223+
else:
224+
assert model.n_components_ == n_features

0 commit comments

Comments
 (0)
0