def test_sparse_formats(fmt, X_sparse):
    n_samples = X_sparse.shape[0]
    Xfmt = (X_sparse.toarray() if fmt == "dense" else getattr(
        X_sparse, "to" + fmt)())
    tsvd = TruncatedSVD(n_components=11)
    Xtrans = tsvd.fit_transform(Xfmt)
    assert Xtrans.shape == (n_samples, 11)
    Xtrans = tsvd.transform(Xfmt)
    assert Xtrans.shape == (n_samples, 11)
def test_singular_values_consistency(solver):
    # Check that the TruncatedSVD output has the correct singular values
    rng = np.random.RandomState(0)
    n_samples, n_features = 100, 80
    X = rng.randn(n_samples, n_features)

    pca = TruncatedSVD(n_components=2, algorithm=solver,
                       random_state=rng).fit(X)

    # Compare to the Frobenius norm
    X_pca = pca.transform(X)
    assert_allclose(np.sum(pca.singular_values_**2.0),
                    np.linalg.norm(X_pca, "fro")**2.0,
                    rtol=1e-2)

    # Compare to the 2-norms of the score vectors
    assert_allclose(pca.singular_values_,
                    np.sqrt(np.sum(X_pca**2.0, axis=0)),
                    rtol=1e-2)