8000 change default iterated_power to auto. · scikit-learn/scikit-learn@970ace9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 970ace9

Browse files
committed
change default iterated_power to auto.
1 parent 5404197 commit 970ace9

File tree

3 files changed

+16
-22
lines changed

3 files changed

+16
-22
lines changed

sklearn/decomposition/pca.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ class PCA(_BasePCA):
166166
167167
.. versionadded:: 0.18.0
168168
169-
iterated_power : int >= 0, optional (default 4)
169+
iterated_power : int >= 0, or 'auto', (default 'auto')
170170
Number of iterations for the power method computed by
171171
svd_solver == 'randomized'.
172172
@@ -240,21 +240,21 @@ class PCA(_BasePCA):
240240
>>> X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]])
241241
>>> pca = PCA(n_components=2)
242242
>>> pca.fit(X)
243-
PCA(copy=True, iterated_power=4, n_components=2, random_state=None,
243+
PCA(copy=True, iterated_power='auto', n_components=2, random_state=None,
244244
svd_solver='auto', tol=0.0, whiten=False)
245245
>>> print(pca.explained_variance_ratio_) # doctest: +ELLIPSIS
246246
[ 0.99244... 0.00755...]
247247
248248
>>> pca = PCA(n_components=2, svd_solver='full')
249249
>>> pca.fit(X) # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
250-
PCA(copy=True, iterated_power=4, n_components=2, random_state=None,
250+
PCA(copy=True, iterated_power='auto', n_components=2, random_state=None,
251251
svd_solver='full', tol=0.0, whiten=False)
252252
>>> print(pca.explained_variance_ratio_) # doctest: +ELLIPSIS
253253
[ 0.99244... 0.00755...]
254254
255255
>>> pca = PCA(n_components=1, svd_solver='arpack')
256256
>>> pca.fit(X)
257-
PCA(copy=True, iterated_power=4, n_components=1, random_state=None,
257+
PCA(copy=True, iterated_power='auto', n_components=1, random_state=None,
258258
svd_solver='arpack', tol=0.0, whiten=False)
259259
>>> print(pca.explained_variance_ratio_) # doctest: +ELLIPSIS
260260
[ 0.99244...]
@@ -268,7 +268,7 @@ class PCA(_BasePCA):
268268
"""
269269

270270
def __init__(self, n_components=None, copy=True, whiten=False,
271-
svd_solver='auto', tol=0.0, iterated_power=4,
271+
svd_solver='auto', tol=0.0, iterated_power='auto',
272272
random_state=None):
273273
self.n_components = n_components
274274
self.copy = copy
@@ -535,8 +535,8 @@ class RandomizedPCA(BaseEstimator, TransformerMixin):
535535
fit(X).transform(X) will not yield the expected results,
536536
use fit_transform(X) instead.
537537
538-
iterated_power : int, optional
539-
Number of iterations for the power method. 2 by default.
538+
iterated_power : int, default=2
539+
Number of iterations for the power method.
540540
541541
.. versionchanged:: 0.18
542542

sklearn/pipeline.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,7 @@ def make_union(*transformers):
796796
>>> make_union(PCA(), TruncatedSVD()) # doctest: +NORMALIZE_WHITESPACE
797797
FeatureUnion(n_jobs=1,
798798
transformer_list=[('pca',
799-
PCA(copy=True, iterated_power=4,
799+
PCA(copy=True, iterated_power='auto',
800800
n_components=None, random_state=None,
801801
svd_solver='auto', tol=0.0, whiten=False)),
802802
('truncatedsvd',

sklearn/utils/extmath.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
from ..externals.six.moves import xrange
2626
from .sparsefuncs_fast import csr_row_norms
2727
from .validation import check_array
28-
from ..exceptions import NonBLASDotWarning, ChangedBehaviorWarning
28+
from ..exceptions import NonBLASDotWarning
2929

3030

3131
def norm(x):
@@ -267,7 +267,7 @@ def randomized_range_finder(A, size, n_iter,
267267
return Q
268268

269269

270-
def randomized_svd(M, n_components, n_oversamples=10, n_iter=None,
270+
def randomized_svd(M, n_components, n_oversamples=10, n_iter='auto',
271271
power_iteration_normalizer='auto', transpose='auto',
272272
flip_sign=True, random_state=0):
273273
"""Computes a truncated randomized SVD
@@ -287,11 +287,11 @@ def randomized_svd(M, n_components, n_oversamples=10, n_iter=None,
287287
number can improve speed but can negatively impact the quality of
288288
approximation of singular vectors and singular values.
289289
290-
n_iter: int (default is 4)
290+
n_iter: int or 'auto' (default is 'auto')
291291
Number of power iterations. It can be used to deal with very noisy
292-
problems. When `n_components` is small (< .1 * min(X.shape)) `n_iter`
293-
is set to 7, unless the user specifies a higher number. This improves
294-
precision with few components.
292+
problems. When 'auto', it is set to 4, unless `n_components` is small
293+
(< .1 * min(X.shape)) `n_iter` in which case is set to 7.
294+
This improves precision with few components.
295295
296296
.. versionchanged:: 0.18
297297
@@ -349,16 +349,10 @@ def randomized_svd(M, n_components, n_oversamples=10, n_iter=None,
349349
n_random = n_components + n_oversamples
350350
n_samples, n_features = M.shape
351351

352-
if n_iter is None:
352+
if n_iter is 'auto':
353353
# Checks if the number of iterations is explicitely specified
354354
# Adjust n_iter. 7 was found a good compromise for PCA. See #5299
355-
if n_components < .1 * min(M.shape) and n_iter < 7:
356-
n_iter = 7
357-
warnings.warn("The default number of power iterations is increased from 4"
358-
"to 7 in version 0.18 to achieve higher precision.",
359-
ChangedBehaviorWarning)
360-
else:
361-
n_iter = 4
355+
n_iter = 7 if n_components < .1 * min(M.shape) else 4
362356

363357
if transpose == 'auto':
364358
transpose = n_samples < n_features

0 commit comments

Comments
 (0)
0