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())
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())
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()
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()
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())
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())
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()
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()
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))
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))
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])
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])
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]])
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()
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])
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,))
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())
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())
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()
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])
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]
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, ))
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)))
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]
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())))
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())))
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
def test_non2Dsubs(): sptensor(randint(0, 10, 18).reshape(3, 3, 2), ones(10))
def test_non2Dsubs(): with pytest.raises(ValueError): sptensor(randint(0, 10, 18).reshape(3, 3, 2), ones(10))
def test_nonEqualLength(subs): with pytest.raises(ValueError): sptensor(subs, ones(len(subs) + 1))
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()
def test_nonEqualLength(): subs, vals, shape = mysetup() sptensor(subs, ones(len(subs) + 1))
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())
def test_sttm_me(): S = sptensor(T.nonzero(), T.flatten(), T.shape) S.ttm_me(U, [1], [0], False)
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()
def test_non2Dsubs(): sptensor(randint(0, 10, 18).reshape(3, 3, 2), ones(10))
def test_nonEqualLength(subs): with pytest.raises(ValueError): sptensor(subs, ones(len(subs) + 1))
def test_non2Dsubs(): with pytest.raises(ValueError): sptensor(randint(0, 10, 18).reshape(3, 3, 2), ones(10))
def test_nonEqualLength(): sptensor(subs, ones(len(subs) + 1))
def test_nonEqualLength(): sptensor(subs, ones(len(subs) + 1))
def test_sttm_me(): S = sptensor(T.nonzero(), T.flatten(), T.shape) S.ttm_me(U, [1], [0], False)
def test_sttm_me(T, U): S = sptensor(T.nonzero(), T.flatten(), T.shape) S._ttm_me_compute(U, [1], [0], False)
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())
def test_sttm_me(T, U): S = sptensor(T.nonzero(), T.flatten(), T.shape) S._ttm_me_compute(U, [1], [0], False)