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

Skip to content

[MRG] Add check for n_components in pca #10042

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

Merged
merged 6 commits into from
Nov 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions sklearn/decomposition/pca.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# License: BSD 3 clause

from math import log, sqrt
import numbers

import numpy as np
from scipy import linalg
Expand Down Expand Up @@ -417,6 +418,12 @@ def _fit_full(self, X, n_components):
"min(n_samples, n_features)=%r with "
"svd_solver='full'"
% (n_components, min(n_samples, n_features)))
elif n_components >= 1:
if not isinstance(n_components, (numbers.Integral, np.integer)):
raise ValueError("n_components=%r must be of type int "
"when greater than or equal to 1, "
"was of type=%r"
% (n_components, type(n_components)))

# Center data
self.mean_ = np.mean(X, axis=0)
Expand Down Expand Up @@ -477,6 +484,10 @@ def _fit_truncated(self, X, n_components, svd_solver):
"svd_solver='%s'"
% (n_components, min(n_samples, n_features),
svd_solver))
elif not isinstance(n_components, (numbers.Integral, np.integer)):
raise ValueError("n_components=%r must be of type int "
"when greater than or equal to 1, was of type=%r"
% (n_components, type(n_components)))
elif svd_solver == 'arpack' and n_components == min(n_samples,
n_features):
raise ValueError("n_components=%r must be strictly less than "
Expand Down
10 changes: 10 additions & 0 deletions sklearn/decomposition/tests/test_pca.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from sklearn.utils.testing import assert_greater
from sklearn.utils.testing import assert_raises
from sklearn.utils.testing import assert_raises_regex
from sklearn.utils.testing import assert_raise_message
from sklearn.utils.testing import assert_no_warnings
from sklearn.utils.testing import assert_warns_message
from sklearn.utils.testing import ignore_warnings
Expand Down Expand Up @@ -389,6 +390,15 @@ def test_pca_validation():
PCA(n_components, svd_solver=solver)
.fit, data)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When constructing test in PCA, it is common to use for solver in solver_list: to loop through all the possible svd_solvers. There's already a loop above your code so it might be better to move your test into the loop to make sure that the code works for all svd_solvers.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ye, I will do it since @jnothman also suggested it

n_components = 1.0
type_ncom = type(n_components)
assert_raise_message(ValueError,
"n_components={} must be of type int "
"when greater than or equal to 1, was of type={}"
.format(n_components, type_ncom),
PCA(n_components, svd_solver=solver).fit, data)



def test_n_components_none():
# Ensures that n_components == None is handled correctly
Expand Down