Skip to content
Snippets Groups Projects
Commit 6a5d4c28 authored by Olivier Grisel's avatar Olivier Grisel
Browse files

make fast_svd deteriministc by default while allowing to pass rng seeds

parent 8fa07c39
No related branches found
No related tags found
No related merge requests found
......@@ -110,7 +110,7 @@ y_train, y_test = y[:split], y[split:]
################################################################################
# Compute a PCA (eigenfaces) on the training set
n_components = 100
n_components = 200
print "Extracting the top %d eigenfaces" % n_components
pca = PCA(n_comp=n_components, do_fast_svd=True).fit(X_train)
......
......@@ -86,7 +86,7 @@ def density(w, **kwargs):
return d
def fast_svd(M, k, p=10):
def fast_svd(M, k, p=10, rng=0):
"""Computes the k-truncated SVD using random projections
Parameters
......@@ -101,6 +101,9 @@ def fast_svd(M, k, p=10):
Additional number of samples of the range of M to ensure proper
conditioning. See the notes below.
rng: RandomState or an int seed (0 by default)
A random number generator instance to make behavior
Notes
=====
This algorithm finds the exact truncated singular values decomposition
......@@ -117,11 +120,16 @@ def fast_svd(M, k, p=10):
Halko, et al., 2009 (arXiv:909)
"""
if rng is None:
rng = np.random.RandomState()
elif isinstance(rng, int):
rng = np.random.RandomState(rng)
# lazy import of scipy sparse, because it is very slow.
from scipy import sparse
# generating random gaussian vectors r with shape: (M.shape[1], k + p)
r = np.random.normal(size=(M.shape[1], k + p))
r = rng.normal(size=(M.shape[1], k + p))
# sampling the range of M using by linear projection of r
if sparse.issparse(M):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment