/
popular.py
34 lines (23 loc) · 945 Bytes
/
popular.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from sklearn.base import BaseEstimator
from ..base import RecommenderMixin
import numpy as np
class Popular(BaseEstimator, RecommenderMixin):
"""Popularity-based non-personalized baseline that prioritizes
items observed most through `update`, regardless of user profiles.
"""
def __init__(self):
self.freq = np.array([])
def initialize(self):
super(Popular, self).initialize()
def register_user(self, user):
super(Popular, self).register_user(user)
def register_item(self, item):
super(Popular, self).register_item(item)
self.freq = np.append(self.freq, 0)
def update(self, e, batch_train=False):
self.freq[e.item.index] += 1
def score(self, user, candidates):
return self.freq[candidates]
def recommend(self, user, candidates):
scores = self.score(user, candidates)
return self.scores2recos(scores, candidates, rev=True)