Skip to content

Commit 9bd0aad

Browse files
committed
MISC renamed n_iterations to n_iter in all other places.
1 parent 0a81827 commit 9bd0aad

File tree

7 files changed

+44
-33
lines changed

7 files changed

+44
-33
lines changed

benchmarks/bench_plot_svd.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from sklearn.datasets.samples_generator import make_low_rank_matrix
1313

1414

15-
def compute_bench(samples_range, features_range, n_iterations=3, rank=50):
15+
def compute_bench(samples_range, features_range, n_iter=3, rank=50):
1616

1717
it = 0
1818

@@ -36,19 +36,19 @@ def compute_bench(samples_range, features_range, n_iterations=3, rank=50):
3636
results['scipy svd'].append(time() - tstart)
3737

3838
gc.collect()
39-
print "benching scikit-learn randomized_svd: n_iterations=0"
39+
print "benching scikit-learn randomized_svd: n_iter=0"
4040
tstart = time()
41-
randomized_svd(X, rank, n_iterations=0)
42-
results['scikit-learn randomized_svd (n_iterations=0)'].append(
41+
randomized_svd(X, rank, n_iter=0)
42+
results['scikit-learn randomized_svd (n_iter=0)'].append(
4343
time() - tstart)
4444

4545
gc.collect()
46-
print ("benching scikit-learn randomized_svd: n_iterations=%d "
47-
% n_iterations)
46+
print ("benching scikit-learn randomized_svd: n_iter=%d "
47+
% n_iter)
4848
tstart = time()
49-
randomized_svd(X, rank, n_iterations=n_iterations)
50-
results['scikit-learn randomized_svd (n_iterations=%d)'
51-
% n_iterations].append(time() - tstart)
49+
randomized_svd(X, rank, n_iter=n_iter)
50+
results['scikit-learn randomized_svd (n_iter=%d)'
51+
% n_iter].append(time() - tstart)
5252

5353
return results
5454

examples/applications/wikipedia_principal_eigenvector.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ def get_adjacency_matrix(redirects_filename, page_links_filename, limit=None):
172172

173173
print "Computing the principal singular vectors using randomized_svd"
174174
t0 = time()
175-
U, s, V = randomized_svd(X, 5, n_iterations=3)
175+
U, s, V = randomized_svd(X, 5, n_iter=3)
176176
print "done in %0.3fs" % (time() - t0)
177177

178178
# print the names of the wikipedia related strongest compenents of the the

examples/svm/plot_svm_scale_c.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@
128128
# reduce the variance
129129
grid = GridSearchCV(clf, refit=False, param_grid=param_grid,
130130
cv=ShuffleSplit(n=n_samples, train_size=train_size,
131-
n_iterations=250, random_state=1))
131+
n_iter=250, random_state=1))
132132
grid.fit(X, y)
133133
scores = [x[1] for x in grid.grid_scores_]
134134

