numpy.npones

Here are the examples of the python api numpy.npones taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

6 Examples 7

Example 1

Project: bolt Source File: test_local_construct.py
def test_ones():

    from numpy import ones as npones
    x = npones((2, 3, 4))
    b = ones((2, 3, 4))
    assert allclose(x, b.toarray())

Example 2

Project: bolt Source File: test_spark_construct.py
def test_ones(sc):

    from numpy import ones as npones
    x = npones((2, 3, 4))
    b = ones((2, 3, 4), sc)
    assert allclose(x, b.toarray())

    x = npones(5)
    b = ones(5, sc)
    assert allclose(x, b.toarray())

Example 3

Project: bolt Source File: test_spark_basic.py
def test_dtype(sc):

    a = arange(2**8, dtype=int64)
    b = array(a, sc, dtype=int64)
    assert a.dtype == b.dtype
    assert b.dtype == dtype(int64)
    dtypes = b._rdd.map(lambda x: x[1].dtype).collect()
    for dt in dtypes:
        assert dt == dtype(int64)
    
    a = arange(2.0**8)
    b = array(a, sc)
    assert a.dtype == b.dtype
    assert b.dtype == dtype(float64)
    dtypes = b._rdd.map(lambda x: x[1].dtype).collect()
    for dt in dtypes:
        assert dt == dtype(float64)

    a = arange(2**8)
    b = array(a, sc)
    assert a.dtype == b.dtype
    assert b.dtype == dtype(int64)
    dtypes = b._rdd.map(lambda x: x[1].dtype).collect()
    for dt in dtypes:
        assert dt == dtype(int64)

    from numpy import ones as npones
    a = npones(2**8, dtype=bool)
    b = array(a, sc)
    assert a.dtype == b.dtype
    assert b.dtype == dtype(bool)
    dtypes = b._rdd.map(lambda x: x[1].dtype).collect()
    for dt in dtypes:
        assert dt == dtype(bool)

    b = ones(2**8, sc)
    assert b.dtype == dtype(float64)
    dtypes = b._rdd.map(lambda x: x[1].dtype).collect()
    for dt in dtypes:
        assert dt == dtype(float64)

    b = ones(2**8, sc, dtype=bool)
    assert b.dtype == dtype(bool)
    dtypes = b._rdd.map(lambda x: x[1].dtype).collect()
    for dt in dtypes:
        assert dt == dtype(bool)

Example 4

Project: bolt Source File: test_spark_basic.py
def test_astype(sc):
    
    from numpy import ones as npones
    
    a = npones(2**8, dtype=int64)
    b = array(a, sc, dtype=int64)
    c = b.astype(bool)
    assert c.dtype == dtype(bool)
    dtypes = c._rdd.map(lambda x: x[1].dtype).collect()
    for dt in dtypes:
        assert dt == dtype(bool)
        
    b = ones((100, 100), sc, dtype=int64)
    c = b.astype(bool)
    assert c.dtype == dtype(bool)
    dtypes = c._rdd.map(lambda x: x[1].dtype).collect()
    for dt in dtypes:
        assert dt == dtype(bool)

    b = ones((100, 100), sc)
    c = b.astype(bool)
    assert c.dtype == dtype(bool)
    dtypes = c._rdd.map(lambda x: x[1].dtype).collect()
    for dt in dtypes:
        assert dt == dtype(bool)

Example 5

Project: bolt Source File: test_spark_getting.py
def test_squeeze(sc):

    from numpy import ones as npones

    x = npones((1, 2, 1, 4))
    b = ones((1, 2, 1, 4), sc, axis=0)
    assert allclose(b.squeeze().toarray(), x.squeeze())
    assert allclose(b.squeeze((0, 2)).toarray(), x.squeeze((0, 2)))
    assert allclose(b.squeeze(0).toarray(), x.squeeze(0))
    assert allclose(b.squeeze(2).toarray(), x.squeeze(2))
    assert b.squeeze().split == 0
    assert b.squeeze((0, 2)).split == 0
    assert b.squeeze(2).split == 1

    x = npones((1, 2, 1, 4))
    b = ones((1, 2, 1, 4), sc, axis=(0, 1))
    assert allclose(b.squeeze().toarray(), x.squeeze())
    assert allclose(b.squeeze((0, 2)).toarray(), x.squeeze((0, 2)))
    assert allclose(b.squeeze(0).toarray(), x.squeeze(0))
    assert allclose(b.squeeze(2).toarray(), x.squeeze(2))
    assert b.squeeze().split == 1
    assert b.squeeze((0, 2)).split == 1
    assert b.squeeze(2).split == 2

    x = npones((1, 1, 1, 1))
    b = ones((1, 1, 1, 1), sc, axis=(0, 1))
    assert allclose(b.squeeze().toarray(), x.squeeze())

Example 6

Project: bolt Source File: test_spark_stacking.py
def test_stacked_shape_inference(sc):

    from numpy import ones as npones

    a = ones((100, 2), sc)
    a._rdd = a._rdd.partitionBy(2)
    s = a.stack(5)
    n = s.tordd().count()

    # operations that preserve keys
    assert s.map(lambda x: x * 2).unstack().shape == (100, 2)
    assert s.map(lambda x: x.sum(axis=1)).unstack().shape == (100,)
    assert s.map(lambda x: tile(x, (1, 2))).unstack().shape == (100, 4)

    # operations that create new keys
    assert s.map(lambda x: npones((2, 2))).unstack().shape == (n, 2, 2)
    assert s.map(lambda x: x.sum(axis=0)).unstack().shape == (n, 2)
    assert s.map(lambda x: asarray([2])).unstack().toarray().shape == (n, 1)
    assert s.map(lambda x: asarray(2)).unstack().toarray().shape == (n,)

    # composing functions works
    assert s.map(lambda x: x * 2).map(lambda x: x * 2).unstack().shape == (100, 2)
    assert s.map(lambda x: x * 2).map(lambda x: npones((2, 2))).unstack().shape == (n, 2, 2)
    assert s.map(lambda x: npones((2, 2))).map(lambda x: x * 2).unstack().shape == (n, 2, 2)

    # check the result
    assert allclose(s.map(lambda x: x.sum(axis=1)).unstack().toarray(), npones(100) * 2)
    assert allclose(s.map(lambda x: tile(x, (1, 2))).unstack().toarray(), npones((100, 4)))

    with pytest.raises(ValueError):
        s.map(lambda x: 2)

    with pytest.raises(ValueError):
        s.map(lambda x: None)

    with pytest.raises(RuntimeError):
        s.map(lambda x: 1/0)