Пример #1
0
    def test_update_index_new_index(self):
        # Virtually the same as `test_build_index` but using update_index.
        bt = SkLearnBallTreeHashIndex(random_seed=0)
        # Make 1000 random bit vectors of length 256
        m = np.random.randint(0, 2, 1000 * 256).reshape(1000, 256).astype(bool)
        bt.update_index(m)

        # deterministically sort index of built and source data to determine
        # that an index was built.
        self.assertIsNotNone(bt.bt)
        np.testing.assert_array_almost_equal(
            sorted(np.array(bt.bt.data).tolist()), sorted(m.tolist()))
Пример #2
0
    def test_update_index_additive(self):
        # Test updating an existing index, i.e. rebuilding using the union of
        # previous and new data.
        bt = SkLearnBallTreeHashIndex(random_seed=0)
        # Make 1000 random bit vectors of length 256
        m1 = np.random.randint(0, 2, 1000 * 256).reshape(1000, 256)\
               .astype(bool)
        m2 = np.random.randint(0, 2, 100 * 256).reshape(100, 256).astype(bool)

        # Build initial index
        bt.build_index(m1)
        # Current model should only contain m1's data.
        np.testing.assert_array_almost_equal(
            sorted(np.array(bt.bt.data).tolist()), sorted(m1.tolist()))

        # "Update" index with new hashes
        bt.update_index(m2)
        # New model should contain the union of the data.
        np.testing.assert_array_almost_equal(
            sorted(np.array(bt.bt.data).tolist()),
            sorted(np.concatenate([m1, m2], 0).tolist()))