Created
August 13, 2017 17:44
-
-
Save victorkohler/1bd238164bf84a06e163c7db3a5d2aef to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" Continuation of implicit_als function""" | |
# Start main loop. For each iteration we first compute X and then Y | |
for i in xrange(iterations): | |
print 'iteration %d of %d' % (i+1, iterations) | |
# Precompute Y-transpose-Y and X-transpose-X | |
yTy = Y.T.dot(Y) | |
xTx = X.T.dot(X) | |
# Loop through all users | |
for u in xrange(user_size): | |
# Get the user row. | |
u_row = confidence[u,:].toarray() | |
# Calculate the binary preference p(u) | |
p_u = u_row.copy() | |
p_u[p_u != 0] = 1.0 | |
# Calculate Cu and Cu - I | |
CuI = sparse.diags(u_row, [0]) | |
Cu = CuI + Y_I | |
# Put it all together and compute the final formula | |
yT_CuI_y = Y.T.dot(CuI).dot(Y) | |
yT_Cu_pu = Y.T.dot(Cu).dot(p_u.T) | |
X[u] = spsolve(yTy + yT_CuI_y + lI, yT_Cu_pu) | |
for i in xrange(item_size): | |
# Get the item column and transpose it. | |
i_row = confidence[:,i].T.toarray() | |
# Calculate the binary preference p(i) | |
p_i = i_row.copy() | |
p_i[p_i != 0] = 1.0 | |
# Calculate Ci and Ci - I | |
CiI = sparse.diags(i_row, [0]) | |
Ci = CiI + X_I | |
# Put it all together and compute the final formula | |
xT_CiI_x = X.T.dot(CiI).dot(X) | |
xT_Ci_pi = X.T.dot(Ci).dot(p_i.T) | |
Y[i] = spsolve(xTx + xT_CiI_x + lI, xT_Ci_pi) | |
return X, Y |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment