Пример #1
0
 def test_score(self):
     self.recommender.register(User(0))
     self.recommender.register(Item(0))
     self.recommender.update(Event(User(0), Item(0), 1))
     score = self.recommender.score(User(0), np.array([0]))
     print(score)
     self.assertTrue(score >= -1.0 and score <= 1.0)
Пример #2
0
 def test_update(self):
     self.recommender.register(User(0))
     self.recommender.register(Item(0))
     self.recommender.update(
         Event(User(0), Item(0), 1, context=np.array([1, 2, 3])))
     self.assertEqual(self.recommender.n_user, 1)
     self.assertEqual(self.recommender.n_item, 1)
Пример #3
0
 def test_score(self):
     self.recommender.register(User(0))
     self.recommender.register(Item(0))
     self.recommender.update(
         Event(User(0), Item(0), 1, context=np.array([1, 2, 3])))
     score = self.recommender.score(User(0),
                                    candidates=np.array([0]),
                                    context=np.array([1, 2, 3]))
     self.assertTrue(score >= 0.0 and score <= 1.0)
Пример #4
0
    def test_item(self):
        item = Item(1, np.arange(5))
        self.assertEqual(item.index, 1)

        v = item.encode(dim=None, index=True, feature=True, vertical=False)
        assert_array_equal(v, np.array([0, 1, 0, 1, 2, 3, 4]))

        v = item.encode(dim=3, index=True, feature=False, vertical=False)
        assert_array_equal(v, np.array([0, 1, 0]))

        v = item.encode(dim=None, index=False, feature=True, vertical=True)
        assert_array_equal(v, np.array([[0], [1], [2], [3], [4]]))
Пример #5
0
    def test_event(self):
        user = User(1, np.arange(3))
        item = Item(1, np.arange(3))
        event = Event(user, item, 5.0, np.arange(5))
        self.assertEqual(event.value, 5.0)

        v = event.encode(index=False,
                         feature=True,
                         context=True,
                         vertical=False)
        assert_array_equal(v, np.array([0, 1, 2, 0, 1, 2, 3, 4, 0, 1, 2]))

        v = event.encode(index=True,
                         feature=True,
                         context=False,
                         vertical=False)
        assert_array_equal(v, np.array([0, 1, 0, 1, 2, 0, 1, 0, 1, 2]))

        v = event.encode(n_user=3,
                         n_item=3,
                         index=True,
                         feature=False,
                         context=False,
                         vertical=True)
        assert_array_equal(v, np.array([[0], [1], [0], [0], [1], [0]]))
Пример #6
0
async def process(stream):
    async for obj in stream:
        event = json.loads(obj)
        if event["rating"] < 3:
            continue
        user, item = User(event["user"] - 1), Item(event["item"] - 1)
        print(recommender.recommend(user, np.arange(0, n_item)))
        recommender.update(Event(user, item))
Пример #7
0
    def setUp(self):
        recommender = Popular()
        recommender.initialize()
        self.evaluator = Evaluator(recommender=recommender, repeat=False)

        self.samples = [
            Event(User(0), Item(0), 1),
            Event(User(0), Item(1), 1),
            Event(User(1), Item(2), 1),
            Event(User(0), Item(3), 1),
            Event(User(2), Item(4), 1),
            Event(User(1), Item(4), 1),
            Event(User(0), Item(5), 1),
            Event(User(2), Item(1), 1),
            Event(User(0), Item(6), 1),
            Event(User(2), Item(0), 1),
        ]
Пример #8
0
 def test_register_item(self):
     self.recommender.register(Item(0))
     self.assertEqual(self.recommender.n_item, 1)
Пример #9
0
 def test_update(self):
     self.recommender.register(User(0))
     self.recommender.register(Item(0))
     self.recommender.update(Event(User(0), Item(0), 1))
     self.assertEqual(self.recommender.n_user, 1)
     self.assertEqual(self.recommender.n_item, 1)
Пример #10
0
app = faust.App(
    "flurs-recommender",
    broker="kafka://localhost:9092",
    value_serializer="raw",
)

topic = app.topic("flurs-events", value_type=bytes)

recommender = MFRecommender(k=40)
recommender.initialize()

n_user, n_item = 943, 1682

for u in range(1, n_user + 1):
    recommender.register(User(u - 1))

for i in range(1, n_item + 1):
    recommender.register(Item(i - 1))


@app.agent(topic)
async def process(stream):
    async for obj in stream:
        event = json.loads(obj)
        if event["rating"] < 3:
            continue
        user, item = User(event["user"] - 1), Item(event["item"] - 1)
        print(recommender.recommend(user, np.arange(0, n_item)))
        recommender.update(Event(user, item))