예제 #1
0
def test_singular_values(setup):
    # Check that the TruncatedSVD output has the correct singular values

    # Set the singular values and see what we get back
    rng = np.random.RandomState(0)
    n_samples = 100
    n_features = 110

    X = rng.randn(n_samples, n_features)

    rpca = TruncatedSVD(n_components=3,
                        algorithm='randomized',
                        random_state=rng)
    X_rpca = rpca.fit_transform(X)

    X_rpca /= mt.sqrt(mt.sum(X_rpca**2.0, axis=0))
    X_rpca[:, 0] *= 3.142
    X_rpca[:, 1] *= 2.718

    X_hat_rpca = mt.dot(X_rpca, rpca.components_)
    rpca.fit(X_hat_rpca)
    assert_array_almost_equal(rpca.singular_values_.to_numpy(),
                              [3.142, 2.718, 1.0], 14)
예제 #2
0
def test_too_many_components(setup):
    for n_components in (n_features, n_features + 1):
        tsvd = TruncatedSVD(n_components=n_components, algorithm='randomized')
        with pytest.raises(ValueError):
            tsvd.fit(X)
예제 #3
0
 def test_too_many_components(self):
     for n_components in (self.n_features, self.n_features + 1):
         tsvd = TruncatedSVD(n_components=n_components,
                             algorithm='randomized')
         with self.assertRaises(ValueError):
             tsvd.fit(self.X)