Esempio n. 1
0
def test_init_diagonal():
    subs, vals, shape = setup_diagonal()
    T = sptensor(subs, vals, shape)
    assert_equal(len(shape), T.ndim)
    assert_true((array(shape) == T.shape).all())

    T = sptensor(subs, vals)
    assert_equal(len(subs), len(T.shape))
    assert_true((shape == array(T.shape)).all())
Esempio n. 2
0
def test_init_diagonal():
    subs, vals, shape = setup_diagonal()
    T = sptensor(subs, vals, shape)
    assert_equal(len(shape), T.ndim)
    assert_true((array(shape) == T.shape).all())

    T = sptensor(subs, vals)
    assert_equal(len(subs), len(T.shape))
    assert_true((shape == array(T.shape)).all())
Esempio n. 3
0
def test_init_diagonal():
    subs, vals, shape = setup_diagonal()
    T = sptensor(subs, vals, shape)
    assert len(shape) == T.ndim
    assert (array(shape) == T.shape).all()

    T = sptensor(subs, vals)
    assert len(subs) == len(T.shape)
    assert (shape == array(T.shape)).all()
Esempio n. 4
0
def test_init_diagonal():
    subs, vals, shape = setup_diagonal()
    T = sptensor(subs, vals, shape)
    assert len(shape) == T.ndim
    assert (array(shape) == T.shape).all()

    T = sptensor(subs, vals)
    assert len(subs) == len(T.shape)
    assert (shape == array(T.shape)).all()
Esempio n. 5
0
def test_init():
    """
    Creation of new sptensor objects
    """
    T = sptensor(subs, vals, shape)
    assert_equal(len(shape), T.ndim)
    assert_true((array(shape) == T.shape).all())

    T = sptensor(subs, vals)
    tshape = array(subs).max(axis=1) + 1
    assert_equal(len(subs), len(T.shape))
    assert_true((tshape == array(T.shape)).all())
Esempio n. 6
0
def test_init():
    """
    Creation of new sptensor objects
    """
    T = sptensor(subs, vals, shape)
    assert_equal(len(shape), T.ndim)
    assert_true((array(shape) == T.shape).all())

    T = sptensor(subs, vals)
    tshape = array(subs).max(axis=1) + 1
    assert_equal(len(subs), len(T.shape))
    assert_true((tshape == array(T.shape)).all())
Esempio n. 7
0
def test_init(subs, vals, shape):
    """
    Creation of new sptensor objects
    """
    T = sptensor(subs, vals, shape)
    assert len(shape) == T.ndim
    assert (array(shape) == T.shape).all()

    T = sptensor(subs, vals)
    tshape = array(subs).max(axis=1) + 1
    assert len(subs) == len(T.shape)
    assert (tshape == array(T.shape)).all()
Esempio n. 8
0
def test_init(subs, vals, shape):
    """
    Creation of new sptensor objects
    """
    T = sptensor(subs, vals, shape)
    assert len(shape) == T.ndim
    assert (array(shape) == T.shape).all()

    T = sptensor(subs, vals)
    tshape = array(subs).max(axis=1) + 1
    assert len(subs) == len(T.shape)
    assert (tshape == array(T.shape)).all()
Esempio n. 9
0
def test_sp_uttkrp():
    # Test case by Andre Panisson, sparse ttv
    # see issue #3
    S = sptensor(subs, vals, shape)
    U = []
    for shp in shape:
        U.append(np.zeros((shp, 5)))
    SU = S.uttkrp(U, mode=0)
    assert_equal(SU.shape, (25, 5))
Esempio n. 10
0
def test_sp_uttkrp():
    # Test case by Andre Panisson, sparse ttv
    # see issue #3
    S = sptensor(subs, vals, shape)
    U = []
    for shp in shape:
        U.append(np.zeros((shp, 5)))
    SU = S.uttkrp(U, mode=0)
    assert_equal(SU.shape, (25, 5))
Esempio n. 11
0
def test_add():
    subs = (array([0, 1, 0]), array([2, 0, 2]), array([0, 1, 2]))
    vals = array([1, 2, 3])
    S = sptensor(subs, vals, shape=[3, 3, 3])
    D = np.arange(27).reshape(3, 3, 3)
    T = S - D
    for i in range(3):
        for j in range(3):
            for k in range(3):
                assert_equal(S[i, j, k] - D[i, j, k], T[i, j, k])
