O'Reilly logo

Building Recommendation Engines by Suresh Kumar Gorakala

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Item-based recommendations

IBCF is very similar to UBCF but with very minor changes in how we use the rating matrix.

The first step is to calculate the similarities between movies, as follows:

Since we have to calculate the similarity between movies, we use movie count as k instead of user count:

k = ratings_train.shape[1] 
neigh = NearestNeighbors(k,'cosine') 

We fit the transpose of the rating matrix to the NearestNeighbors object:

neigh.fit(ratings_train.T) 

Calculate the cosine similarity distance between each movie pair:

top_k_distances,top_k_users = neigh.kneighbors(ratings_train.T, return_distance=True) 
top_k_distances.shape 
(1682, 1682) 

The next step is to predict the movie ratings using the following code:

item__pred = ratings_train.dot(top_k_distances) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required