Example #1
0
 def test_rand(self):
     # Simple distributional checks for sparse.rand.
     for random_state in None, 4321, np.random.RandomState():
         x = sprand(10, 20, density=0.5, dtype=np.float64,
                    random_state=random_state)
         assert_(np.all(np.less_equal(0, x.data)))
         assert_(np.all(np.less_equal(x.data, 1)))
Example #2
0
 def test_rand(self):
     # Simple distributional checks for sparse.rand.
     for random_state in None, 4321, np.random.RandomState():
         x = sprand(10, 20, density=0.5, dtype=np.float64,
                    random_state=random_state)
         assert_(np.all(np.less_equal(0, x.data)))
         assert_(np.all(np.less_equal(x.data, 1)))
Example #3
0
    def test_rand(self):
        # Simple sanity checks for sparse.rand
        for t in [np.float32, np.float64, np.longdouble]:
            x = sprand(5, 10, density=0.1, dtype=t)
            assert_equal(x.dtype, t)
            assert_equal(x.shape, (5, 10))
            assert_equal(x.nonzero()[0].size, 5)

        x = sprand(5, 10, density=0.1)
        assert_equal(x.dtype, np.double)

        for fmt in ['coo', 'csc', 'csr', 'lil']:
            x = sprand(5, 10, format=fmt)
            assert_equal(x.format, fmt)

        assert_raises(ValueError, lambda: sprand(5, 10, 1.1))
        assert_raises(ValueError, lambda: sprand(5, 10, -0.1))
Example #4
0
    def test_rand(self):
        # Simple sanity checks for sparse.rand
        for t in [np.float32, np.float64, np.longdouble]:
            x = sprand(5, 10, density=0.1, dtype=t)
            assert_equal(x.dtype, t)
            assert_equal(x.shape, (5, 10))
            assert_equal(x.nonzero()[0].size, 5)

        x = sprand(5, 10, density=0.1)
        assert_equal(x.dtype, np.double)

        for fmt in ['coo', 'csc', 'csr', 'lil']:
            x = sprand(5, 10, format=fmt)
            assert_equal(x.format, fmt)

        assert_raises(ValueError, lambda: sprand(5, 10, 1.1))
        assert_raises(ValueError, lambda: sprand(5, 10, -0.1))
Example #5
0
    def test_rand(self):
        # Simple sanity checks for sparse.rand
        for t in [np.float32, np.float64, np.longdouble]:
            x = sprand(5, 10, density=0.1, dtype=t)
            assert_equal(x.dtype, t)
            assert_equal(x.shape, (5, 10))
            assert_equal(x.nonzero()[0].size, 5)

        x1 = sprand(5, 10, density=0.1,
                    random_state=4321)
        assert_equal(x1.dtype, np.double)

        x2 = sprand(5, 10, density=0.1,
                    random_state=np.random.RandomState(4321))

        assert_array_equal(x1.data, x2.data)
        assert_array_equal(x1.row, x2.row)
        assert_array_equal(x1.col, x2.col)

        for density in [0.0, 0.1, 0.5, 1.0]:
            x = sprand(5, 10, density=density)
            assert_equal(x.nnz, int(density * np.prod(x.shape)))

        for fmt in ['coo', 'csc', 'csr', 'lil']:
            x = sprand(5, 10, format=fmt)
            assert_equal(x.format, fmt)

        assert_raises(ValueError, lambda: sprand(5, 10, 1.1))
        assert_raises(ValueError, lambda: sprand(5, 10, -0.1))
Example #6
0
    def test_rand(self):
        # Simple sanity checks for sparse.rand
        for t in [np.float32, np.float64, np.longdouble]:
            x = sprand(5, 10, density=0.1, dtype=t)
            assert_equal(x.dtype, t)
            assert_equal(x.shape, (5, 10))
            assert_equal(x.nonzero()[0].size, 5)

        x1 = sprand(5, 10, density=0.1, random_state=4321)
        assert_equal(x1.dtype, np.double)

        x2 = sprand(5,
                    10,
                    density=0.1,
                    random_state=np.random.RandomState(4321))

        assert_array_equal(x1.data, x2.data)
        assert_array_equal(x1.row, x2.row)
        assert_array_equal(x1.col, x2.col)

        for density in [0.0, 0.1, 0.5, 1.0]:
            x = sprand(5, 10, density=density)
            assert_equal(x.nnz, int(density * np.prod(x.shape)))

        for fmt in ['coo', 'csc', 'csr', 'lil']:
            x = sprand(5, 10, format=fmt)
            assert_equal(x.format, fmt)

        assert_raises(ValueError, lambda: sprand(5, 10, 1.1))
        assert_raises(ValueError, lambda: sprand(5, 10, -0.1))
    def test_rand(self):
        # Simple distributional checks for sparse.rand.
        random_states = [None, 4321, np.random.RandomState()]
        try:
            gen = np.random.default_rng()
            random_states.append(gen)
        except AttributeError:
            pass

        for random_state in random_states:
            x = sprand(10, 20, density=0.5, dtype=np.float64,
                       random_state=random_state)
            assert_(np.all(np.less_equal(0, x.data)))
            assert_(np.all(np.less_equal(x.data, 1)))