def test_fast_svd(): n_samples = 100 k = 10 rng = np.random.RandomState(42) # We need to use n_features > 500 to trigger the randomized_svd for n_features in (30, 100, 550): # generate a matrix X of approximate effective rank `rank` and no noise # component (very structured signal): U = rng.normal(size=(n_samples, k)) V = rng.normal(size=(k, n_features)) X = np.dot(U, V) assert_equal(X.shape, (n_samples, n_features)) # compute the singular values of X using the slow exact method U_, s_, V_ = linalg.svd(X, full_matrices=False) Ur, Sr, Vr = fast_svd(X, k, random_state=0) assert_equal(Vr.shape, (k, n_features)) assert_equal(Ur.shape, (n_samples, k)) # check the singular vectors too (while not checking the sign) assert_array_almost_equal( np.abs(np.diag(np.corrcoef(V_[:k], Vr)))[:k], np.ones(k))
def test_fast_svd(): n_samples = 100 k = 10 rng = np.random.RandomState(42) # We need to use n_features > 500 to trigger the randomized_svd for n_features in (30, 100, 550): # generate a matrix X of approximate effective rank `rank` and no noise # component (very structured signal): U = rng.normal(size=(n_samples, k)) V = rng.normal(size=(k, n_features)) X = np.dot(U, V) assert X.shape == (n_samples, n_features) # compute the singular values of X using the slow exact method U_, s_, V_ = linalg.svd(X, full_matrices=False) Ur, Sr, Vr = fast_svd(X, k, random_state=0) assert Vr.shape == (k, n_features) assert Ur.shape == (n_samples, k) # check the singular vectors too (while not checking the sign) assert_array_almost_equal( np.abs(np.diag(np.corrcoef(V_[:k], Vr)))[:k], np.ones(k))
def _reduce(self, signals): """ Perform temporal dimensionality reduction. :param signals: single-subject resting state matrix. :return U: reduced resting state matrix """ U, S, V = fast_svd(signals.T, self.n_components) U = U.T.copy() U = U * S[:, np.newaxis] return U