Esempio n. 12
0
def test_add():
    subs = (array([0, 1, 0]), array([2, 0, 2]), array([0, 1, 2]))
    vals = array([1, 2, 3])
    S = sptensor(subs, vals, shape=[3, 3, 3])
    D = np.arange(27).reshape(3, 3, 3)
    T = S - D
    for i in range(3):
        for j in range(3):
            for k in range(3):
                assert_equal(S[i, j, k] - D[i, j, k], T[i, j, k])
Esempio n. 13
0
def test_getitem():
    subs = (
        array([0, 1, 0, 5, 7, 8]),
        array([2, 0, 4, 5, 3, 9]),
        array([0, 1, 2, 2, 1, 0])
    )
    vals = array([1, 2, 3, 4, 5, 6])
    S = sptensor(subs, vals, shape=[10, 10, 3])
    assert_equal(0, S[1, 1, 1])
    assert_equal(0, S[1, 2, 3])
    for i in range(len(vals)):
        assert_equal(vals[i], S[subs[0][i], subs[1][i], subs[2][i]])
Esempio n. 14
0
def test_fold(subs, vals, shape):
    T = sptensor(subs, vals, shape)
    for i in range(len(shape)):
        X = T.unfold([i]).fold()
        assert shape == tuple(T.shape)
        assert len(shape) == len(T.subs)
        assert len(subs) == len(T.subs)
        assert X == T
        for j in range(len(subs)):
            subs[j].sort()
            T.subs[j].sort()
            assert (subs[j] == T.subs[j]).all()
Esempio n. 15
0
def test_getitem():
    subs = (array([0, 1, 0, 5, 7, 8]), array([2, 0, 4, 5, 3, 9]), array([0, 1, 2, 2, 1, 0]))
    vals = array([1, 2, 3, 4, 5, 6])
    S = sptensor(subs, vals, shape=[10, 10, 3])
    assert_equal(0, S[1, 1, 1])
    assert_equal(0, S[1, 2, 3])
    assert_equal(1, S[0, 2, 0])
    assert_equal(2, S[1, 0, 1])
    assert_equal(3, S[0, 4, 2])
    assert_equal(4, S[5, 5, 2])
    assert_equal(5, S[7, 3, 1])
    assert_equal(6, S[8, 9, 0])
Esempio n. 16
0
def test_ttv_sparse_result():
    # Test case by Andre Panisson to check return type of sptensor.ttv
    subs = (array([0, 1, 0, 5, 7, 8]), array([2, 0, 4, 5, 3, 9]), array([0, 1, 2, 2, 1, 0]))
    vals = array([1, 1, 1, 1, 1, 1])
    S = sptensor(subs, vals, shape=[10, 10, 3])

    sttv = S.ttv((zeros(10), zeros(10)), modes=[0, 1])
    assert_equal(type(sttv), sptensor)
    # sparse tensor should return only nonzero vals
    assert_true((allclose(np.array([]), sttv.vals)))
    assert_true((allclose(np.array([]), sttv.subs)))
    assert_equal(sttv.shape, (3,))
Esempio n. 17
0
def test_fold():
    T = sptensor(subs, vals, shape)
    for i in range(len(shape)):
        X = T.unfold([i]).fold()
        assert_equal(shape, tuple(T.shape))
        assert_equal(len(shape), len(T.subs))
        assert_equal(len(subs), len(T.subs))
        assert_equal(X, T)
        for j in range(len(subs)):
            subs[j].sort()
            T.subs[j].sort()
            assert_true((subs[j] == T.subs[j]).all())
Esempio n. 18
0
def test_fold():
    T = sptensor(subs, vals, shape)
    for i in range(len(shape)):
        X = T.unfold([i]).fold()
        assert_equal(shape, tuple(T.shape))
        assert_equal(len(shape), len(T.subs))
        assert_equal(len(subs), len(T.subs))
        assert_equal(X, T)
        for j in range(len(subs)):
            subs[j].sort()
            T.subs[j].sort()
            assert_true((subs[j] == T.subs[j]).all())
Esempio n. 19
0
def test_fold(subs, vals, shape):
    T = sptensor(subs, vals, shape)
    for i in range(len(shape)):
        X = T.unfold([i]).fold()
        assert shape == tuple(T.shape)
        assert len(shape) == len(T.subs)
        assert len(subs) == len(T.subs)
        assert X == T
        for j in range(len(subs)):
            subs[j].sort()
            T.subs[j].sort()
            assert (subs[j] == T.subs[j]).all()