sklearn/cluster/k_means_.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -897,7 +897,7 @@ def _mini_batch_step(X, x_squared_norms, centers, counts,
897897
return inertia, squared_diff
898898

899899

900-
def _mini_batch_convergence(model, iteration_idx, n_iterations, tol,
900+
def _mini_batch_convergence(model, iteration_idx, n_iter, tol,
901901
n_samples, centers_squared_diff, batch_inertia,
902902
context, verbose=0):
903903
"""Helper function to encapsulte the early stopping logic"""
@@ -926,7 +926,7 @@ def _mini_batch_convergence(model, iteration_idx, n_iterations, tol,
926926
progress_msg = (
927927
'Minibatch iteration %d/%d:'
928928
'mean batch inertia: %f, ewa inertia: %f ' % (
929-
iteration_idx + 1, n_iterations, batch_inertia,
929+
iteration_idx + 1, n_iter, batch_inertia,
930930
ewa_inertia))
931931
print progress_msg
932932

@@ -935,7 +935,7 @@ def _mini_batch_convergence(model, iteration_idx, n_iterations, tol,
935935
if tol > 0.0 and ewa_diff < tol:
936936
if verbose:
937937
print 'Converged (small centers change) at iteration %d/%d' % (
938-
iteration_idx + 1, n_iterations)
938+
iteration_idx + 1, n_iter)
939939
return True
940940

941941
# Early stopping heuristic due to lack of improvement on smoothed inertia
@@ -952,7 +952,7 @@ def _mini_batch_convergence(model, iteration_idx, n_iterations, tol,
952952
if verbose:
953953
print ('Converged (lack of improvement in inertia)'
954954
' at iteration %d/%d' % (
955-
iteration_idx + 1, n_iterations))
955+
iteration_idx + 1, n_iter))
956956
return True
957957

958958
# update the convergence context to maintain state across sucessive calls:
@@ -1102,7 +1102,7 @@ def fit(self, X, y=None):
11021102

11031103
distances = np.zeros(self.batch_size, dtype=np.float64)
11041104
n_batches = int(np.ceil(float(n_samples) / self.batch_size))
1105-
n_iterations = int(self.max_iter * n_batches)
1105+
n_iter = int(self.max_iter * n_batches)
11061106

11071107
init_size = self.init_size
11081108
if init_size is None:
@@ -1158,7 +1158,7 @@ def fit(self, X, y=None):
11581158

11591159
# Perform the iterative optimization untill the final convergence
11601160
# criterion
1161-
for iteration_idx in xrange(n_iterations):
1161+
for iteration_idx in xrange(n_iter):
11621162

11631163
# Sample the minibatch from the full dataset
11641164
minibatch_indices = self.random_state.random_integers(
@@ -1172,7 +1172,7 @@ def fit(self, X, y=None):
11721172

11731173
# Monitor the convergence and do early stopping if necessary
11741174
if _mini_batch_convergence(
1175-
self, iteration_idx, n_iterations, tol, n_samples,
1175+
self, iteration_idx, n_iter, tol, n_samples,
11761176
centers_squared_diff, batch_inertia, convergence_context,
11771177
verbose=self.verbose):
11781178
break

sklearn/decomposition/pca.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ def fit(self, X, y=None):
485485
n_components = self.n_components
486486

487487
U, S, V = randomized_svd(X, n_components,
488-
n_iterations=self.iterated_power,
488+
n_iter=self.iterated_power,
489489
random_state=self.random_state)
490490

491491
self.explained_variance_ = exp_var = (S ** 2) / n_samples

sklearn/utils/extmath.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# Authors: G. Varoquaux, A. Gramfort, A. Passos, O. Grisel
55
# License: BSD
66

7+
import warnings
78
import numpy as np
89
from scipy import linalg
910

@@ -78,7 +79,8 @@ def safe_sparse_dot(a, b, dense_output=False):
7879
return np.dot(a, b)
7980

8081

81-
def randomized_range_finder(A, size, n_iterations, random_state=None):
82+
def randomized_range_finder(A, size, n_iter, random_state=None,
83+
n_iterations=None):
8284
"""Computes an orthonormal matrix whose range approximates the range of A.
8385
8486
Parameters
@@ -87,7 +89,7 @@ def randomized_range_finder(A, size, n_iterations, random_state=None):
8789
The input data matrix
8890
size: integer
8991
Size of the return array
90-
n_iterations: integer
92+
n_iter: integer
9193
Number of power iterations used to stabilize the result
9294
random_state: RandomState or an int seed (0 by default)
9395
A random number generator instance
@@ -106,6 +108,10 @@ def randomized_range_finder(A, size, n_iterations, random_state=None):
106108
approximate matrix decompositions
107109
Halko, et al., 2009 (arXiv:909) http://arxiv.org/pdf/0909.4061
108110
"""
111+
if n_iterations is not None:
112+
warnings.warn("n_iterations was renamed to n_iter for consistency "
113+
"and will be removed in 0.16.", DeprecationWarning)
114+
n_iter = n_iterations
109115
random_state = check_random_state(random_state)
110116

111117
# generating random gaussian vectors r with shape: (A.shape[1], size)
@@ -117,16 +123,16 @@ def randomized_range_finder(A, size, n_iterations, random_state=None):
117123

118124
# perform power iterations with Y to further 'imprint' the top
119125
# singular vectors of A in Y
120-
for i in xrange(n_iterations):
126+
for i in xrange(n_iter):
121127
Y = safe_sparse_dot(A, safe_sparse_dot(A.T, Y))
122128

123129
# extracting an orthonormal basis of the A range samples
124130
Q, R = qr_economic(Y)
125131
return Q
126132

127133

128-
def randomized_svd(M, n_components, n_oversamples=10, n_iterations=0,
129-
transpose='auto', random_state=0):
134+
def randomized_svd(M, n_components, n_oversamples=10, n_iter=0,
135+
transpose='auto', random_state=0, n_iterations=None):
130136
"""Computes a truncated randomized SVD
131137
132138
Parameters
@@ -142,7 +148,7 @@ def randomized_svd(M, n_components, n_oversamples=10, n_iterations=0,
142148
to ensure proper conditioning. The total number of random vectors
143149
used to find the range of M is n_components + n_oversamples.
144150
145-
n_iterations: int (default is 0)
151+
n_iter: int (default is 0)
146152
Number of power iterations (can be used to deal with very noisy
147153
problems).
148154
@@ -172,6 +178,11 @@ def randomized_svd(M, n_components, n_oversamples=10, n_iterations=0,
172178
* A randomized algorithm for the decomposition of matrices
173179
Per-Gunnar Martinsson, Vladimir Rokhlin and Mark Tygert
174180
"""
181+
if n_iterations is not None:
182+
warnings.warn("n_iterations was renamed to n_iter for consistency "
183+
"and will be removed in 0.16.", DeprecationWarning)
184+
n_iter = n_iterations
185+
175186
random_state = check_random_state(random_state)
176187
n_random = n_components + n_oversamples
177188
n_samples, n_features = M.shape
@@ -182,7 +193,7 @@ def randomized_svd(M, n_components, n_oversamples=10, n_iterations=0,
182193
# this implementation is a bit faster with smaller shape[1]
183194
M = M.T
184195

185-
Q = randomized_range_finder(M, n_random, n_iterations, random_state)
196+
Q = randomized_range_finder(M, n_random, n_iter, random_state)
186197

187198
# project M to the (k + p) dimensional space using the basis vectors
188199
B = safe_sparse_dot(Q.T, M)

sklearn/utils/tests/test_svd.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,14 @@ def test_randomized_svd_low_rank_with_noise():
6969

7070
# compute the singular values of X using the fast approximate method
7171
# without the iterated power method
72-
_, sa, _ = randomized_svd(X, k, n_iterations=0)
72+
_, sa, _ = randomized_svd(X, k, n_iter=0)
7373

7474
# the approximation does not tolerate the noise:
7575
assert_greater(np.abs(s[:k] - sa).max(), 0.05)
7676

7777
# compute the singular values of X using the fast approximate method with
7878
# iterated power method
79-
_, sap, _ = randomized_svd(X, k, n_iterations=5)
79+
_, sap, _ = randomized_svd(X, k, n_iter=5)
8080

8181
# the iterated power method is helping getting rid of the noise:
8282
assert_almost_equal(s[:k], sap, decimal=3)
@@ -100,14 +100,14 @@ def test_randomized_svd_infinite_rank():
100100

101101
# compute the singular values of X using the fast approximate method
102102
# without the iterated power method
103-
_, sa, _ = randomized_svd(X, k, n_iterations=0)
103+
_, sa, _ = randomized_svd(X, k, n_iter=0)
104104

105105
# the approximation does not tolerate the noise:
106106
assert_greater(np.abs(s[:k] - sa).max(), 0.1)
107107

108108
# compute the singular values of X using the fast approximate method with
109109
# iterated power method
110-
_, sap, _ = randomized_svd(X, k, n_iterations=5)
110+
_, sap, _ = randomized_svd(X, k, n_iter=5)
111111

112112
# the iterated power method is still managing to get most of the structure
113113
# at the requested rank
@@ -125,11 +125,11 @@ def test_randomized_svd_transpose_consistency():
125125
effective_rank=rank, tail_strength=0.5, random_state=0)
126126
assert_equal(X.shape, (n_samples, n_features))
127127

128-
U1, s1, V1 = randomized_svd(X, k, n_iterations=3, transpose=False,
128+
U1, s1, V1 = randomized_svd(X, k, n_iter=3, transpose=False,
129129
random_state=0)
130-
U2, s2, V2 = randomized_svd(X, k, n_iterations=3, transpose=True,
130+
U2, s2, V2 = randomized_svd(X, k, n_iter=3, transpose=True,
131131
random_state=0)
132-
U3, s3, V3 = randomized_svd(X, k, n_iterations=3, transpose='auto',
132+
U3, s3, V3 = randomized_svd(X, k, n_iter=3, transpose='auto',
133133
random_state=0)
134134
U4, s4, V4 = linalg.svd(X, full_matrices=False)
135135

0 commit comments

Comments
 (0)