|
9 | 9 | # Stefan van der Walt |
10 | 10 | # Kyle Kastner |
11 | 11 | # Giorgio Patrini |
| 12 | +# Andrew Knyazev added lobpcg |
12 | 13 | # License: BSD 3 clause |
13 | 14 |
|
14 | 15 | from __future__ import division |
|
26 | 27 | from .validation import check_array |
27 | 28 | from scipy.sparse.linalg import lobpcg |
28 | 29 |
|
| 30 | + |
29 | 31 | @deprecated("sklearn.utils.extmath.norm was deprecated in version 0.19 " |
30 | 32 | "and will be removed in 0.21. Use scipy.linalg.norm instead.") |
31 | 33 | def norm(x): |
@@ -241,7 +243,7 @@ def randomized_range_finder(A, size, n_iter, |
241 | 243 |
|
242 | 244 | # Perform power iterations with Q to further 'imprint' the top |
243 | 245 | # singular vectors of A in Q |
244 | | - for i in range(n_iter): |
| 246 | + for _ in range(n_iter): |
245 | 247 | if power_iteration_normalizer == 'none': |
246 | 248 | Q = safe_sparse_dot(A, Q) |
247 | 249 | Q = safe_sparse_dot(A.T, Q) |
@@ -387,9 +389,10 @@ def randomized_svd(M, n_components, n_oversamples=10, n_iter='auto', |
387 | 389 | else: |
388 | 390 | return U[:, :n_components], s[:n_components], V[:n_components, :] |
389 | 391 |
|
| 392 | + |
390 | 393 | def lobpcg_svd(M, n_components, n_oversamples=10, n_iter='auto', |
391 | | - transpose='auto', lobpcg_tol=None, |
392 | | - flip_sign=True, random_state=0): |
| 394 | + transpose='auto', lobpcg_tol=None, |
| 395 | + flip_sign=True, random_state=0): |
393 | 396 | """Computes a truncated SVD using LOBPCG mimicking the randomized SVD setup |
394 | 397 |
|
395 | 398 | Parameters |
@@ -436,14 +439,14 @@ def lobpcg_svd(M, n_components, n_oversamples=10, n_iter='auto', |
436 | 439 | Notes |
437 | 440 | ----- |
438 | 441 | This algorithm finds a (usually very good) approximate truncated |
439 | | - singular value decomposition using LOBPCG with randomization to speed up |
| 442 | + singular value decomposition using LOBPCG with randomization to speed up |
440 | 443 | the computations. It is particularly fast on large matrices on which |
441 | 444 | you wish to extract only a small number of components. In order to |
442 | 445 | obtain further speed up, `n_iter` can be set <=2 (at the cost of |
443 | | - loss of precision). Compared to 'ranomised', the 'lobpcg' option gives |
444 | | - more accurate approximations, with the same n_iter, n_components, and |
445 | | - n_oversamples, at the slightly increased costs, allows setting |
446 | | - the tolerance, and can output the accuracy. |
| 446 | + loss of precision). Compared to 'ranomised', the 'lobpcg' option gives |
| 447 | + more accurate approximations, with the same n_iter, n_components, and |
| 448 | + n_oversamples, at the slightly increased costs, allows setting |
| 449 | + the tolerance, and can output the accuracy. |
447 | 450 |
|
448 | 451 | References |
449 | 452 | ---------- |
@@ -475,16 +478,16 @@ def lobpcg_svd(M, n_components, n_oversamples=10, n_iter='auto', |
475 | 478 | if transpose: |
476 | 479 | # this implementation is a bit faster with smaller shape[1] |
477 | 480 | M = M.T |
478 | | - |
| 481 | + |
479 | 482 | Q = random_state.normal(size=(M.shape[0], n_random)) |
480 | 483 | if M.dtype.kind == 'f': |
481 | 484 | # Ensure f32 is preserved as f32 |
482 | 485 | Q = Q.astype(M.dtype, copy=False) |
483 | 486 |
|
484 | 487 | A = - safe_sparse_dot(M, M.T) |
485 | | - # LOBPCG default option largest=True is currently broken, so we go the |
| 488 | + # LOBPCG default option largest=True is currently broken, so we go the |
486 | 489 | # smallest (negative) of the negative normal matrix A |
487 | | - lambdas, Q = lobpcg(A, Q, tol=lobpcg_tol, maxiter=n_iter, largest=False) |
| 490 | + _, Q = lobpcg(A, Q, tol=lobpcg_tol, maxiter=n_iter, largest=False) |
488 | 491 |
|
489 | 492 | # project M to the (k + p) dimensional space using the basis vectors |
490 | 493 | # project M to the (k + p) dimensional space using the basis vectors |
@@ -652,7 +655,7 @@ def cartesian(arrays, out=None): |
652 | 655 | if out is None: |
653 | 656 | out = np.empty_like(ix, dtype=dtype) |
654 | 657 |
|
655 | | - for n, arr in enumerate(arrays): |
| 658 | + for n, _ in enumerate(arrays): |
656 | 659 | out[:, n] = arrays[n][ix[:, n]] |
657 | 660 |
|
658 | 661 | return out |
|
0 commit comments