red_devil red_devil - 29 days ago 7
Python Question

Understanding Alternating Least Squares for Collaborative Filtering

I have been messing around with recommendation engines for the last few days and came across this very nice tutorial which demonstrates the use of Alternating Least Squares in Collaborative filters:http://bugra.github.io/work/notes/2014-04-19/alternating-least-squares-method-for-collaborative-filtering/

I managed to follow the instructions till the very last step. It's the part where the author writes the code to print recommendations. The code snippet is as follows:-

def print_recommendations(W=W, Q=Q, Q_hat=Q_hat, movie_titles=movie_titles):
Q_hat -= np.min(Q_hat)
Q_hat *= float(5) / np.max(Q_hat)
movie_ids = np.argmax(Q_hat - 5 * W, axis=1)
for jj, movie_id in zip(range(m), movie_ids):

print('User {} liked {}\n'.format(jj + 1, ', '.join([movie_titles[ii] for ii, qq in enumerate(Q[jj]) if qq > 3])))

print('\n User {} recommended movie is {} - with predicted rating: {}'.format( jj + 1, movie_titles[movie_id], Q_hat[jj, movie_id]))

print('\n' + 100 * '-' + '\n')


In this snippet, W is the weight matrix. Q matrix is used to formalize the notion of confidence which the ratings measure. Therefore:

Q = 1 if user u rated item i

Q= 0 if user u did not rate item i


Q_hat is the new matrix obtained after implementing the ALS algorithm, after specified number of iterations.

I cannot understand why the author implements these two steps in particular:

Q_hat -= np.min(Q_hat)
Q_hat *= float(5) / np.max(Q_hat)


Could somebody guide me and help me understand this? I would really appreciate it.

Thanks

Edit : Here is a gist link to the original function: https://gist.github.com/arjun180/71124392b0b70f7b96a8826b59400b99

Answer

This is a normalization of the predicted ratings.

Q_hat -= np.min(Q_hat)

Here the author is subtracting the smallest in the predicted ratings matrix to all predicted values.

This guarantees that all predicted ratings start at 0.

Q_hat *= float(5) / np.max(Q_hat)

Here the author is normalizing the predicted ratings to range from 0 up to 5.