Esempio n. 20
0
def test_getitem():
    subs = (array([0, 1, 0, 5, 7, 8]), array([2, 0, 4, 5, 3,
                                              9]), array([0, 1, 2, 2, 1, 0]))
    vals = array([1, 2, 3, 4, 5, 6])
    S = sptensor(subs, vals, shape=[10, 10, 3])
    assert_equal(0, S[1, 1, 1])
    assert_equal(0, S[1, 2, 3])
    assert_equal(1, S[0, 2, 0])
    assert_equal(2, S[1, 0, 1])
    assert_equal(3, S[0, 4, 2])
    assert_equal(4, S[5, 5, 2])
    assert_equal(5, S[7, 3, 1])
    assert_equal(6, S[8, 9, 0])
Esempio n. 21
0
def test_getitem():
    subs = (array([0, 1, 0, 5, 7, 8]), array([2, 0, 4, 5, 3,
                                              9]), array([0, 1, 2, 2, 1, 0]))
    vals = array([1, 2, 3, 4, 5, 6])
    S = sptensor(subs, vals, shape=[10, 10, 3])
    assert 0 == S[1, 1, 1]
    assert 0 == S[1, 2, 3]
    assert 1 == S[0, 2, 0]
    assert 2 == S[1, 0, 1]
    assert 3 == S[0, 4, 2]
    assert 4 == S[5, 5, 2]
    assert 5 == S[7, 3, 1]
    assert 6 == S[8, 9, 0]
Esempio n. 22
0
def test_ttv_sparse_result():
    # Test case by Andre Panisson to check return type of sptensor.ttv
    subs = (array([0, 1, 0, 5, 7, 8]), array([2, 0, 4, 5, 3,
                                              9]), array([0, 1, 2, 2, 1, 0]))
    vals = array([1, 1, 1, 1, 1, 1])
    S = sptensor(subs, vals, shape=[10, 10, 3])

    sttv = S.ttv((zeros(10), zeros(10)), modes=[0, 1])
    assert_equal(type(sttv), sptensor)
    # sparse tensor should return only nonzero vals
    assert_true((allclose(np.array([]), sttv.vals)))
    assert_true((allclose(np.array([]), sttv.subs)))
    assert_equal(sttv.shape, (3, ))
Esempio n. 23
0
def test_ttv():
    # Test case by Andre Panisson to check return type of sptensor.ttv
    subs = (
        array([0, 1, 0, 5, 7, 8]),
        array([2, 0, 4, 5, 3, 9]),
        array([0, 1, 2, 2, 1, 0])
    )
    vals = array([1, 1, 1, 1, 1, 1])
    S = sptensor(subs, vals, shape=[10, 10, 3])

    sttv = S.ttv((zeros(10), zeros(10)), modes=[0, 1])
    assert_equal(type(sttv), sptensor)
    assert_true((allclose(zeros(3), sttv.vals)))
    assert_true((allclose(np.arange(3), sttv.subs)))
Esempio n. 24
0
def test_getitem():
    subs = (
        array([0, 1, 0, 5, 7, 8]),
        array([2, 0, 4, 5, 3, 9]),
        array([0, 1, 2, 2, 1, 0])
    )
    vals = array([1, 2, 3, 4, 5, 6])
    S = sptensor(subs, vals, shape=[10, 10, 3])
    assert 0 == S[1, 1, 1]
    assert 0 == S[1, 2, 3]
    assert 1 == S[0, 2, 0]
    assert 2 == S[1, 0, 1]
    assert 3 == S[0, 4, 2]
    assert 4 == S[5, 5, 2]
    assert 5 == S[7, 3, 1]
    assert 6 == S[8, 9, 0]
Esempio n. 25
0
def test_unfold():
    Td = dtensor(zeros(shape, dtype=np.float32))
    Td[subs] = vals

    for i in range(len(shape)):
        rdims = [i]
        cdims = setdiff1d(range(len(shape)), rdims)[::-1]
        Md = Td.unfold(i)

        T = sptensor(subs, vals, shape, accumfun=lambda l: l[-1])

        Ms = T.unfold(rdims, cdims)
        assert_equal(Md.shape, Ms.shape)
        assert_true((allclose(Md, Ms.toarray())))

        Ms = T.unfold(rdims)
        assert_equal(Md.shape, Ms.shape)
        assert_true((allclose(Md, Ms.toarray())))

        Md = Md.T
        Ms = T.unfold(rdims, cdims, transp=True)
        assert_equal(Md.shape, Ms.shape)
        assert_true((allclose(Md, Ms.toarray())))
