You are on page 1of 4

Python Coding assignment

1) Reproduce the code for matrix factorization.


Matrix factorization code:
try:
import numpy
except:
print "This implementation requires the numpy module."
exit(0)
@INPUT:
R : a matrix to be factorized, dimension N x M
P : an initial matrix of dimension N x K
Q : an initial matrix of dimension M x K
K : the number of latent features
steps : the maximum number of steps to perform the optimisation
alpha : the learning rate
beta : the regularization parameter
@OUTPUT:
the final matrices P and Q
def matrix_factorization(R, P, Q, K, steps=5000, alpha=0.0002, beta=0.02):
Q = Q.T
for step in xrange(steps):
for i in xrange(len(R)):
for j in xrange(len(R[i])):
if R[i][j] > 0:
eij = R[i][j] - numpy.dot(P[i,:],Q[:,j])
for k in xrange(K):
P[i][k] = P[i][k] + alpha * (2 * eij * Q[k][j] - beta * P[i][k])
Q[k][j] = Q[k][j] + alpha * (2 * eij * P[i][k] - beta * Q[k][j])
eR = numpy.dot(P,Q)
e=0
for i in xrange(len(R)):
for j in xrange(len(R[i])):
if R[i][j] > 0:
e = e + pow(R[i][j] - numpy.dot(P[i,:],Q[:,j]), 2)
for k in xrange(K):
e = e + (beta/2) * ( pow(P[i][k],2) + pow(Q[k][j],2) )
if e < 0.001:
break
return P, Q.T
if __name__ == "__main__":
R=[
[5,3,0,1],
[4,0,0,1],
[1,1,0,5],
[1,0,0,4],
[0,1,5,4],
]

R = numpy.array(R)
N = len(R)
M = len(R[0])
K=2
P = numpy.random.rand(N,K)
Q = numpy.random.rand(M,K)
nP, nQ = matrix_factorization(R, P, Q, K)

2) Given the sparse matrix R, obtain the complete matrix after X-Y factorization.

3) Note that the third movie has not been watched by first four viewers. Based on your results,
who should be recommended this movie? Consider a predicted rating of 3 and above a criterion for
recommendation.
There are six lines having a place with have watchers and 3 sections having a place with four motion
pictures. The watchers' appraised the films in a size of 1 to 5. The sections with zeros are basically
the films that the relating watcher did not watch and henceforth we need to anticipate the rating for
those motion pictures and if it is anticipated to be high in rating - Netflix will prescribe them to
watch. So, in a perfect world, we need to and the missing qualities in the grid. Here, we accept the
shrouded measurement or rank to be k = 3 with the end goal that 3X4 grid can be decayed into two
frameworks of 4X2 (tall network) and 1X4 (fat lattice). Here, R is our A framework, P and Q are our X
and Y lattices and the missing qualities in the grid. Here, we accept the shrouded measurement or
rank to be k = 2 with the end goal that 5X4 grid can be decayed into two frameworks of 5X2 (tall
network) and 2X5 (fat lattice). Here, R is our A framework, P and Q are our X and Y lattices.
4)
a) The complete matrix R_hat and discussion on which movie to recommend which person.

The person will respond to movie k=1 and he watches most of the movies on the scale from 2-5

b) Print the final estimated X and Y matrices and the final error matrix eij showing error values for
all the elements of R matrix
c) A plot on error versus step number to show how error is changing. Change the value of alpha to
0.5, 0.1, 0.01, 0.05, 0.001 and show five error versus step size curves in one plot. Comment how
change in learning rate affects the learning speed and accuracy.

d) Identify the equations discussed in the lecture note as implemented in the python script. Submit
your code highlighted with equation numbers.

You might also like