コード例 #1
0
ファイル: test_sparse.py プロジェクト: ray2020/mrec
def test_savez_loadz():
    m = get_random_coo_matrix()
    f, path = tempfile.mkstemp(suffix=".npz")
    savez(m, path)
    n = loadz(path)
    os.remove(path)
    assert_array_equal(n.toarray(), m.toarray())
コード例 #2
0
def test_savez_loadz():
    m = get_random_coo_matrix()
    f, path = tempfile.mkstemp(suffix='.npz')
    savez(m, path)
    n = loadz(path)
    os.remove(path)
    assert_array_equal(n.toarray(), m.toarray())
コード例 #3
0
def test_init_fast_sparse_matrix():
    X = get_random_coo_matrix()
    Y = X.tocsr()
    Z = X.tocsc()
    for M in [X, Y, Z]:
        m = fast_sparse_matrix(M)
        assert_array_equal(m.X.toarray(), M.toarray())
        assert_equal(m.shape, M.shape)
コード例 #4
0
ファイル: test_sparse.py プロジェクト: ray2020/mrec
def test_init_fast_sparse_matrix():
    X = get_random_coo_matrix()
    Y = X.tocsr()
    Z = X.tocsc()
    for M in [X, Y, Z]:
        m = fast_sparse_matrix(M)
        assert_array_equal(m.X.toarray(), M.toarray())
        assert_equal(m.shape, M.shape)
コード例 #5
0
def test_loadtxt():
    X = get_random_coo_matrix()
    f, path = tempfile.mkstemp(suffix='.npz')
    with open(path, 'w') as f:
        for i, j, v in zip(X.row, X.col, X.data):
            print >> f, '{0}\t{1}\t{2}'.format(i + 1, j + 1, v)
    Y = loadtxt(path)
    os.remove(path)
    assert_sparse_matrix_equal(X, Y)
コード例 #6
0
ファイル: test_sparse.py プロジェクト: ray2020/mrec
def test_loadtxt():
    X = get_random_coo_matrix()
    f, path = tempfile.mkstemp(suffix=".npz")
    with open(path, "w") as f:
        for i, j, v in zip(X.row, X.col, X.data):
            print >> f, "{0}\t{1}\t{2}".format(i + 1, j + 1, v)
    Y = loadtxt(path)
    os.remove(path)
    assert_sparse_matrix_equal(X, Y)
コード例 #7
0
def test_save_load():
    """Save to file as arrays in numpy binary format."""
    X = get_random_coo_matrix()
    m = fast_sparse_matrix(X)
    f, path = tempfile.mkstemp(suffix='.npz')
    m.save(path)
    n = fast_sparse_matrix.load(path)
    os.remove(path)
    assert_equal(m.shape, n.shape)
    assert_array_equal(m.X.toarray(), n.X.toarray())
    assert_array_equal(m.col_view.toarray(), n.col_view.toarray())
コード例 #8
0
ファイル: test_sparse.py プロジェクト: ray2020/mrec
def test_save_load():
    """Save to file as arrays in numpy binary format."""
    X = get_random_coo_matrix()
    m = fast_sparse_matrix(X)
    f, path = tempfile.mkstemp(suffix=".npz")
    m.save(path)
    n = fast_sparse_matrix.load(path)
    os.remove(path)
    assert_equal(m.shape, n.shape)
    assert_array_equal(m.X.toarray(), n.X.toarray())
    assert_array_equal(m.col_view.toarray(), n.col_view.toarray())
def test_zero_known_item_scores():
    train = get_random_coo_matrix().tocsr()
    predictions = np.random.random_sample(train.shape)
    r = BaseRecommender()
    safe = r._zero_known_item_scores(predictions, train)
    num_users, num_items = predictions.shape
    for u in xrange(num_users):
        for i in xrange(num_items):
            if i in train[u].indices:
                assert_less_equal(safe[u, i], 0)
            else:
                assert_equal(safe[u, i], predictions[u, i])
コード例 #10
0
def test_zero_known_item_scores():
    train = get_random_coo_matrix().tocsr()
    predictions = np.random.random_sample(train.shape)
    r = BaseRecommender()
    safe = r._zero_known_item_scores(predictions,train)
    num_users,num_items = predictions.shape
    for u in xrange(num_users):
        for i in xrange(num_items):
            if i in train[u].indices:
                assert_less_equal(safe[u,i],0)
            else:
                assert_equal(safe[u,i],predictions[u,i])
コード例 #11
0
ファイル: test_mrec.py プロジェクト: ray2020/mrec
def test_save_load_sparse_matrix():
    X = get_random_coo_matrix()
    for fmt in ["tsv", "csv", "npz", "mm", "fsm"]:
        if fmt == "mm":
            suffix = ".mtx"
        elif fmt == "npz" or fmt == "fsm":
            suffix = ".npz"
        else:
            suffix = ""
        f, path = tempfile.mkstemp(suffix=suffix)
        save_sparse_matrix(X, fmt, path)
        Y = load_sparse_matrix(fmt, path)
        assert_sparse_matrix_equal(X, Y)
        os.remove(path)
コード例 #12
0
def test_save_load_sparse_matrix():
    X = get_random_coo_matrix()
    for fmt in ['tsv','csv','npz','mm','fsm']:
        if fmt == 'mm':
            suffix = '.mtx'
        elif fmt == 'npz' or fmt == 'fsm':
            suffix = '.npz'
        else:
            suffix = ''
        f,path = tempfile.mkstemp(suffix=suffix)
        save_sparse_matrix(X,fmt,path)
        Y = load_sparse_matrix(fmt,path)
        assert_sparse_matrix_equal(X,Y)
        os.remove(path)
コード例 #13
0
def test_fast_update_col():
    X = get_random_coo_matrix().tocsc()
    m = fast_sparse_matrix(X)
    cols = X.shape[1]
    for j in xrange(cols):
        vals = m.fast_get_col(j).data
        if (vals == 0).all():
            continue
        vals[vals != 0] += 1
        m.fast_update_col(j, vals)
        expected = X[:, j].toarray()
        for i in xrange(expected.shape[0]):
            if expected[i] != 0:
                expected[i] += 1
        assert_array_equal(m.fast_get_col(j).toarray(), expected)
コード例 #14
0
ファイル: test_sparse.py プロジェクト: ray2020/mrec
def test_fast_update_col():
    X = get_random_coo_matrix().tocsc()
    m = fast_sparse_matrix(X)
    cols = X.shape[1]
    for j in xrange(cols):
        vals = m.fast_get_col(j).data
        if (vals == 0).all():
            continue
        vals[vals != 0] += 1
        m.fast_update_col(j, vals)
        expected = X[:, j].toarray()
        for i in xrange(expected.shape[0]):
            if expected[i] != 0:
                expected[i] += 1
        assert_array_equal(m.fast_get_col(j).toarray(), expected)
コード例 #15
0
def test_fast_get_col():
    X = get_random_coo_matrix().tocsc()
    m = fast_sparse_matrix(X)
    rows, cols = X.shape
    for j in xrange(cols):
        assert_array_equal(m.fast_get_col(j).toarray(), X[:, j].toarray())
コード例 #16
0
ファイル: test_sparse.py プロジェクト: ray2020/mrec
def test_fast_get_col():
    X = get_random_coo_matrix().tocsc()
    m = fast_sparse_matrix(X)
    rows, cols = X.shape
    for j in xrange(cols):
        assert_array_equal(m.fast_get_col(j).toarray(), X[:, j].toarray())