Last active
August 29, 2015 14:17
-
-
Save nkt1546789/c46f176d57be80dc2732 to your computer and use it in GitHub Desktop.
Implementation of Weighted Matrix Factorization for Implicit Feedbacks
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
import numpy | |
from numpy import random | |
from numpy import linalg | |
def WMF(R,k=10,alpha=0.1,beta=10.,max_iteration=100000,stop_criterion=1e-6): | |
""" | |
R: feedback matrix like R_ij represents the number that user_i clicks item_j | |
k: dimensionality of latent factor vector | |
alpha: regularization parameter | |
beta: confidence parameter | |
max_iteration: max number of iteration | |
stop_criterion: the number representing stop criterion | |
""" | |
m,n=R.shape | |
P=(R>0)*1. # preference matrix | |
C=1.+beta*R # confidence matrix | |
X=random.rand(m,k) | |
Y=random.rand(n,k) | |
for itr in xrange(max_iteration): | |
Xold=X.copy() | |
Yold=Y.copy() | |
for i in xrange(m): | |
ri=P[i] | |
ci=C[i] | |
if sum(ci)==0: | |
continue | |
X[i]=numpy.dot(linalg.pinv(numpy.dot(Y.T,numpy.c_[ci]*Y)+alpha*numpy.identity(k)),numpy.dot(Y.T,ci*ri)) | |
for j in xrange(n): | |
rj=P[:,j] | |
cj=C[:,j] | |
if sum(cj)==0: | |
continue | |
Y[j]=numpy.dot(linalg.pinv(numpy.dot(X.T,numpy.c_[cj]*X)+alpha*numpy.identity(k)),numpy.dot(X.T,cj*rj)) | |
if linalg.norm(X-Xold)+linalg.norm(Y-Yold)<stop_criterion: | |
break | |
return X,Y |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note: It takes a long time...