示例#1
0
 def test3_id_list(self):
     opt = MatrixMarketOptions().get_default_option()
     opt.input.main = np.array([[1, 2], [1, 2], [2, 1]])
     opt.input.uid = [1, 2.0, '3']
     opt.input.iid = np.array(['1', 'a'])
     mm = MatrixMarket(opt)
     mm.create()
     self.assertTrue(True)
示例#2
0
    def test2_create(self):
        set_log_level(3)
        opt = MatrixMarketOptions().get_default_option()
        opt.input.main = self.mm_path
        opt.input.uid = self.uid_path
        opt.input.iid = self.iid_path
        mm = MatrixMarket(opt)
        mm.create()
        self.temp_files.append(opt.data.path)
        self.assertTrue(True)
        db = mm.handle
        self.assertEqual(sorted(db.keys()),
                         sorted(['vali', 'idmap', 'rowwise', 'colwise']))
        header = mm.get_header()
        self.assertEqual(header['num_nnz'], 5)
        self.assertEqual(header['num_users'], 5)
        self.assertEqual(header['num_items'], 3)

        data = [(u, kk, vv) for u, kk, vv in mm.iterate()]
        self.assertEqual(len(data), 5)
        self.assertEqual([int(kk) for _, kk, _ in data], [0, 0, 2, 1, 1])
        self.assertEqual(data[2], (2, 2, 1.0))

        data = [(u, kk, vv) for u, kk, vv in mm.iterate(axis='colwise')]
        self.assertEqual([int(kk) for _, kk, _ in data], [0, 1, 3, 4, 2])
示例#3
0
    def test1_minmax(self):
        opt = MatrixMarketOptions().get_default_option()
        opt.input.main = self.mm_path
        opt.input.uid = self.uid_path
        opt.input.iid = self.iid_path
        opt.data.value_prepro = aux.Option({
            'name': 'MinMaxScalar',
            'min': 3,
            'max': 5.0
        })
        mm = MatrixMarket(opt)
        mm.create()
        self.assertTrue(True)
        db = mm.handle
        self.assertEqual(sorted(db.keys()),
                         sorted(['vali', 'idmap', 'rowwise', 'colwise']))
        header = mm.get_header()
        self.assertEqual(header['num_nnz'], 5)
        self.assertEqual(header['num_users'], 5)
        self.assertEqual(header['num_items'], 3)

        data = [(u, kk, vv) for u, kk, vv in mm.iterate()]
        self.assertEqual(len(data), 5)
        self.assertEqual([int(kk) for _, kk, _ in data], [0, 0, 2, 1, 1])
        self.assertEqual([int(vv) for _, _, vv in data], [3, 5, 3, 3, 4])
        self.assertEqual(data[2], (2, 2, 3.0))
示例#4
0
def load(opt):
    from buffalo.data.mm import MatrixMarket
    from buffalo.data.stream import Stream
    if isinstance(opt, (str, )):
        opt = aux.Option(opt)

    assert isinstance(
        opt, (dict, aux.Option
              )), 'opt must be either str, or dict/aux.Option but {}'.format(
                  type(opt))
    if opt['type'] == 'matrix_market':
        return MatrixMarket(opt)
    if opt['type'] == 'stream':
        return Stream(opt)
    raise RuntimeError('Unexpected data.type: {}'.format(opt['type']))
示例#5
0
    def test0_onebased(self):
        opt = MatrixMarketOptions().get_default_option()
        opt.input.main = self.mm_path
        opt.input.uid = self.uid_path
        opt.input.iid = self.iid_path
        opt.data.value_prepro = aux.Option({'name': 'OneBased'})
        mm = MatrixMarket(opt)
        mm.create()
        self.temp_files.append(opt.data.path)
        self.assertTrue(True)
        db = mm.handle
        self.assertEqual(sorted(db.keys()),
                         sorted(['vali', 'idmap', 'rowwise', 'colwise']))
        header = mm.get_header()
        self.assertEqual(header['num_nnz'], 5)
        self.assertEqual(header['num_users'], 5)
        self.assertEqual(header['num_items'], 3)

        data = [(u, kk, vv) for u, kk, vv in mm.iterate()]
        self.assertEqual(len(data), 5)
        self.assertEqual([int(kk) for _, kk, _ in data], [0, 0, 2, 1, 1])
        self.assertEqual([int(vv) for _, _, vv in data], [1, 1, 1, 1, 1])
        self.assertEqual(data[2], (2, 2, 1.0))
示例#6
0
    def test2_implicit_als(self):
        opt = MatrixMarketOptions().get_default_option()
        opt.input.main = self.mm_path
        opt.input.uid = self.uid_path
        opt.input.iid = self.iid_path
        opt.data.value_prepro = aux.Option({
            'name': 'ImplicitALS',
            'epsilon': 0.5
        })
        mm = MatrixMarket(opt)
        mm.create()
        self.assertTrue(True)
        db = mm.handle
        self.assertEqual(sorted(db.keys()),
                         sorted(['vali', 'idmap', 'rowwise', 'colwise']))
        header = mm.get_header()
        self.assertEqual(header['num_nnz'], 5)
        self.assertEqual(header['num_users'], 5)
        self.assertEqual(header['num_items'], 3)

        data = [(u, kk, vv) for u, kk, vv in mm.iterate()]
        self.assertEqual(len(data), 5)
        self.assertEqual([int(kk) for _, kk, _ in data], [0, 0, 2, 1, 1])
        self.assertAlmostEqual(data[2][2], math.log(1 + 1.0 / 0.5))
示例#7
0
 def test3_id_list_except(self):
     opt = MatrixMarketOptions().get_default_option()
     opt.input.main = np.array([[1, 2], [1, 2], [2, 1]])
     opt.input.uid = [1, 2.0]  # size should be 3
     mm = MatrixMarket(opt)
     self.assertRaises(TypeError, mm.create)
示例#8
0
 def test3_list(self):
     opt = MatrixMarketOptions().get_default_option()
     opt.input.main = [[10, 123], [1, 2]]
     mm = MatrixMarket(opt)
     self.assertRaises(RuntimeError, opt.is_valid_option)
     self.assertRaises(RuntimeError, mm.create)
示例#9
0
 def test2_dense(self):
     opt = MatrixMarketOptions().get_default_option()
     opt.input.main = self.mm_dense
     mm = MatrixMarket(opt)
     mm.create()
     self.assertTrue(True)
示例#10
0
def get_buffalo_db(db):
    from buffalo.data.mm import MatrixMarket
    db_opt = BuffaloLib().get_database(db)
    db = MatrixMarket(db_opt)
    db.create()
    return db