Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
class CosineRecommender(ItemItemRecommender):
""" An Item-Item Recommender on Cosine distances between items """
def fit(self, counts, show_progress=True):
# cosine distance is just the dot-product of a normalized matrix
ItemItemRecommender.fit(self, normalize(counts), show_progress)
class TFIDFRecommender(ItemItemRecommender):
""" An Item-Item Recommender on TF-IDF distances between items """
def fit(self, counts, show_progress=True):
weighted = normalize(tfidf_weight(counts))
ItemItemRecommender.fit(self, weighted, show_progress)
class BM25Recommender(ItemItemRecommender):
""" An Item-Item Recommender on BM25 distance between items """
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
def fit(self, counts, show_progress=True):
weighted = bm25_weight(counts, self.K1, self.B)
ItemItemRecommender.fit(self, weighted, show_progress)
def tfidf_weight(X):
""" Weights a Sparse Matrix by TF-IDF Weighted """
X = coo_matrix(X)
# calculate IDF
ret = cls()
ret.similarity = similarity
ret.scorer = NearestNeighboursScorer(similarity)
ret.K = m['K']
return ret
class CosineRecommender(ItemItemRecommender):
""" An Item-Item Recommender on Cosine distances between items """
def fit(self, counts, show_progress=True):
# cosine distance is just the dot-product of a normalized matrix
ItemItemRecommender.fit(self, normalize(counts), show_progress)
class TFIDFRecommender(ItemItemRecommender):
""" An Item-Item Recommender on TF-IDF distances between items """
def fit(self, counts, show_progress=True):
weighted = normalize(tfidf_weight(counts))
ItemItemRecommender.fit(self, weighted, show_progress)
class BM25Recommender(ItemItemRecommender):
""" An Item-Item Recommender on BM25 distance between items """
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
def fit(self, counts, show_progress=True):
weighted = bm25_weight(counts, self.K1, self.B)
ItemItemRecommender.fit(self, weighted, show_progress)
def load(cls, filename):
# numpy.save automatically appends a npz suffic, numpy.load doesn't apparently
if not filename.endswith(".npz"):
filename = filename + ".npz"
m = numpy.load(filename)
similarity = csr_matrix((m['data'], m['indices'], m['indptr']), shape=m['shape'])
ret = cls()
ret.similarity = similarity
ret.scorer = NearestNeighboursScorer(similarity)
ret.K = m['K']
return ret
class CosineRecommender(ItemItemRecommender):
""" An Item-Item Recommender on Cosine distances between items """
def fit(self, counts, show_progress=True):
# cosine distance is just the dot-product of a normalized matrix
ItemItemRecommender.fit(self, normalize(counts), show_progress)
class TFIDFRecommender(ItemItemRecommender):
""" An Item-Item Recommender on TF-IDF distances between items """
def fit(self, counts, show_progress=True):
weighted = normalize(tfidf_weight(counts))
ItemItemRecommender.fit(self, weighted, show_progress)
class BM25Recommender(ItemItemRecommender):
""" An Item-Item Recommender on BM25 distance between items """
def __init__(self, K=20, K1=1.2, B=.75, num_threads=0):