

def test_sparse_random_matrix():
    n_components = 100
    n_features = 500
    for density in [0.3, 1.0]:
        s = (1 / density)
        A = sparse_random_matrix(n_components, n_features, density=density, random_state=0)
        A = densify(A)
        values = np.unique(A)
        assert_in((np.sqrt(s) / np.sqrt(n_components)), values)
        assert_in(((- np.sqrt(s)) / np.sqrt(n_components)), values)
        if (density == 1.0):
            assert_equal(np.size(values), 2)
        else:
            assert_in(0.0, values)
            assert_equal(np.size(values), 3)
        assert_almost_equal(np.mean((A == 0.0)), (1 - (1 / s)), decimal=2)
        assert_almost_equal(np.mean((A == (np.sqrt(s) / np.sqrt(n_components)))), (1 / (2 * s)), decimal=2)
        assert_almost_equal(np.mean((A == ((- np.sqrt(s)) / np.sqrt(n_components)))), (1 / (2 * s)), decimal=2)
        assert_almost_equal(np.var((A == 0.0), ddof=1), (((1 - (1 / s)) * 1) / s), decimal=2)
        assert_almost_equal(np.var((A == (np.sqrt(s) / np.sqrt(n_components))), ddof=1), (((1 - (1 / (2 * s))) * 1) / (2 * s)), decimal=2)
        assert_almost_equal(np.var((A == ((- np.sqrt(s)) / np.sqrt(n_components))), ddof=1), (((1 - (1 / (2 * s))) * 1) / (2 * s)), decimal=2)

dataset/ETHPy150Open scikit-learn/scikit-learn/sklearn/tests/test_random_projection.py test_sparse_random_matrix/original