Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
ratings = (bm25_weight(ratings, B=0.9) * 5).tocsr()
elif model_name == "bpr":
model = BayesianPersonalizedRanking()
elif model_name == "lmf":
model = LogisticMatrixFactorization()
elif model_name == "tfidf":
model = TFIDFRecommender()
elif model_name == "cosine":
model = CosineRecommender()
elif model_name == "bm25":
model = BM25Recommender(B=0.2)
else:
raise NotImplementedError("TODO: model %s" % model_name)
# train the model
log.debug("training model %s", model_name)
start = time.time()
model.fit(ratings)
log.debug("trained model '%s' in %s", model_name, time.time() - start)
log.debug("calculating top movies")
user_count = np.ediff1d(ratings.indptr)
to_generate = sorted(np.arange(len(titles)), key=lambda x: -user_count[x])
log.debug("calculating similar movies")
with tqdm.tqdm(total=len(to_generate)) as progress:
from implicit.bpr import BayesianPersonalizedRanking
from implicit.datasets.lastfm import get_lastfm
from implicit.lmf import LogisticMatrixFactorization
from implicit.nearest_neighbours import (BM25Recommender, CosineRecommender,
TFIDFRecommender, bm25_weight)
# maps command line model argument to class name
MODELS = {"als": AlternatingLeastSquares,
"nmslib_als": NMSLibAlternatingLeastSquares,
"annoy_als": AnnoyAlternatingLeastSquares,
"faiss_als": FaissAlternatingLeastSquares,
"tfidf": TFIDFRecommender,
"cosine": CosineRecommender,
"bpr": BayesianPersonalizedRanking,
"lmf": LogisticMatrixFactorization,
"bm25": BM25Recommender}
def get_model(model_name):
print("getting model %s" % model_name)
model_class = MODELS.get(model_name)
if not model_class:
raise ValueError("Unknown Model '%s'" % model_name)
# some default params
if issubclass(model_class, AlternatingLeastSquares):
params = {'factors': 16, 'dtype': np.float32}
elif model_name == "bm25":
params = {'K1': 100, 'B': 0.5}
elif model_name == "bpr":
params = {'factors': 63}
elif model_name == "lmf":
def __init__(self, K=20, K1=1.2, B=.75, num_threads=0):
super(BM25Recommender, self).__init__(K, num_threads)
self.K1 = K1
self.B = B