Esempio n. 26
0
def test_unfold():
    Td = dtensor(zeros(shape, dtype=np.float32))
    Td[subs] = vals

    for i in range(len(shape)):
        rdims = [i]
        cdims = setdiff1d(range(len(shape)), rdims)[::-1]
        Md = Td.unfold(i)

        T = sptensor(subs, vals, shape, accumfun=lambda l: l[-1])

        Ms = T.unfold(rdims, cdims)
        assert_equal(Md.shape, Ms.shape)
        assert_true((allclose(Md, Ms.toarray())))

        Ms = T.unfold(rdims)
        assert_equal(Md.shape, Ms.shape)
        assert_true((allclose(Md, Ms.toarray())))

        Md = Md.T
        Ms = T.unfold(rdims, cdims, transp=True)
        assert_equal(Md.shape, Ms.shape)
        assert_true((allclose(Md, Ms.toarray())))
Esempio n. 27
0
def dataframeToTensor(dataset,
                      dimensions_col=['user', 'item', 'action'],
                      rating_col='rating',
                      keepZero=False):
    dims = dict.fromkeys(dimensions_col)

    shape = []
    for col in dimensions_col:
        dims[col] = max(dataset[col]) + 1
        shape.append(dims[col])

    if not (keepZero):
        dataset = dataset.loc[dataset[rating_col] != 0]

    subs = []
    for col in dimensions_col:
        subs.append(list(dataset[col]))

    tensor = sptensor(tuple(subs),
                      dataset[rating_col].values,
                      shape=tuple(shape),
                      dtype=np.float)

    return tensor
Esempio n. 28
0
def test_non2Dsubs():
    sptensor(randint(0, 10, 18).reshape(3, 3, 2), ones(10))
Esempio n. 29
0
def test_non2Dsubs():
    with pytest.raises(ValueError):
        sptensor(randint(0, 10, 18).reshape(3, 3, 2), ones(10))
Esempio n. 30
0
def test_nonEqualLength(subs):
    with pytest.raises(ValueError):
        sptensor(subs, ones(len(subs) + 1))
Esempio n. 31
0
def test_ttm(T, Y, U):
    S = sptensor(T.nonzero(), T.flatten(), T.shape)
    Y2 = S.ttm(U, 0)
    assert (2, 4, 2) == Y2.shape
    assert (Y == Y2).all()
Esempio n. 32
0
def test_nonEqualLength():
    subs, vals, shape = mysetup()
    sptensor(subs, ones(len(subs) + 1))
Esempio n. 33
0
def test_ttm():
    S = sptensor(T.nonzero(), T.flatten(), T.shape)
    Y2 = S.ttm(U, 0)
    assert_equal((2, 4, 2), Y2.shape)
    assert_true((Y == Y2).all())
Esempio n. 34
0
def test_sttm_me():
    S = sptensor(T.nonzero(), T.flatten(), T.shape)
    S.ttm_me(U, [1], [0], False)
Esempio n. 35
0
def test_ttm(T, Y, U):
    S = sptensor(T.nonzero(), T.flatten(), T.shape)
    Y2 = S.ttm(U, 0)
    assert (2, 4, 2) == Y2.shape
    assert (Y == Y2).all()
Esempio n. 36
0
def test_non2Dsubs():
    sptensor(randint(0, 10, 18).reshape(3, 3, 2), ones(10))
Esempio n. 37
0
def test_nonEqualLength(subs):
    with pytest.raises(ValueError):
        sptensor(subs, ones(len(subs) + 1))
Esempio n. 38
0
def test_non2Dsubs():
    with pytest.raises(ValueError):
        sptensor(randint(0, 10, 18).reshape(3, 3, 2), ones(10))
Esempio n. 39
0
def test_nonEqualLength():
    sptensor(subs, ones(len(subs) + 1))
Esempio n. 40
0
def test_nonEqualLength():
    sptensor(subs, ones(len(subs) + 1))
Esempio n. 41
0
def test_sttm_me():
    S = sptensor(T.nonzero(), T.flatten(), T.shape)
    S.ttm_me(U, [1], [0], False)
Esempio n. 42
0
def test_sttm_me(T, U):
    S = sptensor(T.nonzero(), T.flatten(), T.shape)
    S._ttm_me_compute(U, [1], [0], False)
Esempio n. 43
0
def test_ttm():
    S = sptensor(T.nonzero(), T.flatten(), T.shape)
    Y2 = S.ttm(U, 0)
    assert_equal((2, 4, 2), Y2.shape)
    assert_true((Y == Y2).all())
Esempio n. 44
0
def test_sttm_me(T, U):
    S = sptensor(T.nonzero(), T.flatten(), T.shape)
    S._ttm_me_compute(U, [1], [0], False)