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

Skip to content

Commit f309126

Browse files
committed
FIX PCA error handling for invalid n_components
n_components < 0 was silently handled like None; > n_features gave a cryptic error message from the bowels of np.linalg. XXX n_components == 0 is still allowed as changing it makes tests fail, not sure what it means.
1 parent 1c7e37e commit f309126

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

sklearn/decomposition/pca.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,9 @@ def _fit(self, X):
292292

293293
n_components = _infer_dimension_(explained_variance_,
294294
n_samples, n_features)
295+
elif not 0 <= n_components <= n_features:
296+
raise ValueError("n_components=%r invalid for n_features=%d"
297+
% (n_components, n_features))
295298

296299
if 0 < n_components < 1.0:
297300
# number of components for which the cumulated explained variance

sklearn/decomposition/tests/test_pca.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from sklearn.utils.testing import assert_true
77
from sklearn.utils.testing import assert_equal
88
from sklearn.utils.testing import assert_greater
9+
from sklearn.utils.testing import assert_raises
910
from sklearn.utils.testing import assert_warns
1011

1112
from sklearn import datasets
@@ -165,6 +166,12 @@ def test_pca_inverse():
165166
assert_almost_equal(relative_max_delta, 0.11, decimal=2)
166167

167168

169+
def test_pca_validation():
170+
X = [[0, 1], [1, 0]]
171+
for n_components in [-1, 3]:
172+
assert_raises(ValueError, PCA(n_components).fit, X)
173+
174+
168175
def test_randomized_pca_check_projection():
169176
"""Test that the projection by RandomizedPCA on dense data is correct"""
170177
rng = np.random.RandomState(0)

0 commit comments

Comments
 (0)