Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def user_linear_equation(Y, YtY, Cui, u, regularization, n_factors):
# Xu = (YtCuY + regularization * I)^-1 (YtCuPu)
# YtCuY + regularization * I = YtY + regularization * I + Yt(Cu-I)
# accumulate YtCuY + regularization*I in A
A = YtY + regularization * np.eye(n_factors)
# accumulate YtCuPu in b
b = np.zeros(n_factors)
for i, confidence in nonzeros(Cui, u):
factor = Y[i]
if confidence > 0:
b += confidence * factor
else:
confidence *= -1
A += (confidence - 1) * np.outer(factor, factor)
return A, b
def similar_items(self, itemid, N=10):
""" Returns a list of the most similar other items """
if itemid >= self.similarity.shape[0]:
return []
return sorted(list(nonzeros(self.similarity, itemid)), key=lambda x: -x[1])[:N]
for i, confidence in nonzeros(Cui, u):
if confidence > 0:
r += (confidence - (confidence - 1) * Y[i].dot(x)) * Y[i]
else:
confidence *= -1
r += - (confidence - 1) * Y[i].dot(x) * Y[i]
p = r.copy()
rsold = r.dot(r)
if rsold < 1e-20:
continue
for it in range(cg_steps):
# calculate Ap = YtCuYp - without actually calculating YtCuY
Ap = YtY.dot(p)
for i, confidence in nonzeros(Cui, u):
if confidence < 0:
confidence *= -1
Ap += (confidence - 1) * Y[i].dot(p) * Y[i]
# standard CG update
alpha = rsold / p.dot(Ap)
x += alpha * p
r -= alpha * Ap
rsnew = r.dot(r)
if rsnew < 1e-20:
break
p = r + (rsnew / rsold) * p
rsold = rsnew
X[u] = x
# user_weights = Cholesky decomposition of Wu^-1
# from section 5 of the paper CF for Implicit Feedback Datasets
user_items = user_items.tocsr()
if user_weights is None:
A, _ = user_linear_equation(self.item_factors, self.YtY,
user_items, userid,
self.regularization, self.factors)
user_weights = scipy.linalg.cho_factor(A)
seed_item = self.item_factors[itemid]
# weighted_item = y_i^t W_u
weighted_item = scipy.linalg.cho_solve(user_weights, seed_item)
total_score = 0.0
h = []
for i, (itemid, confidence) in enumerate(nonzeros(user_items, userid)):
if confidence < 0:
continue
factor = self.item_factors[itemid]
# s_u^ij = (y_i^t W^u) y_j
score = weighted_item.dot(factor) * confidence
total_score += score
contribution = (score, itemid)
if i < N:
heapq.heappush(h, contribution)
else:
heapq.heappushpop(h, contribution)
items = (heapq.heappop(h) for i in range(len(h)))
top_contributions = list((i, s) for s, i in items)[::-1]
return total_score, top_contributions, user_weights