From 43cf1a820cea8d5b3218627d047dd78e4a152dd4 Mon Sep 17 00:00:00 2001 From: Leland McInnes Date: Sun, 7 Oct 2018 22:17:58 -0400 Subject: [PATCH] LOBPCG not so good for smaller matrices. --- umap/spectral.py | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/umap/spectral.py b/umap/spectral.py index 19c5efd6..c82d68de 100644 --- a/umap/spectral.py +++ b/umap/spectral.py @@ -254,21 +254,23 @@ def spectral_layout(data, graph, dim, random_state, metric="euclidean", metric_k k = dim + 1 num_lanczos_vectors = max(2 * k + 1, int(np.sqrt(graph.shape[0]))) try: - # eigenvalues, eigenvectors = scipy.sparse.linalg.eigsh( - # L, - # k, - # which="SM", - # ncv=num_lanczos_vectors, - # tol=1e-4, - # v0=np.ones(L.shape[0]), - # maxiter=graph.shape[0] * 5, - # ) - eigenvalues, eigenvectors = scipy.sparse.linalg.lobpcg( - L, - random_state.normal(size=(L.shape[0], k)), - largest=False, - tol=1e-4 - ) + if L.shape[0] < 2000000: + eigenvalues, eigenvectors = scipy.sparse.linalg.eigsh( + L, + k, + which="SM", + ncv=num_lanczos_vectors, + tol=1e-4, + v0=np.ones(L.shape[0]), + maxiter=graph.shape[0] * 5, + ) + else: + eigenvalues, eigenvectors = scipy.sparse.linalg.lobpcg( + L, + random_state.normal(size=(L.shape[0], k)), + largest=False, + tol=1e-8 + ) order = np.argsort(eigenvalues)[1:k] return eigenvectors[:, order] except scipy.sparse.linalg.ArpackError: